【问题标题】:Elastic Search - exclude index and type from json response弹性搜索 - 从 json 响应中排除索引和类型
【发布时间】:2015-07-22 16:54:27
【问题描述】:

当我对这样的索引执行查询时:

{
   "_source":["bar"] , "size":100,
   "query": {
       "match_all": {}
   },
   "filter": {
       "type" : {
           "value" : "foo"
        }
    }
}

响应包括索引、类型等。但我已经知道索引和类型,因为我指定了它。这些信息只会增加 json 数据的大小。有没有办法从响应中排除这些?

这是我得到的:

{
"took": 31,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
"hits": {
    "total": 364024,
    "max_score": 1,
    "hits": [
          {
        "_index": "foo_bar",
        "_type": "foo",
        "_id": "asdjj123123",
        "_score": 1,
        "_source": {
          "bar": "blablablabla"
    }
  }
,...

我想要的是这样的,所以没有type,score,index的回复:

{
"took": 31,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
"hits": {
    "total": 364024,
    "max_score": 1,
    "hits": [
          {
        "_id": "asdjj123123",
        "_source": {
          "bar": "blablablabla"
    }
  }
,...

【问题讨论】:

    标签: json elasticsearch


    【解决方案1】:

    是的,从 ES 1.6 开始,您可以使用 response filtering 并在查询中使用 filter_path 参数仅在响应中枚举您需要的内容:

    curl -XGET 'localhost:9200/foo_bar/foo/_search?pretty&filter_path=hits.total,hits.max_score,hits.hits._id,hits.hits._source'
    

    【讨论】:

    • 如何在帖子正文中包含 filter_path 而不是作为 URL 中的获取?
    • 您不必这样做,您可以为您的查询指定正文,并且仍然在 URL 中提供 filter_path
    【解决方案2】:

    _source 选项可用于实现此目的。

    当您查询 JSON 并且不想使用 filter path 时,您可以执行以下操作

    {
      "_source":{
         "includes":["field1", "field2"],
         "excludes":["_index", "_type", "_score"]
      }
    }
    

    您只能定义includes、只能定义excludes 或两者。响应会相应地起作用。

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 2018-03-27
      • 2015-05-23
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多