How to add infix and prefix indexes for nested model based on condition in sphinx search

102 Views Asked by At

I have a note model with polymorphic association which is like below

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

and

I would like to do the infix indexing for the note with the note type "symptom" and want to do the prefix indexing for the note with type "comment"

I've tried the below code in Sphinx index

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")

}

the above code is just doing the indexing with INFIX option and ignores the PREFIX one. I guess something wrong with my where condition, can someone let me know the way to achieve this?

1

There are 1 best solutions below

0
On

I'm afraid you can't use Arel/ActiveRecord querying methods within the index definition to build on top of associations - you can only use associations and columns.

If you only want notes of certain types, then the best approach here is to create associations that have those filters applied, and then reference the association in your index:

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

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