解决 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
      }
    }
    '
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值