Exclude _id and _index field in elasticsearch result data

Each document has 5 fields, if you just click api. but I want only these two fields (user_id and loc_code), so I mentioned in the list of fields. but still it returns some unnecessary data like _shards, hits, time_out, etc.

executing a POST request in a postman plugin in chrome using the following request

<:9200>/myindex/mytype/_search { "fields" : ["user_id", "loc_code"], "query":{"term":{"group_id":"1sd323s"}} } 

//output

  { "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 ] } }, ... ] } } 

but I only want document fields (the two fields mentioned), nor do I want _id, _index, _type. is there any way to do this

+7
full-text-search elasticsearch
source share
2 answers

A solution that may be incomplete but helps a lot is the user filter_path . For example, suppose we have the following content in an index:

 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..." } 

Such a search ...

 GET foods/_search { "query": { "match_all": {} } } 

... will give a lot of metadata:

 { "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..." } } ] } } 

But if we give the search URL the filter_path=hits.hits._score ...

 GET foods/_search?filter_path=hits.hits._source { "query": { "match_all": {} } } 

... it will return only the source (although still deeply nested):

 { "hits" : { "hits" : [ { "_source" : { "name" : "lemon pie", "calories" : "a lot!" } }, { "_source" : { "name" : "chocolate cake", "calories" : "too much" } }, { "_source" : { "name" : "pizza", "calories" : "oh boy..." } } ] } } 

You can even filter the fields:

 GET foods/_search?filter_path=hits.hits._source { "query": { "match_all": {} } } 

... and you will get this:

 { "hits" : { "hits" : [ { "_source" : { "name" : "lemon pie" } }, { "_source" : { "name" : "chocolate cake" } }, { "_source" : { "name" : "pizza" } } ] } } 

And you can do much more if you want, just look at the documentation .

0
source share

You can use the get api instead. Try something like:

 /myindex/mytype/<objectId>/_source 

In your results you will receive only the source.

See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html

Well, this assumes you know the document id. I'm not sure if you can exclude metadata when using search api.

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

-2
source share

All Articles