如何从elasticsearch索引中检索所有文档ID

5w9g7ksd  于 7个月前  发布在  ElasticSearch
关注(0)|答案(3)|浏览(192)

如何从Elasticsearch索引中检索所有文档ID(内部文档'_id')?如果我在该索引中有2000万个文档,最好的方法是什么?

s4n0splo

s4n0splo1#

我只需要导出整个索引并读取文件系统。当处理数百万的查询结果集时,我使用size/from和scan/scroll的经验是灾难性的。只是需要太长时间。
如果你可以使用像knapsack这样的工具,你可以将索引导出到文件系统,然后遍历目录。每个文档都存储在它自己的以_id命名的目录下。不需要真正打开文件。只需遍历目录。
背包链接:https://github.com/jprante/elasticsearch-knapsack
编辑:希望你不是经常这样做......或者这可能不是一个可行的解决方案

nle07wnf

nle07wnf2#

对于这么多的文档,您可能希望使用scan and scroll API
许多客户端库都有现成的帮助器来使用该接口。例如,使用elasticsearch-py,您可以执行以下操作:

es = elasticsearch.Elasticsearch(eshost)
scroll = elasticsearch.helpers.scan(es, query='{"fields": "_id"}', index=idxname, scroll='10s')
for res in scroll:
        print res['_id']

字符串

6tr1vspr

6tr1vspr3#

首先,您可以发出请求以获取索引中记录的完整计数。

curl -X GET 'http://localhost:9200/documents/document/_count?pretty=true'

{
  "count" : 1408,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  }
}

字符串
然后,您需要使用sizefrom参数的组合来遍历集合,直到达到总计数。传递空的field参数将仅返回您感兴趣的索引和_id。
找到一个合适的page大小,您可以在不耗尽内存的情况下使用它,并在每次迭代时递增from

curl -X GET 'http://localhost:9200/documents/document/_search?fields=&size=1000&from=5000'


项目响应示例:

{
  "_index" : "documents",
  "_type" : "document",
  "_id" : "1341",
  "_score" : 1.0
},
...

相关问题