Sorting elasticsearch by column weight results with tire gem for rails project

522 views Asked by At
def self.search(params)
  return [] unless params[:query].present?
   tire.search(load: true) do
   query { string(params[:query], fields: %w(title description topics     
   username discussions)) }
  sort do
    by "likes", "desc"
    by "badges_count", "desc"
  end
  facet :tags do
    terms :tags
  end

  facet :topics do
    terms :topics
  end

    size params[:size] || 5
   end.results
end

I'm attempting to perform a search on a particular model. Although the results are currently sorting based on the most likes, and I'd like to base it more on a percentage basis for each column in the sort block.

for example:

50% for "likes" based on strength of semantic match in another column(:header) 20% for "badges_count" based on "badges_count"

Any help would be great as I am a bit stuck on how to expand the block more and create a mini algorithm to sort based by weight.

1

There are 1 answers

0
raykin On

Index your columns with boost options. ex:

class YourParticularModel
  mapping do
    indexes :likes,           boost: 100
    indexes :badges_count,    boost: 40
    # .... other indexes
  end
end

Then remove sort from your search method. The query result will auto weight the likes and badges_count matches.