2013-04-22 125 views
0

我使用的輪胎索引和搜索汽車的數據庫:如果你搜索「紐約喬眼下彈性搜索多個字段

s = Tire.search Search.index_name do 

     query do |query| 
     query.text :_all, downcased_query, :type => :phrase 
     end 

    end 

create :mappings => { 
    :cars => { 
     :properties => { 
      :id => {:type => 'string', :index => 'not_analyzed', :include_in_all => true}, 
      :user_id => {:type => 'string', :index => 'not_analyzed'}, 
      :name => {:type => 'string', :analyzer => 'snowball' }, 
      :time => {:type => 'long', :index => 'not_analyzed'}, 
      :origin_country => {:type => 'string', :analyzer => 'keyword', :include_in_all => false}, 
      :origin_state => {:type => 'string', :analyzer => 'keyword', :include_in_all => false}, 
      :origin_city => {:type => 'string', :analyzer => 'keyword', :include_in_all => false}, 
      :dealer_name => {:type => 'string', :analyzer => 'snowball', :boost => 1.5, :include_in_all => false} 
     } 
    } 
} 

和查詢的樣子經銷商「,如果dealer_name是」Joe Dealer「,或者origin_state是」紐約「,但不是兩者,則它將匹配。有沒有辦法匹配origin_state是「紐約」,「dealer_name」是「Joe Dealer」(或者其中之一)?這個想法是我想要一個用戶輸入一個fre-form查詢字符串,並且能夠找到可能的最佳匹配,並且能夠找到他們希望進入紐約Joe Dealer的用戶,並找到在紐約Joe Dealer出售的所有汽車(或紐約的所有汽車或喬經銷商排名較低)。

回答

0

將溶液轉移到一個「匹配」類型的過濾器,和魔鬼:類型=>:短語,所以:

query do |query| 
    query.match :_all, downcased_query 
end 

仍然有一些問題與排序,使得時間仍然是一個大的輸入,但是這解決了許多問題。

2

正確的解決方案是使用multi_match查詢對多個字段執行相同的查詢。它在Tire中受到支持,請參閱https://github.com/karmi/tire/blob/master/test/integration/match_query_test.rb

Tire.search Search.index_name do 
    query do |query| 
    query.match :dealer_name, downcased_query 
    query.match :origin_state, downcased_query 
    end 
end 

你甚至可以使用phrase類型,如果查詢將同時包含在正確的順序經銷商和狀態信息,如「紐約喬經銷商」。