42. In-depth explanation of Elasticsearch Java API

0. Inscription

There were many applications of Elasticsearch before, but most of them focused on the synchronization between relational and non-relational databases and Elasticsearch. The above content completes the supply of the basic data volume required by Elasticsearch. However, if you want to find business data related to yourself in the massive data, realize full-text retrieval, classification statistics and other functions of the existing data and apply them to the business system, you must use the Java API to achieve it.

1. Overview of Elasticsearch Java API

Research results related to the use of the Elasticsearch Java API:

1.1 Most of the domestic blog posts explaining the Elasticsearch Java API focus on implementation and do not explain the Elasticsearch Java API as a whole. Such a problem is that a headache treats the head, and a foot pain treats the foot, which is always unreasonable.

1.2 Elasticsearch 1.X, 2.X, 5.X With the iteration of the version, in addition to the system upgrade, the Java API has also been relatively adjusted.

That is to say, the API of 1.X is not universal in 2.X and 5.X and even the future 6.X version. FuzzyLikeThisQuery such as 1.x no longer exists in subsequent versions.

1.3 The Java API on the Elasticsearch official website provides a relatively rich interpretation. (The official website address is given at the end of the article)

1.4 The Elasticsearch Java API is roughly divided into the following four categories:

1.4.1、CustomerTransport

Step 1: Obtain the client; 
Step 2: Perform the corresponding add, delete, modify, and check operations.

Query usage reference (I have verified the following, it is easy to use ok):

/**
* Query Search
* @param index
* @param type
* @param term
* @param queryString
*/
private static void querySearch(String index, String type,String term,String queryString){
Client client = createTransportClient();
SearchResponse response = client.prepareSearch(index)
  .setTypes(type)
  // 设置查询类型
// 1.SearchType.DFS_QUERY_THEN_FETCH = 精确查询
// 2.SearchType.SCAN = 扫描查询,无序
// 3.SearchType.COUNT = 不设置的话,这个为默认值,还有的自己去试试吧
  .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  // 设置查询关键词
  .setQuery(QueryBuilders.matchQuery(term, queryString))
  .addHighlightedField(term)
  .setHighlighterPreTags("<em>")
  .setHighlighterPostTags("</em>")
  // 设置查询数据的位置,分页用
.setFrom(0)
// 设置查询结果集的最大条数
.setSize(60)
// 设置是否按查询匹配度排序
.setExplain(true)
// 最后就是返回搜索响应信息
  .execute()
  .actionGet();
SearchHits searchHits = response.getHits();
System.out.println("-----------------在["+term+"]中搜索关键字["+queryString+"]---------------------");
System.out.println("共匹配到:"+searchHits.getTotalHits()+"条记录!");
SearchHit[] hits = searchHits.getHits();
for (SearchHit searchHit : hits) {
//获取高亮的字段
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
HighlightField highlightField = highlightFields.get(term);
System.out.println("高亮字段:"+highlightField.getName()+"\n高亮部分内容:"+highlightField.getFragments()[0].string());
Map<String, Object> sourceAsMap = searchHit.sourceAsMap();
Set<String> keySet = sourceAsMap.keySet();
for (String string : keySet) {
//key value 值对应关系
System.out.println(string+":"+sourceAsMap.get(string));
}
System.out.println();
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

1.4.2、RestClient

elasticsearch 5.0 introduces a new client RestClient that uses the HTTP API elasticsearch instead of the internal protocol. This requires fewer dependencies. You also don't need to pay attention to so many versions, the current client can also be used for elasticsearch 2.x version.

Use the reference as follows (I have no code verification below):

HttpEntity entity = new NStringEntity(
      "{ \"query\": { \"match_all\": {}}}",
      ContentType.APPLICATION_JSON);
  // alternative: performRequestAsync
  Response response = restClient.performRequest("POST",                                     "/_search", emptyMap(), entity);
  String json = toString(response.getEntity());
  // ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

1.4.3 、 There is

Jest is a Java HTTP Rest client for ElasticSearch. Jest fills the gap where ElasticSearch lacks a Http Rest interface client. 
Compiled by Maven, different versions need to be set. Now the latest Elasticsearch version of pom.xml is 5.3.3 (May 27, 2017). The API of 5.3.3 inserts index data into ES2.3.4, which cannot guarantee the normal insertion of data and no error is reported.

Specific use reference (the following example runs through, verify ok):

/**
* @brief:全文检索入口
* @throws Exception
*/
private static void fullTextQuery(String queryString) throws Exception {
JestClient jestClient = JestExample.getJestClient();   //工厂模式实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryStringQuery(queryString));

Search search = new Search.Builder(searchSourceBuilder.toString())
// .addIndex("article")
.build();
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

1.4.4、Spring Data Elasticsearch

This one has not been studied in depth. Spring Data Elasticsearch is more suitable for developers who use Spring Database and don't want to touch the REST API directly. Reference 2 is attached for details.

2. Summary

1) The calling methods of the above APIs have their own advantages and disadvantages, and the selection is made according to the actual development situation. 
2) For the operations of adding, deleting, modifying and checking the TransportClient method of the specific Java API and the Jest method, please refer to (Appendix 4 below). 
3) There is still a long way to go in the practical development of ES, especially distributed deployment, principles, troubleshooting, and the use of Java APIs for efficient retrieval.

3. Reference

1), API comparison: http://blog.florian-hopf.de/2016/11/java-clients-elasticsearch.html 
2), official website API address: https://www.elastic.co/guide/en/ elasticsearch/client/java-api/2.3/index.html 
3), Jest address: https://github.com/searchbox-io/Jest 
4), code example: https://github.com/ameizi/elasticsearch- jest-example

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325483512&siteId=291194637