Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search - exclude index and type from json response

When I execute a query over an index like this:

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

the response includes index, type, etc. But I already know the index and type because I specified it. This information just bloats up the size of the json data. Is there a way to exclude these from the response?

This is what I get:

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

What I want is something like this, so a response without 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"
    }
  }
,...
like image 464
benbo Avatar asked Jul 22 '15 16:07

benbo


People also ask

What is _source in Elasticsearch query?

The _source field contains the original JSON document body that was passed at index time. The _source field itself is not indexed (and thus is not searchable), but it is stored so that it can be returned when executing fetch requests, like get or search.

How do I make only one field in Elasticsearch?

There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.

How do I get all Elasticsearch index data?

You can use cURL in a UNIX terminal or Windows command prompt, the Kibana Console UI, or any one of the various low-level clients available to make an API call to get all of the documents in an Elasticsearch index. All of these methods use a variation of the GET request to search the index.


2 Answers

Yes, as of ES 1.6, you can use response filtering and using the filter_path parameter in the query enumerate only what you need in the response:

curl -XGET 'localhost:9200/foo_bar/foo/_search?pretty&filter_path=hits.total,hits.max_score,hits.hits._id,hits.hits._source'
like image 56
Val Avatar answered Oct 13 '22 12:10

Val


_source option can be used to achieve this.

When you query a JSON and you don't want to use filter path, you can do as below

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

You can define only includes, only excludes or Both. Response works accordingly.

like image 20
Gibbs Avatar answered Oct 13 '22 14:10

Gibbs