Elasticsearch global search for a different filter across multiple indexes

We have several indexes in Elastic Search and you want to search data for all indexes, but we want to apply different filters for different indexes.

For instance:

  • multiple indices are dependent on client_id, so a filter is requiredclient_id
  • we have a flag is_deletedin several indices, so a is_deletedfilter is required

How to approach this in Elastic Search?

In addition, we use the highlight function, which should give suggestions to users. But we would like to ignore certain fields in the highlighted results. Can certain fields be excluded globally?

+3
source share
1 answer

, , .

( , ):

 @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' } }
             ]
           }
         }
       }
     ]
   }
 })

ES-, .

, "" , , . , multi_match - .

+3

All Articles