中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Elasticsearch:RestClient+SearchSourceBuilder使用案例

發布時間:2020-06-16 09:30:01 來源:網絡 閱讀:8762 作者:xpleaf 欄目:大數據

1 前言

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以上的版本才有。

2 測試案例

測試代碼如下:

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();
    }

}

3 分析

3.1 Rest Json

上面測試案例的查詢條件:

// 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"
      }
    }
  ]
}

3.2 match query VS match_phrase query

注意其差別:

  • match query:會對查詢語句進行分詞,分詞后查詢語句中的任何一個詞項被匹配,文檔就會被搜索到。如果想查詢匹配所有關鍵詞的文檔,可以用and操作符連接;
  • match_phrase query:滿足下面兩個條件才會被搜索到
    • (1)分詞后所有詞項都要出現在該字段中
    • (2)字段中的詞項順序要一致
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

肥乡县| 融水| 塔河县| 庆安县| 宜昌市| 汝南县| 湖口县| 贡觉县| 庐江县| 曲松县| 章丘市| 彩票| 永泰县| 丰台区| 樟树市| 卢龙县| 炎陵县| 宜宾县| 礼泉县| 四子王旗| 称多县| 兴城市| 土默特右旗| 镇平县| 清镇市| 德令哈市| 定日县| 凤山市| 紫阳县| 石屏县| 周口市| 安图县| 汽车| 衡东县| 青浦区| 抚宁县| 榕江县| 宝应县| 望城县| 元江| 图们市|