2

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I find "size": 0 also works like search_type=count.
How about changing the structure? Something like: "fields":["fieldName1":{"fieldName2", etc...}] Is this possible?
You won't be able to do directly in ElasticSearch : this have to be done on client side.
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.