ElasticSearch: Java API
来源:互联网 发布:yum命令安装mysql 编辑:程序博客网 时间:2024/04/27 20:18
ElasticSearch provides Java API, thus it executes all operations asynchronously by using client object. Client object can execute the operations in a bulk accumulatively.Java API can be used internally in order to execute all APIs in ElasticSearch.
In this tutorial, we will consider how to carry out some operations with Java API in a standalone Java application that is similar to the ones we made inprevious article with console.
Dependency
ElasticSearch is hosted on maven central. In your Maven Project, you can define which ElasticSearch version you want to use in your pom.xml file as shown below:
1
<
dependency
>
2
<
groupId
>org.elasticsearch</
groupId
>
3
<
artifactId
>elasticsearch</
artifactId
>
4
<
version
>0.90.3</
version
>
5
</
dependency
>
Client
By client, you can perform administrative tasks in a running cluster and some operations such as standard index, get, delete and search operations in an existing cluster and also you can launch all of the nodes.
The most common way of obtaining an ElasticSearch client is creating an embedded node which acts like a node in a cluster and then requesting a client from that embedded node.
Another way of obtaining a client is creating a TransportClient (it connects from remote by using transport module) which connects to a cluster.
It should be considered to use the same version of client and cluster while using Java API. The difference between client and cluster versions may cause some incompatibilities.
Node client
The simplest way of getting a client instance is the node based client.
1
Node node = nodeBuilder().node();
2
Client client = node.client();
When a node is started, it joins to “elasticsearch” cluster. You can create different clusters by cluster.name setting or clusterName method. In/src/main/resources directory and in elasticsearch.yml file in your project:
1
cluster.name: yourclustername
Or in Java code:
1
Node node = nodeBuilder().clusterName(
"yourclustername"
).node();
2
Client client = node.client();
Creating index
The Index API allows you to type a JSON document into a specific index and makes it searchable. There are different ways of generating JSON document. In here we used map which represents JSON structure very well.
01
public
static
Map<String, Object> putJsonDocument(String title, String content, Date postDate,
02
String[] tags, String author){
03
04
Map<String, Object> jsonDocument =
new
HashMap<String, Object>();
05
06
jsonDocument.put(
"title"
, title);
07
jsonDocument.put(
"conten"
, content);
08
jsonDocument.put(
"postDate"
, postDate);
09
jsonDocument.put(
"tags"
, tags);
10
jsonDocument.put(
"author"
, author);
11
12
return
jsonDocument;
13
}
01
Node node = nodeBuilder().node();
02
Client client = node.client();
03
04
client.prepareIndex(
"kodcucom"
,
"article"
,
"1"
)
05
.setSource(putJsonDocument(
"ElasticSearch: Java API"
,
06
"ElasticSearch provides the Java API, all operations "
07
+
"can be executed asynchronously using a client object."
,
08
new
Date(),
09
new
String[]{
"elasticsearch"
},
10
"Hüseyin Akdoğan"
)).execute().actionGet();
11
12
node.close();
With the above code, we generate an index by the name of kodcucom and a type by the name of article with standard settings and a record (we don’t have to give an ID here) whose ID value is 1 is stored to ElasticSearch.
Getting document
The Get API allows you to get a typed JSON document as ID based from the index.
01
GetResponse getResponse = client.prepareGet(
"kodcucom"
,
"article"
,
"1"
).execute().actionGet();
02
03
Map<String, Object> source = getResponse.getSource();
04
05
System.out.println(
"------------------------------"
);
06
System.out.println(
"Index: "
+ getResponse.getIndex());
07
System.out.println(
"Type: "
+ getResponse.getType());
08
System.out.println(
"Id: "
+ getResponse.getId());
09
System.out.println(
"Version: "
+ getResponse.getVersion());
10
System.out.println(source);
11
System.out.println(
"------------------------------"
);
Search
The Search API allows you to execute a search query and to get the matched results. The query can be executed across more than one indices and types. The query can be provided by usingquery Java API or filter Java API. Below you can see an example whose body of search request is built by usingSearchSourceBuilder.
01
public
static
void
searchDocument(Client client, String index, String type,
02
String field, String value){
03
04
SearchResponse response = client.prepareSearch(index)
05
.setTypes(type)
06
.setSearchType(SearchType.QUERY_AND_FETCH)
07
.setQuery(fieldQuery(field, value))
08
.setFrom(
0
).setSize(
60
).setExplain(
true
)
09
.execute()
10
.actionGet();
11
12
SearchHit[] results = response.getHits().getHits();
13
14
System.out.println(
"Current results: "
+ results.length);
15
for
(SearchHit hit : results) {
16
System.out.println(
"------------------------------"
);
17
Map<String,Object> result = hit.getSource();
18
System.out.println(result);
19
}
20
21
}
1
searchDocument(client,
"kodcucom"
,
"article"
,
"title"
,
"ElasticSearch"
);
Updating
Below you can see an example of a field update.
01
public
static
void
updateDocument(Client client, String index, String type,
02
String id, String field, String newValue){
03
04
Map<String, Object> updateObject =
new
HashMap<String, Object>();
05
updateObject.put(field, newValue);
06
07
client.prepareUpdate(index, type, id)
08
.setScript(
"ctx._source."
+ field +
"="
+ field)
09
.setScriptParams(updateObject).execute().actionGet();
10
}
1
updateDocument(client,
"kodcucom"
,
"article"
,
"1"
,
"tags"
,
"big-data"
);
Deleting
The delete API allows you to delete a document whose ID value is specified. You can see below an example of deleting a document whose index, type and value is specified.
1
public
static
void
deleteDocument(Client client, String index, String type, String id){
2
3
DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();
4
System.out.println(
"Information on the deleted document:"
);
5
System.out.println(
"Index: "
+ response.getIndex());
6
System.out.println(
"Type: "
+ response.getType());
7
System.out.println(
"Id: "
+ response.getId());
8
System.out.println(
"Version: "
+ response.getVersion());
9
}
1
deleteDocument(client,
"kodcucom"
,
"article"
,
"1"
);
The sample application: ElasticSearch
- Elasticsearch之 java api
- elasticsearch java api优化
- ElasticSearch: Java API
- Elasticsearch Java API总汇
- elasticsearch java api资料
- elasticsearch java API ------搜索
- elasticsearch java api demo
- ElasticSearch的Java API
- Elasticsearch Java API
- 03 elasticsearch java api
- elasticsearch java query API
- Elasticsearch Java API 开发
- ElasticSearch Java API
- ElasticSearch java API (1)
- Elasticsearch java API (2)
- Elasticsearch java API (3)
- Elasticsearch JAVA API 学习
- elasticsearch java api
- 2013英语82经验之谈
- 雷澤歸妹
- UVA 10827 Maxsum升级版
- --java实现约瑟夫环问题--java学习日记1(基础知识)
- exec: arm-none-linux-gnueabi-gcc: not found
- ElasticSearch: Java API
- C# 导出Excel方法
- 至兴哥
- Java编程思想之6访问权限控制
- SSH深度历险(九) Struts2+DWZ+Uploadify实现多文件(文件和图片等等)上传
- Cell Broadcast Service (CBS)
- ci框架---控制器名和方法名不能相同
- 转载:JUnit4的参数化测试和打包测试
- Linux netfilter 学习笔记 之十一 ip层netfilter的NAT模块初始化以及NAT原理