在elasticsearch结果数据中排除_id和_index字段

use*_*682 7 full-text-search elasticsearch

如果只是点击api,每个文档中有5个字段.但我只想要这两个字段(user_id和loc_code)所以我在字段列表中提到过.但它仍会返回一些不必要的数据,如_shards,hits,time_out等.

使用以下查询在chrome中的postman插件中发出POST请求

<:9200>/myindex/mytype/_search
{
    "fields" : ["user_id", "loc_code"],
    "query":{"term":{"group_id":"1sd323s"}}
}   
Run Code Online (Sandbox Code Playgroud)

//输出

 {
        "took": 17,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "hits": {
            "total": 323,
            "max_score": 8.402096,
            "hits": [
                {
                    "_index": "myindex",
                    "_type": "mytype",
                    "_id": "<someid>",
                    "_score": 8.402096,
                    "fields": {
                        "user_id": [
                            "<someuserid>"
                        ],
                        "loc_code": [
                            768
                        ]
                    }
                },
               ...
            ]
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我只想要文件字段(两个提到的字段)我想要_id,_index,_type.有没有办法这样做

bra*_*zzi 11

一个可能不完整但有很大帮助的解决方案是使用filter_path. 例如,假设我们的索引中有以下内容:

PUT foods/_doc/_bulk
{ "index" : { "_id" : "1" } }
{ "name" : "chocolate cake", "calories": "too much" }
{ "index" : { "_id" : "2" } }
{ "name" : "lemon pie", "calories": "a lot!"  }
{ "index" : { "_id" : "3" } }
{ "name" : "pizza", "calories": "oh boy..."  }
Run Code Online (Sandbox Code Playgroud)

像这样的搜索...

GET foods/_search
{
  "query": {
    "match_all": {}
  }
}
Run Code Online (Sandbox Code Playgroud)

...将产生大量元数据:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "foods",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "lemon pie",
          "calories" : "a lot!"
        }
      },
      {
        "_index" : "foods",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "chocolate cake",
          "calories" : "too much"
        }
      },
      {
        "_index" : "foods",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "pizza",
          "calories" : "oh boy..."
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

但是如果我们给搜索 URL 参数filter_path=hits.hits._source......

GET foods/_search?filter_path=hits.hits._source
{
  "query": {
    "match_all": {}
  }
}
Run Code Online (Sandbox Code Playgroud)

...它只会返回源(尽管仍然嵌套得很深):

{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "name" : "lemon pie",
          "calories" : "a lot!"
        }
      },
      {
        "_source" : {
          "name" : "chocolate cake",
          "calories" : "too much"
        }
      },
      {
        "_source" : {
          "name" : "pizza",
          "calories" : "oh boy..."
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

您甚至可以过滤字段:

GET foods/_search?filter_path=hits.hits._source.name
{
  "query": {
    "match_all": {}
  }
}
Run Code Online (Sandbox Code Playgroud)

...你会得到这个:

{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "name" : "lemon pie"
        }
      },
      {
        "_source" : {
          "name" : "chocolate cake"
        }
      },
      {
        "_source" : {
          "name" : "pizza"
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您愿意,您还可以做更多事情:只需查看文档即可。


Mat*_*man -2

您也许可以改用 GET api。尝试使用类似的东西:

/myindex/mytype/<objectId>/_source
Run Code Online (Sandbox Code Playgroud)

在你的结果中你只会得到_source。

请参阅:http ://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html

好吧,这假设您知道文档的 id。我不确定使用搜索 api 时是否可以排除元数据。

也许: http ://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-source-filtering.html