如何在sphinx搜索中根据条件为嵌套模型添加中缀和前缀索引

时间:2017-08-29 11:21:50

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

我有一个多态关联的笔记模型,如下所示

incident has one symptom -> (note model)
incident has many comments -> (note model)

我想使用笔记类型"症状"对笔记进行中缀索引。并希望为类型为"注释"

的注释执行前缀索引

我在Sphinx索引中尝试了以下代码

ThinkingSphinx::Index.define(:incident, DEFAULT_INDEX_OPTIONS.merge(name: "incident_prefix"), &Searchable.beetilable_index('Incident', index_count: incident_index_count, index_id: i) {
      set_property :min_infix_length => 3 
      indexes notes.note, :as => :notes, :source => :query
      notes.where(note_type: "Symptom")
}

ThinkingSphinx::Index.define(:incident, DEFAULT_INDEX_OPTIONS.merge(name: "incident_infix"), &Searchable.beetilable_index('Incident', index_count: incident_index_count, index_id: i) {
      set_property :min_prefix_length => 3 
      indexes notes.note, :as => :notes, :source => :query
      notes.where.not(note_type: "Symptom")

}

上面的代码只是使用INFIX选项进行索引并忽略PREFIX。我想我的情况有问题,有人能让我知道实现这个目标的方法吗?

1 个答案:

答案 0 :(得分:0)

我担心你不能在索引定义中使用Arel / ActiveRecord查询方法来建立在关联之上 - 你只能使用关联和列。

如果您只想要某些类型的注释,那么这里最好的方法是创建应用了这些过滤器的关联,然后引用索引中的关联:

# in the model
has_many :symptom_notes,
  lambda { where(:note_type => "Symptom") },
  :class_name => "Note"

# in the index:
indexes symptom_notes.note, :as => :notes