Prakash Murthy
Prakash Murthy

Reputation: 13067

thinking-sphinx returning results that don't fit the criteria

I have a model practice_test_result for which I have defined the index as follows:

ThinkingSphinx::Index.define :practice_test_result, with: :active_record, delta: true do
  indexes [student.first_name, student.last_name], :as => :student_name

  set_property :min_infix_len => 1
end

Searching for test results returns unexpected results in some cases listed below:

> PracticeTestResult.search('x').map{|x| x.student.name }                                             
 => []

This is as per expectation, as none of the student names have 'x' in it.

> PracticeTestResult.search('p').map{|x| x.student.name } 
 => ["Jane Doe", "Jane Doe", "David Doe", "Timothy Doe", "Jane Doe"]

This is not expected, as none of the student names have 'p' in it.

> PracticeTestResult.search('ti').map{|x| x.student.name }
 => ["Jane Doe", "Jane Doe", "David Doe", "Timothy Doe", "Jane Doe"]

Again not as per expectation; should have only one value Timothy Doe returned.

> PracticeTestResult.search('tim').map{|x| x.student.name }
 => ["Timothy Doe"]

Works as expected.

Any suggestion on how to debug this? Could this be because min_infix_len is set too low at 1?

Sphinx version : Sphinx 2.1.7-release (rel21-r4638)

Thinking-Sphinx version : 3.1.1

Database : MySql

Rails version: 3.2.13

Upvotes: 1

Views: 99

Answers (1)

pat
pat

Reputation: 16226

What you're facing is a scenario that only occurs when you have the inheritance column (which is named type by default) in your model (whether or not you're using it for STI - Thinking Sphinx can't figure that out).

To allow for querying across certain subclasses of the STI hierarchy, Thinking Sphinx adds a field called sphinx_internal_class_name which is used to limit search results to certain models. In your case, it would contain 'PracticeTestResult' - hence, ti and p return all objects.

The workaround, as you've found, is to limit your queries to specific fields.

It's also worth noting that in earlier versions of TS v3.0.x, this internal field was always present. Given it's only useful when STI is in play, it now doesn't get added unless necessary.

Upvotes: 2

Related Questions