aamirl
aamirl

Reputation: 1550

Elasticsearch: Exclude non-matching nested objects in results

In Elasticsearch, is there any way to exclude the nested objects that don't match a particular query/filter from the resulting _source?

For example, let's say that a document has four objects in a nested field. Querying on the required filters results in only matching objects 1 and 3. When we get the results via _source, we will pull back the entire document along with objects 1,2,3,4.

Is it possible to exclude objects 2 and 4 from the results? Or is that something that we have to re-iterate and exclude using application-side logic?

Upvotes: 4

Views: 3145

Answers (2)

Jay Shah
Jay Shah

Reputation: 3771

You can achieve this with use of inner_hits which will return you only matching nested objects. you can exclude this nested field in source.

Suggested by Val at: ElasticSearch - Get only matching nested objects with All Top level fields in search response

Upvotes: 4

keety
keety

Reputation: 17441

At the moment there is no way to include only the matched nested objects in the result.

There is a inner_hits feature coming out in elasticsearch 1.5.0 which should help with this.

Upvotes: 3

Related Questions