【问题标题】:ElasticSearch get only document ids, _id field, using search query on indexElasticSearch 使用索引上的搜索查询仅获取文档 ID、_id 字段
【发布时间】:2017-04-07 12:11:26
【问题描述】:

对于给定的查询,我只想获取 _id 值的列表,而不获取任何其他信息(没有 _source_index_type,...)。

我注意到通过使用_source 并请求不存在的字段,它只会返回最少的数据,但我可以得到更少的数据作为回报吗? 一些答案建议使用响应的hits 部分,但我不想要其他信息。

【问题讨论】:

  • 这个答案可能对你有帮助:stackoverflow.com/questions/33481977/…(提示:使用filter_path
  • 好吧看起来它仍然存在于 ES5 中,这很好
  • 是的,确实,这应该可以解决您的问题。

标签: elasticsearch elasticsearch-2.0


【解决方案1】:

最好使用滚动和扫描来获取结果列表,这样弹性搜索就不必对结果进行排名和排序。

使用 elasticsearch-dsl python 库,这可以通过以下方式完成:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

es = Elasticsearch()
s = Search(using=es, index=ES_INDEX, doc_type=DOC_TYPE)

s = s.fields([])  # only get ids, otherwise `fields` takes a list of field names
ids = [h.meta.id for h in s.scan()]

【讨论】:

【解决方案2】:

我建议对 python 使用 elasticsearch_dsl。他们有一个很好的 api。

from elasticsearch_dsl import Document

# don't return any fields, just the metadata
s = s.source(False)
results = list(s)

之后您可以通过以下方式获取 id:

first_result: Document = results[0]
id: Union[str,int] = first_result.meta.id

这里是获得一些额外信息的官方文档:https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#extra-properties-and-parameters

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2015-03-08
    • 1970-01-01
    • 2017-09-01
    • 2019-01-17
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多