1 Preface
Restclient is the lower-level API, which uses a high-level API based on its encapsulation, the resthighlevelclient.
The dependencies you need to add are as follows:
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.6.10</version></dependency><dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>5.6.10</version></dependency>
Lower versions of ES may not support resthighlevelclient.
2 Test Cases
The test code is as follows:
Package Com.xpleaf.es.leaf;import Org.apache.http.httphost;import org.elasticsearch.action.search.SearchRequest; Import Org.elasticsearch.action.search.searchresponse;import Org.elasticsearch.client.restclient;import Org.elasticsearch.client.resthighlevelclient;import Org.elasticsearch.common.unit.timevalue;import Org.elasticsearch.index.query.*;import Org.elasticsearch.search.builder.searchsourcebuilder;import Org.elasticsearch.search.sort.sortorder;import org.junit.after;import Org.junit.before;import org.junit.Test;/** * @author xpleaf * @GitHub https://github.com/xpleaf * @Blog http://blog.51cto.com/xpleaf * @date 2018/10/7 pm 12:05 */public Class Resthighlevelclienttest {private httphost[] eshosts = new httphost[]{new Httphost ("localhost", 9200 ) }; Private restclient restclient = null; Private resthighlevelclient client = null; Private Boolquerybuilder boolquerybuilder = null; @Before public void Init () throws Exception {//1. Creating Restclient Objects Restclient = Restclient.builder (eshosts). build (); Client = new Resthighlevelclient (restclient); 2. Create Boolquerybuilder Object boolquerybuilder = new Boolquerybuilder (); 3. Set Boolquerybuilder condition Matchphrasequerybuilder Matchphrasequerybuilder = querybuilders. MatchPhra Sequery ("Key_word", "Guangdong"); Matchphrasequerybuilder matchPhraseQueryBuilder2 = querybuilders. Matchphrasequery ("Key_word", "Lakers"); Rangequerybuilder Rangequerybuilder = querybuilders. Rangequery ("Postdate"). From ("2016-0 1-01 00:00:00 "); Sub-boolquerybuilder condition, which is used to denote the relationship of the query condition or boolquerybuilder Childboolquerybuilder = new Boolquerybuilder () . should (Matchphrasequerybuilder). should (MatchPhraseQueryBuilder2); 4. Add the query condition to Boolquerybuilder in Boolquerybuilder. Must (Childboolquerybuilder). Must (ran Gequerybuilder); }//Test SearchsourCebuilder search @Test public void test01 () throws Exception {//1. Creating and setting Searchsourcebuilder objects Searchsourc Ebuilder Searchsourcebuilder = new Searchsourcebuilder (); Query criteria---> Generate DSL query statement searchsourcebuilder.query (Boolquerybuilder); The first few pages of Searchsourcebuilder.from (0); How many data per page searchsourcebuilder.size (100); Get the fields (columns) and the columns you don't need to get Searchsourcebuilder.fetchsource (new string[]{"Postdate", "Key_word"}, New string[]{}); Set Collation Searchsourcebuilder.sort ("Postdate", SORTORDER.ASC); Set the time-out to 2s searchsourcebuilder.timeout (new TimeValue (2000)); 2. Create and set SearchRequest object SearchRequest searchrequest = new SearchRequest (); Set the index and type searchrequest.indices ("Spnews") to search for the request. Types ("News"); Set Searchsourcebuilder Query Properties Searchrequest.source (Searchsourcebuilder); 3. Query SearchResponse SearchResponse = Client.search (searchrequest); SysteM.out.println (Searchresponse.tostring ()); } @After public void after () throws Exception {restclient.close (); }}
3 Parsing 3.1 Rest Json
Query criteria for the above test case:
// 2.创建BoolQueryBuilder对象boolQueryBuilder = new BoolQueryBuilder();// 3.设置boolQueryBuilder条件MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders .matchPhraseQuery("key_word", "广东");MatchPhraseQueryBuilder matchPhraseQueryBuilder2 = QueryBuilders .matchPhraseQuery("key_word", "湖人");RangeQueryBuilder rangeQueryBuilder = QueryBuilders .rangeQuery("postdate") .from("2016-01-01 00:00:00");// 子boolQueryBuilder条件条件,用来表示查询条件or的关系BoolQueryBuilder childBoolQueryBuilder = new BoolQueryBuilder() .should(matchPhraseQueryBuilder) .should(matchPhraseQueryBuilder2);// 4.添加查询条件到boolQueryBuilder中boolQueryBuilder .must(childBoolQueryBuilder) .must(rangeQueryBuilder);
Actually translates to the following ES query statement (you can debug it and use Searchsourcebuilder to do this kind of conversion):
{"From": 0, "size": +, "timeout": "2000ms", "query": {"bool": {"must": [{"bool": {"should": [{"match_phrase": {"Key_word": { "Query": "Guangdong", "slop": 0, "Boost": 1.0}} }, {"Match_phrase": {"Key_word": {"Query" : "Lakers", "slop": 0, "Boost": 1.0}} }], "Disable_coord": false, "adjust_pure_negative": true, "boost": 1.0 }}, {"range": {"postdate": {"from": "2016-01-01 00:00:00", "to": null, "Include_lower": True, "Include_upper": true, "boost": 1. 0}}}], "Disable_coord": false, "adjust_pure_negative": true, "Boost": 1.0}}, "_source ": {" includes ": [" Postdate "," Key_word "]," excludes ": []}," sort ": [{" Postdate ": {"Order": "ASC"}} ]}
3.2 Match Query VS match_phrase query
Note the difference:
- Match query: A word Word will be used to make a sentence, and the document will be searched if any of the words in the query statement are matched. If you want to query the document matching all the keywords, you can connect with the and operator;
- Match_phrase query: The following two conditions will be met to be searched
- (1) All words are to appear in this field after the word breaker
- (2) The order of the terms in the field must be consistent
Elasticsearch:restclient+searchsourcebuilder Use Cases