returning the score with searchkick

1.1k views Asked by At

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?

2

There are 2 answers

0
pthamm On BEST ANSWER

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]`
1
Georg Ledermann On

With SearchKick v2.0.1 or later you can do:

Profile.search("NYC").first.search_hit['_score']