zoukankan      html  css  js  c++  java
  • Elasticsearch7.4 spring boot 使用 RestHighLevelClient实现searchAfter分页

    官网searchafter介绍:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/paginate-search-results.html#search-after

    官网介绍的查询返回如下:

    {
      "pit_id" : "46ToAwEPbXktaW5kZXgtMDAwMDAxFnVzaTVuenpUVGQ2TFNheUxVUG5LVVEAFldicVdzOFFtVHZTZDFoWWowTGkwS0EAAAAAAAAAAAQURzZzcUszUUJ5U1NMX3Jyak5ET0wBFnVzaTVuenpUVGQ2TFNheUxVUG5LVVEAAA==", 
      "took" : 17,
      "timed_out" : false,
      "_shards" : ...,
      "hits" : {
        "total" : ...,
        "max_score" : null,
        "hits" : [
          ...
          {
            "_index" : "my-index-000001",
            "_id" : "FaslK3QBySSL_rrj9zM5",
            "_score" : null,
            "_source" : ...,
            "sort" : [                                
              4098435132000,
              "FaslK3QBySSL_rrj9zM5"
            ]
          }
        ]
      }
    }

    其中,sort数组长度为2.

    但在实现测试中发现7.4版本es查询返回时,长度为一

    {
      "took" : 191,
      "timed_out" : false,
      "_shards" : {
        "total" : 2,
        "successful" : 2,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 10000,
          "relation" : "gte"
        },
        "max_score" : null,
        "hits" : [
          {
            "_index" : "atc_drg_stat",
            "_type" : "man",
            "_id" : "1000",
            "_score" : null,
            "_source" : {
              "district_code" : "DistrictCode",
              ---------------
            },
            "sort" : [
              1000
            ]

    不知道是否是版本变化引起的。

    POM文件如下:

    <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>7.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>7.3.2</version>
            </dependency>

    代码如下:

    @RequestMapping("/searchAfter")
        public void searchAfter() throws IOException {
            String searchAfter =null;
            Object[] objects = new Object[]{};
            try {
                for (int i = 0; i < 100; i++) {
                    SearchRequest searchRequest = new SearchRequest(index);
                    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
                    searchSourceBuilder.query(matchQuery("year_month", "yearMonth"));
                    searchSourceBuilder.size(1000);
                    searchSourceBuilder.sort("sort_id", SortOrder.ASC);
                    if(objects.length>0) {
                        searchSourceBuilder.searchAfter(objects);
                    }
                    searchRequest.source(searchSourceBuilder);
                    System.out.println(searchRequest.source().toString());
                    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
                    System.out.println(searchResponse);
                    SearchHit[] hits = searchResponse.getHits().getHits();
                    objects = hits[hits.length-1].getSortValues();
    
                    System.out.println(hits);
                }
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    云? 云! 晕! 云计算适合创业公司么?
    批量转移Windows Server的DNS设置
    Signs that you are a bad programmer
    IsNull Function in PeopleSoft MetaSQL
    JS绘制曲线图
    Why does my shared clipboard not work?
    C#入门详解(2)
    C#入门详解(1)
    分享范玮琪最初的梦想
    像战士一样生活
  • 原文地址:https://www.cnblogs.com/hankuikui/p/13711095.html
Copyright © 2011-2022 走看看