【问题标题】:elasticsearch reducing result to one column - return only 1 value for each documentelasticsearch 将结果减少到一列 - 每个文档仅返回 1 个值
【发布时间】:2021-12-09 15:02:57
【问题描述】:

我尝试将 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中有没有开箱即用的方法来减少结果集?

【问题讨论】:

标签: elasticsearch elasticsearch-dsl


【解决方案1】:

您可以过滤输出 JSON 查看文档:response filtering

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

【讨论】:

  • 这确实有效,直到 _id 与 _source,id 相同但不是我想要的。
  • 是的,根据您的问题 _id 与 id 相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 2020-11-28
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多