Search code examples
ruby-on-rails-3full-text-indexingindextank

IndexTank - How to prioritize result of a query


Using the indextank rubygem, if I index a post like this

indexes.document(post.id).add({:title=> post.title, :text=> post.body})

What should my query look like such that the title matches are returned before the body matches. Doing this:

indexes.search("title:#{query} OR text:#{query}", :snippet=> :text)

returns all matches ordered from newest to oldest.

Thank you.


Solution

  • What is your scoring function 0 defined as? By default, it is "-age", which means most recent first. For text relevance, you can replace scoring function with "rel", which means text relevance.

    I don't know of a way to ensure that every title match is guaranteed to be above the rest, but you can "boost" the title greatly by adding "^10" to your title part of the query. Here's an example:

    title:potter^10 text:whatever

    This means title matches are 10 times higher than matches for the "text" field. You can raise the number "10" higher if necessary.