【问题标题】:How to add infix and prefix indexes for nested model based on condition in sphinx search如何在 sphinx 搜索中根据条件为嵌套模型添加中缀和前缀索引
【发布时间】:2017-08-29 11:21:49
【问题描述】:

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

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 选项。我猜我的 where 条件有问题,有人可以告诉我实现这一点的方法吗?

【问题讨论】:

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


    【解决方案1】:

    恐怕您不能在索引定义中使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      相关资源
      最近更新 更多