2014-02-13 104 views
3

我們在彈性搜索中有多個索引並希望在所有索引中搜索數據,但我們希望對不同的索引應用不同的過濾器。Elasticsearch全局搜索多個索引上的不同過濾器

例如:

  • 幾個指標取決於client_id,因此一個client_id過濾器需要
  • 我們is_deleted標誌幾個指標,因此is_deleted過濾器需要

應該怎樣一個在彈性搜索?

此外,我們正在使用高亮功能,這應該給用戶提供建議。但是我們想忽略突出顯示的結果中的某些字段。是否有可能在全球範圍內排除某些領域?

回答

3

這是可能的,使用過濾查詢,嵌套在布爾查詢

這個例子說明了基本設置(注意不同的過濾器是如何被使用):

@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-客戶端的工作,但它應該給一個相當不錯的主意這個概念。

請注意,查詢「meow」會出現兩次,您可能想要使用一個變量來搜索兩個索引中的同一事物。另外,multi_match顯然可以是其他類型的查詢。

+0

謝謝!它幫助! –