2012-08-04 141 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