-->

Thinking sphinx search returns nothing when multip

2019-08-27 16:57发布

问题:

Searching sometimes yields no results when my sphinx indices are separated into multiple files in one model.

The versions I'm using:

  • Rails - 4.1
  • Thinking Sphinx - 3.0.6
  • Sphinx - 2.0.9

I have five indices on this model:

ThinkingSphinx::Index.define :incident, name: "incident_index_1" ... do
  indexes name
end

ThinkingSphinx::Index.define :incident, name: "incident_index_5" ... do
  indexes tags.name, as: :tag      
  indexes custom_fields_values.value, as: :custom
end

Searching separately, the queries return correct results:

Incident.search(conditions: { custom: "dd" })
Incident.search("some string")

However, combining the field-specific query with the generic query sometimes returns nothing:

Incident.search("some string", conditions: { custom: "dd" })

If "some string" is in the tag field (which is defined in the same index file), it works. If it is in the name field (which is defined in a different index file), it doesn't work.

回答1:

As clarified via discussions on the Sphinx forums, the issue here is that the fields are in different indices, and a Sphinx document (a record in an index, which in the Thinking Sphinx/Rails context is an ActiveRecord model instance) should only exist in one Sphinx index, not spread across several.

Edit

To complete the answer here in SO and make the question standalone, the index now looks like this:

(1..5).each do |ind|
  ThinkingSphinx::Index.define :incident, name: "incident_index_#{ind}" ... do
    where "incidents.id % 5 = #{ind - 1}"
    indexes ...
    has ...
end

Documents are thus equally distributed between five sphinx index files, without a document existing in more than one index file.