Loading

python3 Elasticsearch8.2返回值数量超过10000条解决方案

今天测试写的ES查询接口发现total值最大为10000,找了好久。之前使用Elasticsearch 6.4就没有这个问题
原来从 Elasticsearch 7.0之后,为了提高搜索的性能,在 hits 字段中返回的文档数有时不是最精确的数值。Elasticsearch 限制了最多的数值为10000。

解决方案:(使用的第三种方案解决)
1、第一种解决方案
在restful请求时,解除索引最大查询数的限制

PUT _all/_settings
{
  "index.max_result_window": 200000
}

_all表示所有索引,如果针对单个索引的话修改成索引名称即可!

2、第二种解决方案
在创建索引的时候加上

{
  "settings": {
    "index": {
      "max_result_window": 10000000000
    }
  }
}

但是修改完之后,通过api查询回来的totalhits还是只有10000条?

3、第三种解决方案

在查询时候把 track_total_hits 设置为true。

track_total_hits 设置为false禁用跟踪匹配查询的总点击次数

设置为true就会返回真实的命中条数。

python代码在构建条件时候加上:

from elasticsearch_dsl import Search, Q
s = Search()
s = s.extra(track_total_hits=True)
s = s.to_dict()

rest请求查询时:

GET 索引名/_search
{
    "query": {
        "bool": {}
    },
    "track_total_hits": true,
    "from": 0,
    "size": 0
}



https://cloud.tencent.com/developer/article/1735489
https://github.com/elastic/elasticsearch-dsl-py/issues/1183
【项目经验】解决ES查询只能查一万条数据问题
https://blog.csdn.net/qq_43410878/article/details/123991666?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1-123991666-blog-120501946.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1-123991666-blog-120501946.pc_relevant_multi_platform_whitelistv3&utm_relevant_index=1

elasticsearch 连接
https://www.cnblogs.com/bubu99/p/13580687.html
Elasticsearch - 使用Python批量写入数据
https://www.cnblogs.com/Neeo/articles/10788573.html

posted @ 2022-10-25 17:31  就学45分钟  阅读(838)  评论(0编辑  收藏  举报