Imposing pairing between two relationships indexed as attributes in thinking sphinx search

23 views
Skip to first unread message

Farnaz Ronaghi

unread,
Feb 28, 2014, 8:38:41 PM2/28/14
to thinkin...@googlegroups.com

Hi Everyone, I have a question about indexing and searching relationships and I'd appreciate any help.


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.

Pat Allan

unread,
Feb 28, 2014, 9:08:16 PM2/28/14
to thinkin...@googlegroups.com
Hi Famaz

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 array of question ids. There’s no relationship between 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.

Cheers

— 
Pat

--
You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thinking-sphi...@googlegroups.com.
To post to this group, send email to thinkin...@googlegroups.com.
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages