思考Sphinx排序顺序没有按预期工作。

时间:2014-09-03 19:05:31

标签: ruby-on-rails-3 thinking-sphinx

我在连接(中间)表中有一列。我想搜索位置

的列

这是我的索引文件

 ThinkingSphinx::Index.define :restaurant, :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do

   indexes schools.school_name, :sortable => true, :as => :school_name
   indexes schools.branch_name, :sortable => true, :as => :branch_name
   indexes contact_info.restaurant_name, :sortable => true, :as => :restaurant_name
   indexes delivery_info.delivery_charges, :as => :delivery_charges

   indexes restaurant_schools.position, :as => :restaurant_position, :sortable => true

   has restaurant_info.is_pick_up, :as => :pick_up, :facet => true
   has delivery_info.is_delivery,  :as => :delivery, :facet => true
   has schools.id, :as => :school_id, :facet => true

   has restaurant_categories.id, :as => :restaurant_category_ids, :facet => true
   has restaurant_info.min_order, :as => :min_order, :type => :float

   has avg_rating, :as => :rating, :type => :integer
   has ranking, :as => :ranking, :type => :integer

   has delivery_info.delivery_estimated_time, :as => :delivery_eta, :type => :integer

   set_property :min_infix_len => 1
 end

当我查询时:

 @restaurants = Restaurant.search :with => {:school_id =>  school_ids }
 @restaurants = @restaurants.search :order => :restaurant_position

它没有给我记录,因为它应该是。

请建议我应该如何解决,以获得准确的结果。

1 个答案:

答案 0 :(得分:1)

这里有几点需要注意。

首先:您的restaurant_position字段是多个值的集合(如果restaurant_schoolshas_manyhas_and_belongs_to_many关联,复数名称表示该关联。这意味着它实际上最终是一串连接在一起的值(以空格字符作为分隔符)。例如“1 4 7 2”

其次:即使您将其更改为属性,它也将是一个多值属性。对多值属性进行排序没有意义(如果使用所有值的平均值?最小?最大?总和?)。

最后:也许您希望您提供的过滤器(在school_id上)链接到排序逻辑? Sphinx不是关系数据库,也没有哈希/词典的概念,因此每个restaurant_position值与他们可能在您的数据库中关联的相应学校之间没有任何关联。

如果你想要那个连接,那么最好在模型上有一个Sphinx索引,它有一个学校和一个位置(我猜是RestaurantSchool)。从那里,您可以通过关联提取相关的字段和属性,但您也可以使用以下内容进行搜索/排序:

RestaurantSchool.search :with => {:school_id => school_ids}, :order => :position