jasonz
jasonz

Reputation: 1345

Change the structure of ElasticSearch response json

In some cases, I don't need all of the fields in response json.

For example,

// request json
{
  "_source": "false",
  "aggs": { ... },
  "query": { ... }
}

// response json
{
  "took": 123,
  "timed_out": false,
  "_shards": { ... },
  "hits": {
    "total": 123,
    "max_score": 123,
    "hits": [
      {
        "_index": "foo",
        "_type": "bar",
        "_id": "123",
        "_score": 123
      }
    ],
    ...
  },
  "aggregations": {
    "foo": {
      "buckets": [
        {
          "key": 123,
          "doc_count": 123
        },
        ...
      ]
    }
  }
}

Actually I don't need the _index/_type every time. When I do aggregations, I don't need hits block.

"_source" : false or "_source": { "exclude": [ "foobar" ] } can help ignore/exclude the _source fields in hits block.

But can I change the structure of ES response json in a more common way? Thanks.

Upvotes: 2

Views: 7612

Answers (2)

4levels
4levels

Reputation: 3234

I recently needed to "slim down" the Elasticsearch response as it was well over 1MB in json and I started using the filter_path request variable. This allows to include or exclude specific fields and can have different types of wildcards. Do read the docs in the link above as there is quite some info there.

eg.

_search?filter_path=aggregations.**.hits._source,aggregations.**.key,aggregations.**.doc_count

This reduced (in my case) the response size by half without significantly increasing the search duration, so well worth the effort..

Upvotes: 2

ThomasC
ThomasC

Reputation: 8165

In the hits section, you will always jave _index, _type and _id fields. If you want to retrieve only some specific fields in your search results, you can use fields parameter in the root object :

{
  "query": { ... },
  "aggs": { ... },
  "fields":["fieldName1","fieldName2", etc...]
}

When doing aggregations, you can use the search_type (documentation) parameter with count value like this :

GET index/type/_search?search_type=count

It won't return any document but only the result count, and your aggregations will be computed in the exact same way.

Upvotes: 2

Related Questions