Make elasticsearch only return certain fields? Make elasticsearch only return certain fields? elasticsearch elasticsearch

Make elasticsearch only return certain fields?


Yep, Use a better option source filter. If you're searching with JSON it'll look something like this:

{    "_source": ["user", "message", ...],    "query": ...,    "size": ...}

In ES 2.4 and earlier, you could also use the fields option to the search API:

{    "fields": ["user", "message", ...],    "query": ...,    "size": ...}

This is deprecated in ES 5+. And source filters are more powerful anyway!


I found the docs for the get api to be helpful - especially the two sections, Source filtering and Fields: https://www.elastic.co/guide/en/elasticsearch/reference/7.3/docs-get.html#get-source-filtering

They state about source filtering:

If you only need one or two fields from the complete _source, you can use the _source_include & _source_exclude parameters to include or filter out that parts you need. This can be especially helpful with large documents where partial retrieval can save on network overhead

Which fitted my use case perfectly. I ended up simply filtering the source like so (using the shorthand):

{    "_source": ["field_x", ..., "field_y"],    "query": {              ...    }}

FYI, they state in the docs about the fields parameter:

The get operation allows specifying a set of stored fields that will be returned by passing the fields parameter.

It seems to cater for fields that have been specifically stored, where it places each field in an array. If the specified fields haven't been stored it will fetch each one from the _source, which could result in 'slower' retrievals. I also had trouble trying to get it to return fields of type object.

So in summary, you have two options, either though source filtering or [stored] fields.


For the ES versions 5.X and above you can a ES query something like this:

    GET /.../...    {      "_source": {        "includes": [ "FIELD1", "FIELD2", "FIELD3" ... " ]      },      .      .      .      .    }