Elasticsearch global search different filter on multiple indexes Elasticsearch global search different filter on multiple indexes elasticsearch elasticsearch

Elasticsearch global search different filter on multiple indexes


That's possible, using a filtered queries, nested within a boolean query.

This example illustrates the basic setup (notice how different filters are used):

 @results = elastic_client.search([:dogs, :cats], {   :bool => {     :should => [       # cats       {         :filtered => {           :query => {             :multi_match => {               :query => 'meow', # repeated, replace with a variable               :type => 'phrase_prefix',               :fields => ['name', 'age']             }           },           :filter => {             :and => [               { :term => { :owner_id => '123' } },               { :type => { :value => 'cat' } }             ]           }         }       },       # dogs       {         :filtered => {           :query => {             :multi_match => {               :query => 'meow', # repeated, replace with a variable               :type => 'phrase_prefix',               :fields => ['name', 'color']             }           },           :filter => {             :and => [               { :term => { :kennel_id => '456' } },               { :type => { :value => 'dog' } }             ]           }         }       }     ]   } })

This particular code may or may not work with your ES-client, but it should give a fairly good idea of the concept.

Note that the query "meow" occurs twice, and you may want to use a variable instead, to search for the same thing in the two indices. Also, multi_match could be some other type of query obviously.