2012-08-04 90 views
-1

复杂的搜索我想要做的与思维狮身人面像一个复杂的搜索:与思考狮身人面像

搜索哪些用户: - >住在城市(city_id属性) - >或具有hability移动到一个城市(mobile_cities association) - >或者居住在距经纬度点最大距离处,每个用户的最大距离是不同的,并且设置在mobility_distance属性中。

现在我这样做,与3型动物进行搜索,我volontary设置一个大per_page号码,然后我合并单个阵列上的3个结果,一则分页此阵:

#users living in the @city 
search_set_living = search_set.merge({:city_id => @city.id }) 
users_living = User.search :with => search_set_living.dup, 
           :page => 1, :per_page => 1000 

#users declaring hability to move to the @city 
search_set_mobile = search_set.merge({:mobile_cities_ids => @city.id }) 
users_mobile = User.search :with => search_set_mobile.dup, :page => 1, :per_page => 1000 

#users living at a maximum distance from the origin point(custom distance for each user, max 30km) 
search_set_around = search_set.merge({"@geodist" => 0.0..30_000.0}) 
users_around = User.search :geo => [@search_latitude * Math::PI/180 , @search_longitude * Math::PI/180], 
            :with => search_set_around.dup, 
            :page => 1, :per_page => 1000 
users_around_filtered = users_around.dup.delete_if{|user| (user.mobility_distance * 1000)< user.sphinx_attributes['@geodist'] } 


#merge the 3 results in a array 
all_users = (users_mobile.flatten + users_around_filtered.flatten).uniq 

#look for facets and paginate the array 
@facets = User.facets :with => {:user_id => all_users.map(&:id)} 
@users_to_display = all_users.paginate(:page => params[:page], :per_page => 10) 

这是工作的罚款但我不满意: - 性能都不太好, -I要作为排序依据这样的多重属性的能力:为了=>“created_at DESC,@relevance DESC”

我想做的确切相同的搜索,但在一个狮身人面像的搜索。 我知道我应该使用"OR Logic with Attribute Filters" from the docs,但我不知道如何将它与一个geo_search调用... 我真的不知道该怎么做, 你们能帮我吗?

非常感谢,

回答

-1

太棒了!非常感谢。我只是需要纠正一点你的语法(缺少一些括号)以使其工作。 我不得不添加per_page和页面参数,不知道为什么。

logic = ["city_id = #{@city.id}", 
     "IN(mobile_cities_ids, #{@city.id})", 
     "GEODIST(latitude, longitude, #{@search_latitude * Math::PI/180}, #{@search_longitude * Math::PI/180}) < (mobility_distance * 1000)"] 

search_set_logic = search_set.merge({:valid => true}) 
@users_to_display = User.search :sphinx_select => "*, (#{logic.join(" OR ")}) AS valid", 
       :with => search_set_logic.dup, 
       :sort_mode => :extended, 
       :order => "visibility DESC, last_login_at DESC", 
       :page => params[:page], :per_page => 10 
3

:sphinx_select选项肯定是在这里你的朋友,因为你已经猜到。让我们点点拼凑一起位:

logic = [ 
    "city_id = #{@city.id}", 
    "IN(mobile_cities_ids, #{@city.id}", 
    "GEODIST(lat, lng, #{lat}, #{lng}) < (mobility_distance * 1000)" 
] 

User.search :sphinx_select => "*, #{logic.join(" OR ")}) AS valid", 
    :with => {:valid => true} 

添加分页,只要你喜欢,如果需要的话(也许你的纬度/经度属性被命名为别的东西)调整的属性名称。我不认为你需要围绕该文档中的自定义属性调用IF,但如果事情在他们应该做的时候无法正常工作,那么可以试试看。在方面的调用中也应该很好。

+0

太棒了!我只需要修改一下你的语法就可以使它工作: – alex 2012-08-05 15:12:01