ElasticSearch Java API 官方文档:
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html
ElasticSearch提供了Java API,因此可通过使用Client对象异步执行所有操作。Client对象可以批量执行操作,累计。 Java的API可以在内部使用,以执行所有的API在ElasticSearch。
在本教程中,我们将考虑如何开展将Java API一些操作用在一个独立的Java应用程序里,类似于那些我们在上一篇文章中做的。
Dependency
ElasticSearch托管于Maven的中央。在你的Maven项目,你可以定义你想要的,如下图所示在你的pom.xml文件使用哪个版本ElasticSearch:
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>0.90.3</version> </dependency>
Client
使用Client,您可以执行在正在运行的群集管理任务,如一些操作,如标准索引,get,删除和搜索操作,在现有的集群中。另外,还可以启动所有节点。
获得ElasticSearchclient的最常见的方式是建立它就像在一个群集中的节点的嵌入式节点以及请求客户端从该嵌入节点。
另一种方式获得客户端是创建TransportClient(它从远程连接集群通过使用transport组件),其连接到群集的另一种方式。
应该考虑使用客户端和集群的相同版本,当使用Java API时。客户端和群集版本之间的差异可能会导致某些不兼容性。
Node Client
The simplest way of getting a client instance is the node based client.
Node node = nodeBuilder().node(); Client client = node.client();
When a node is started, it joins to the "elasticsearch" cluster. You can create different clusters using the cluster.name setting or clusterName method in the/src/main/resources directory and in elasticsearch.yml file in your project:
cluster.name: yourclustername
Or in Java code:
Node node = nodeBuilder().clusterName("yourclustername").node(); 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 documents. Here we used map, which represents JSON structure very well.
public static Map<String, Object> putJsonDocument(String title, String content, Date postDate, String[] tags, String author){ Map<String, Object> jsonDocument = new HashMap<String, Object>(); jsonDocument.put("title", title); jsonDocument.put("conten", content); jsonDocument.put("postDate", postDate); jsonDocument.put("tags", tags); jsonDocument.put("author", author); return jsonDocument; }
Node node = nodeBuilder().node(); Client client = node.client(); client.prepareIndex("kodcucom", "article", "1") .setSource(putJsonDocument("ElasticSearch: Java API", "ElasticSearch provides the Java API, all operations " + "can be executed asynchronously using a client object.", new Date(), new String[]{"elasticsearch"}, "Hüseyin Akdoğan")).execute().actionGet(); 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 of 1 is stored to ElasticSearch.
Getting Document
The Get API allows you to get a typed JSON document, based on the ID, from the index.
GetResponse getResponse = client.prepareGet("kodcucom", "article", "1").execute().actionGet(); Map<String, Object> source = getResponse.getSource(); System.out.println("------------------------------"); System.out.println("Index: " + getResponse.getIndex()); System.out.println("Type: " + getResponse.getType()); System.out.println("Id: " + getResponse.getId()); System.out.println("Version: " + getResponse.getVersion()); System.out.println(source); System.out.println("------------------------------");
Search
The Search API allows you to execute a search query and get the matched results. The query can be executed across more than one indices and types. The query can be provided by using query Java API or filter Java API. Below you can see an example whose body of search request is built by using SearchSourceBuilder.
public static void searchDocument(Client client, String index, String type, String field, String value){ SearchResponse response = client.prepareSearch(index) .setTypes(type) .setSearchType(SearchType.QUERY_AND_FETCH) .setQuery(fieldQuery(field, value)) .setFrom(0).setSize(60).setExplain(true) .execute() .actionGet(); SearchHit[] results = response.getHits().getHits(); System.out.println("Current results: " + results.length); for (SearchHit hit : results) { System.out.println("------------------------------"); Map<String,Object> result = hit.getSource(); System.out.println(result); } }
searchDocument(client, "kodcucom", "article", "title", "ElasticSearch");
Updating
Below you can see an example of a field update.
public static void updateDocument(Client client, String index, String type, String id, String field, String newValue){ Map<String, Object> updateObject = new HashMap<String, Object>(); updateObject.put(field, newValue); client.prepareUpdate(index, type, id) .setScript("ctx._source." + field + "=" + field) .setScriptParams(updateObject).execute().actionGet(); }
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.
public static void deleteDocument(Client client, String index, String type, String id){ DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet(); System.out.println("Information on the deleted document:"); System.out.println("Index: " + response.getIndex()); System.out.println("Type: " + response.getType()); System.out.println("Id: " + response.getId()); System.out.println("Version: " + response.getVersion()); }
deleteDocument(client, "kodcucom", "article", "1");
See the sample application here.
Adding mapping to a type from Java (索引库中索引的字段名及其数据类型进行定义,包括是否存储、分词)
阅读全文……