Question

I have a User model that indexes ProfileAnswer, a has_many relationship. I am indexing profile_answers.response as well profile_answers.question_id .

I need to be able to run search queries that search a string for one particular question.

Here is the way I am doing this which is wrong. with data points

A{

profile_answer{
  id = 99
  question_id = 1
  answer = 'Hi'
}

profile_answer{
  id = 100
  question_id = 2
  answer = 'Bye'
}

}
B{

 profile_answer{
     id = 102
     question_id = 1
    answer = 'Bye'
 }

 profile_answer{
    id = 101
    question_id = 2
    answer = 'Hi'
 }

}

A and B are both returned when I search for 'Hi' on question_id = 2.

Here is my index definition:

 indexes profile_answers.answer, sortable: true, as: "answer"
 has profile_answers.question_id, as: "question_id"

and do my search as:

  User.search('@answer "Hi"', match_mode: :extended, with: {question_id: given_question.id})

I have tried indexing some combination of these columns and search that but that has never produced the right index.

Was it helpful?

Solution

Answered this on the Thinking Sphinx Google group too:

What you’re trying to do is not possible (while searching on Users, at least). Sphinx has no concept of hashes/dictionaries… in your index, each user has a field called answer, which contains all the answers joined together in a single string, and then an attribute that is an array of question ids. There’s no relationship between the question ids and each answer.

A better approach would be to define an index on ProfileAnswer, and search via that instead - and then display the user for each ProfileAnswer in the search results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top