Monday, August 18, 2014

how to organize a multi-type elasticsearch query

Suppose your search involves quering a parent type as well as one or more of its child types.
Well, it's pretty obvious that you should use a Bool Query to combine the queries of the different document types.
However, suppose each type involves both a query and a filter. How would you then combine all the queries and filters together?
My first attempt at this was to use a single Filtered Query for the entire query, and each type would contribute to the overall FilteredQuery/query and FilteredQuery/filter.
I later found out this model sometimes produced incorrect search results, was complicated and perhaps not so efficient.
The more logical way to do this, is that each type produces its own Filtered Query, and the top Bool Query mearly joins those filtered queries together. This way, each type has it own independant clause, which results in a simple and clean overall query structure. And the search results are always correct, too.

The writer is R&D team leader at Niloosoft Hunter HRMS

No comments:

Post a Comment