Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Elasticsearch:RestClient+SearchSourceBuilder use case

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1 preface

RestClient is the lower-level API, and the high-level API based on its encapsulation, or RestHighLevelClient, is used here.

The dependencies you need to add are as follows:

Org.elasticsearch elasticsearch 5.6.10 org.elasticsearch.client elasticsearch-rest-high-level-client 5.6.10

Earlier versions of es may not support RestHighLevelClient.

Check out its maven central warehouse: https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client

As you can see, versions above 5.6 are available.

2 Test case

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 https://blog.51cto.com/xpleaf * @ date 12:05 on 2018-10-7 * / 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. Create the RestClient object restClient = RestClient.builder (esHosts). Build (); client = new RestHighLevelClient (restClient); / / 2. Create a BoolQueryBuilder object boolQueryBuilder = new BoolQueryBuilder (); / / 3. Set the boolQueryBuilder condition MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders .matchPhraseQuery ("key_word", "Guangdong"); MatchPhraseQueryBuilder matchPhraseQueryBuilder2 = QueryBuilders .matchPhraseQuery ("key_word", "Lakers"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders .rangeQuery ("postdate"). From ("2016-01-01 00:00:00") / / sub-boolQueryBuilder condition, which is used to express the relation of query condition or BoolQueryBuilder childBoolQueryBuilder = new BoolQueryBuilder (). Should (matchPhraseQueryBuilder). Should (matchPhraseQueryBuilder2); / / 4. Add query criteria to boolQueryBuilder boolQueryBuilder. Must (childBoolQueryBuilder). Must (rangeQueryBuilder);} / Test SearchSourceBuilder search @ Test public void test01 () throws Exception {/ / 1. Create and set SearchSourceBuilder objects SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder (); / / query conditions-- > generate DSL query statements searchSourceBuilder.query (boolQueryBuilder); / / pages searchSourceBuilder.from (0); / / how many pieces of data per page searchSourceBuilder.size (100) / / the fields (columns) obtained and the columns searchSourceBuilder.fetchSource that do not need to be obtained (new String [] {"postdate", "key_word"}, new String [] {}); / / set collation searchSourceBuilder.sort ("postdate", SortOrder.ASC); / / set the timeout to 2s searchSourceBuilder.timeout (new TimeValue (2000)); / / 2. Create and set the SearchRequest object SearchRequest searchRequest = new SearchRequest (); / / set the index and type to be searched by request searchRequest.indices ("spnews"). Types ("news"); / / set the SearchSourceBuilder query property searchRequest.source (searchSourceBuilder); / / 3. Query SearchResponse searchResponse = client.search (searchRequest); System.out.println (searchResponse.toString ());} @ After public void after () throws Exception {restClient.close ();}} 3 analyze 3.1 Rest Json

Query conditions for the above test case:

/ / 2. Create a BoolQueryBuilder object boolQueryBuilder = new BoolQueryBuilder (); / / 3. Set the boolQueryBuilder condition MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders .matchPhraseQuery ("key_word", "Guangdong"); MatchPhraseQueryBuilder matchPhraseQueryBuilder2 = QueryBuilders .matchPhraseQuery ("key_word", "Lakers"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders .rangeQuery ("postdate"). From ("2016-01-01 00:00:00") / / sub-boolQueryBuilder condition, which is used to express the relation of query condition or BoolQueryBuilder childBoolQueryBuilder = new BoolQueryBuilder (). Should (matchPhraseQueryBuilder). Should (matchPhraseQueryBuilder2); / / 4. Add query conditions to boolQueryBuilder boolQueryBuilder. Must (childBoolQueryBuilder). Must (rangeQueryBuilder)

It will actually be translated into an es query statement like this (you can debug it, which is what searchSourceBuilder is used to do):

{"from": 0, "size": 100, "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 differences:

Match query: the query statement will be segmented. After the word segmentation, any word entry in the query statement will be matched, and the document will be searched. If you want to query documents that match all keywords, you can use the and operator to connect; match_phrase query: the following two conditions are met before they will be searched. (1) after the participle, all entries in the (2) field should be in the same order.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report