Elastic Search7限制查询10000条文档的解决方案

袁志蒙 1841次浏览

摘要:elasticsearch升级到7.X后,与之前版本修改的细节还挺多的,首先就是返回文档的total字段格式调整,然后就是当文档数大于10000时,只显示10000条数据...

elasticsearch升级到7.X后,与之前版本修改的细节还挺多的,首先就是返回文档的total字段格式调整,然后就是当文档数大于10000时,只显示10000,这使我们在获取文档的真实数据时变的麻烦了一些。

解决total最大为10000方案,在查询中添加: track_total_hits:true

例如:

$params = [
    'index' => $indice,
    'body' => [
        'query' => [
            'bool' => [
                'must' => $must,
                'should' => $should,
                'must_not' => $must_not
            ]
        ],
        'sort' => [ 
            'id' =>['order'=>'asc']
        ]
    ],
    'track_total_hits' => true, //elasticsearch7 显示真实数据总数
    'from' => 0,
    'size' => 10
];


此时可以显示文档的真实数了,但是在分页查询的时候,大于 10000 条还是报错,错误如下:

Result window is too large, from + size must be less than or equal to: [10000] but was [1278620]. 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.

原因分析:

es对 from + size 的大小进行限制,必须小于等于10000。

解决方案:
在业务中限制分页大小,使 from+size<=10000; 动态更改索引设置,为 max_result_window 参数赋值足够大的值,因为es的优势在于搜索,不在于分页,对此做限制就是为不影响其性。就es的默认配置,假设每页10条记录,也有1000页,如果业务上实在不妥协,则可以设置修改其最大值:

PUT index/_settings
{
  "index":{
    "max_result_window":100000000
  }
}


随机内容

表情

共0条评论
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~