您好,登錄后才能下訂單哦!
RestClient是較低層的API,這里使用基于其封裝的高層API,即RestHighLevelClient。
需要添加的依賴如下:
<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>
較低版本的es可能不支持RestHighLevelClient。
查看一下其maven中央倉庫:https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client
可以看到,5.6以上的版本才有。
測試代碼如下:
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 2018/10/7 下午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.創建RestClient對象
restClient = RestClient.builder(esHosts).build();
client = new RestHighLevelClient(restClient);
// 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);
}
// 測試SearchSourceBuilder的搜索
@Test
public void test01() throws Exception {
// 1.創建并設置SearchSourceBuilder對象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 查詢條件--->生成DSL查詢語句
searchSourceBuilder.query(boolQueryBuilder);
// 第幾頁
searchSourceBuilder.from(0);
// 每頁多少條數據
searchSourceBuilder.size(100);
// 獲取的字段(列)和不需要獲取的列
searchSourceBuilder.fetchSource(new String[]{"postdate", "key_word"}, new String[]{});
// 設置排序規則
searchSourceBuilder.sort("postdate", SortOrder.ASC);
// 設置超時時間為2s
searchSourceBuilder.timeout(new TimeValue(2000));
// 2.創建并設置SearchRequest對象
SearchRequest searchRequest = new SearchRequest();
// 設置request要搜索的索引和類型
searchRequest.indices("spnews").types("news");
// 設置SearchSourceBuilder查詢屬性
searchRequest.source(searchSourceBuilder);
// 3.查詢
SearchResponse searchResponse = client.search(searchRequest);
System.out.println(searchResponse.toString());
}
@After
public void after() throws Exception {
restClient.close();
}
}
上面測試案例的查詢條件:
// 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);
實際上會轉化為如下的es查詢語句(可以debug一下,使用searchSourceBuilder就是用來做這種轉換):
{
"from" : 0,
"size" : 100,
"timeout" : "2000ms",
"query" : {
"bool" : {
"must" : [
{
"bool" : {
"should" : [
{
"match_phrase" : {
"key_word" : {
"query" : "廣東",
"slop" : 0,
"boost" : 1.0
}
}
},
{
"match_phrase" : {
"key_word" : {
"query" : "湖人",
"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"
}
}
]
}
注意其差別:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。