Arel
Arel

Reputation: 3938

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?

Upvotes: 3

Views: 1257

Answers (2)

Georg Ledermann
Georg Ledermann

Reputation: 2752

With SearchKick v2.0.1 or later you can do:

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

Upvotes: 3

pthamm
pthamm

Reputation: 1841

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]`

Upvotes: 4

Related Questions