美文网首页
解决 Elasticsearch 分页查询记录超过10000时异

解决 Elasticsearch 分页查询记录超过10000时异

作者: lei_charles | 来源:发表于2020-01-07 10:46 被阅读0次
    1. 问题一: 查询结果中 hits.total.value 值最大为10000的限制

      解决方法:
      请求时设置 "track_total_hits": true

      1. Rest 请求设置方法:
      curl -X POST "http://192.168.1.101:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
      {
        "track_total_hits": true
        "query": {
          "match_all": {}
        },
        "sort": [
          {
            "timestamp": {
              "order": "desc"
            }
          }
        ]
      }
      '
      
      1. API 设置方法:
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
      
    2. 问题二: 分页查询 from 大于 10000 时的数据异常

      异常信息

      ...
      Caused by: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [10100]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [10100]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.]];
      ...
      

      解决方法:
      修改 max_result_window 设置的最大索引值,注意以 put 方式提交

      curl -X PUT "http://192.168.1.101:9200/my_index/_settings?pretty" -H 'Content-Type: application/json' -d'
      {
        "index":{
          "max_result_window":1000000
        }
      }
      '
      

    相关文章

      网友评论

          本文标题:解决 Elasticsearch 分页查询记录超过10000时异

          本文链接:https://www.haomeiwen.com/subject/pmmpwctx.html