2013-04-22 122 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类型,如果查询将同时包含在正确的顺序经销商和状态信息,如“纽约乔经销商”。