Source Filtering

Earlier in other articles we’ve set the _sourceflag to false to suppress the documents returned in the response. Although we used it for all-or-nothing scenarios, there are a couple of use cases where we can implement the _source option to tweak the response further. For example, listing below sets _source to [title*, synopsis, rating] so that the results will return the synopsis and rating fields along with all fields with title as the prefix.

GET movies/_search
{
"_source": ["title*","synopsis", "rating"],
"query": {
"match": {
"certificate": "R"
}
}
}

In fact, you can take the _source option even further by setting a list of includes and excludes to further control the return fields. The following listing demonstrates this in action.

GET movies/_search
{
"_source": {
"includes": ["title*","synopsis","genre"],
"excludes": ["title.original"]
},
"query": {
"match": {
"certificate": "R"
}
}
}

The _sourceobject expects two arrays:

  • includes, which consists of all the fields that are expected to be returned in the result
  • excludes, which defines the list of fields that must be excluded from the fields returned from the includes list

In the listing, we expect that all title fields (title and title.original), as well as the synopsis and genre fields are returned from the query. However, we can suppress title.original by including it in the excludes array.

We can play with the includes and excludes arrays to gain a finer control of what fields we can return and which to suppress. For example, if you add an excludes array to the _source object, something like “excludes”: [“synopsis”,”actors”], all fields except synopsis and actors will be returned.