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

  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
      }
    }
    '
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容