【问题标题】:Counting number of documents in an index in elasticsearch在elasticsearch中计算索引中的文档数
【发布时间】:2018-09-16 09:50:35
【问题描述】:

我正在使用 python ElasticSearch Client 一次执行批量操作来索引 100 个文档。我想计算索引中的文档总数。所以我做批量操作,然后计算索引中的文档数如下:

helpers.bulk(es_client, actions);
es_client.count('index').get('count')

但是第二行仍然返回旧计数,我尝试从另一个文件运行第二行,它返回正确的结果。我怀疑批量操作尚未完成。如果我错了,请纠正我,什么是解决方法来做我想做的事?

【问题讨论】:

  • 显然,添加批量操作的文档尚未考虑该索引刷新索引,然后才能解决问题。

标签: python python-3.x python-2.7 elasticsearch


【解决方案1】:

在python中获取索引文档数

es.indices.refresh(index_name)
es.cat.count(index_name, params={"format": "json"})

【讨论】:

    【解决方案2】:

    你必须使用refresh API:

    POST /index/_refresh

    刷新 API 允许显式刷新一个或多个索引,使自上次刷新以来执行的所有操作都可用于搜索。 (接近)实时功能取决于所使用的索引引擎。例如,内部需要调用刷新,但默认情况下会定期安排刷新。

    更多信息请查看https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html

    【讨论】:

      【解决方案3】:

      我在下面附上的示例中发现了这一点

      es=Elasticsearch([{'host':'url','port':'9200','timeout':60}]) 
      res = es.count(index='your index', doc_type='your doc_type', body={'query': your query })["count"]
      

      如果对你有帮助,这里有一些很好的 Python 计数示例:

      https://python.hotexamples.com/examples/elasticsearch/Elasticsearch/count/python-elasticsearch-count-method-examples.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-18
        • 2021-07-19
        • 1970-01-01
        • 1970-01-01
        • 2020-02-12
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        相关资源
        最近更新 更多