简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM