Search code examples
rubyelasticsearchsearchkick

returning the score with searchkick


I'm using SearchKick with elastic search, and am trying to return the score. For example, if I did a search like this:

Profile.search("NYC").first

I'm going to get back the record with the highest score that matches that search. I want to also return the score with each record. I know I can grab the searchkick response and parse though it, but is there a faster way to just merge the score into the records that are returned?


Solution

  • To get all of the Profiles with a corresponding score do this:

    results = Profile.search("NYC")
    results_with_scores = results.zip(results.hits.map{ |hit| hit["_score"] })
    

    Now each element of results_with_scores will be of the form:

     `[Profile_object, corresponding_score]`