首页 > 解决方案 > elasticsearch 将结果减少到一列 - 每个文档仅返回 1 个值

问题描述

我尝试将 elasticsearch 的 json 结果减少到仅我建议获取的一列或多列。有什么办法吗?

当我使用以下命令时,我将结果嵌套到“_source”中:

 {
 "from": "0", "size":"2",
 "_source":["id"],
 "query": {                           
  "match_all": {}
  }
 }
 '

并且不需要我的用例。

我得到这个结果:

  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "indexer_1",
        "_type" : "type_indexer_1",
        "_id" : "38142",
        "_score" : 1.0,
        "_source" : {
          "id" : 38142
        }
      },
      {
        "_index" : "indexer_1",
        "_type" : "type_indexer_1",
        "_id" : "38147",
        "_score" : 1.0,
        "_source" : {
          "id" : 38147
        }
      }
    ]
  }
}

我想拥有的:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "id" : 38142
      },
      {
        "id" : 38147
      }
    ]
  }
}

我会喜欢这个 json 结果:

{
  {
    "id" : 38142
  },
  {
    "id" : 38147
  }
}

ES中是否有开箱即用的方法来减少结果集?

标签: elasticsearchelasticsearch-dsl

解决方案


您可以过滤输出 JSON 查看文档:响应过滤

GET /index/_search?filter_path=hits.hits._id
 {
 "from": "0", 
 "size":"2",
 "_source":["id"],
 "query": {                           
  "match_all": {}
  }
 }

推荐阅读