2017-08-29 48 views
0

我有一個多態關聯一張紙條模型,就像下面如何基於在獅身人面像搜索

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

我願做中綴條件加綴和前綴索引嵌套模型索引與筆記型「症狀」的字條,並希望做前綴索引與類型的紀錄「註釋」

我試過在獅身人面像索引下面的代碼

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之一。我猜我的病情有問題,有人能讓我知道如何實現這一目標嗎?

回答

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