如何过滤来自请求正文而不是请求url的elasticsearch响应?

时间:2019-07-08 17:58:46

标签: elasticsearch

我正在使用NEST搜索elasticsearch(v7.1)索引,只需要返回文档的ID。这可以通过在kibana中使用以下请求URI来完成:

请求

GET exampleindex/_search?filter_path=hits.hits._id

响应

{
  "hits" : {
    "hits" : [
      {
        "_id" : "123"
      }
    ]
  }
}

问题

如何在不传递任何查询字符串参数的情况下通过请求正文完成相同的工作?

我希望有这样的东西:

GET exampleindex/_search
{
  "filter_path": "hits.hits._id"
}

但这不起作用:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "Unknown key for a VALUE_STRING in [filter_path].",
        "line": 2,
        "col": 18
      }
    ],
    "type": "parsing_exception",
    "reason": "Unknown key for a VALUE_STRING in [filter_path].",
    "line": 2,
    "col": 18
  },
  "status": 400
}

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您可以使用source filtering

GET /_search
{
    "_source": false
}

答案 1 :(得分:0)

实际上,可以使用 filter_path 参数来减少Elasticsearch返回的响应。因此可以使用

GET exampleindex/_search?filter_path=hits.hits._id

不是请求正文,因为它不支持它,这就是为什么您会出错。

如果像下面那样使用source: false,仍然会得到元数据_index_type_id_score,这些数据必须由elasticsearch返回。

N.B:当前无法从响应中删除这些元数据

GET exampleindex/_search
{
   "query": {
      "match_all": {}
   },
   "_source": false
}

所以。如果您使用任何服务器端语言,例如python / php或其他任何内容,则只能将ID过滤到数组/列表中。例如:

import elasticsearch
es = elasticsearch.Elasticsearch()

res = es.search(
    index=your_index, 
    body={"query": {"match_all": {}}, "size": 10, "fields": ["_id"]})

ids = [d['_id'] for d in res['hits']['hits']]
print(ids) # it will contain only the ids
相关问题