From 899ce33a121f26c56878f0291fb46a024e158567 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 10 May 2018 09:30:36 -0700 Subject: [PATCH] Fixed with_hit and with_highlights when records in search index do not exist in database --- CHANGELOG.md | 1 + lib/searchkick/results.rb | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd3ef70..93dfd13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Added `:inline` as alias for `true` for `callbacks` and `mode` options - Friendlier error message for bad mapping with partial matches - Warn when records in search index do not exist in database +- Fixed `with_hit` and `with_highlights` when records in search index do not exist in database - Fixed error with highlights and match all ## 3.0.3 diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 10ea4e1..beb137f 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -16,7 +16,11 @@ module Searchkick end def results - @results ||= begin + @results ||= with_hit.map(&:first) + end + + def with_hit + @with_hit ||= begin if options[:load] # results can have different types results = {} @@ -38,8 +42,8 @@ module Searchkick end end end - result - end.compact + [result, hit] + end.select { |v| v[0] } if results.size != hits.size warn "[searchkick] WARNING: Records in search index do not exist in database" @@ -65,7 +69,7 @@ module Searchkick end result["id"] ||= result["_id"] # needed for legacy reasons - HashWrapper.new(result) + [HashWrapper.new(result), hit] end end end @@ -179,10 +183,6 @@ module Searchkick end end - def with_hit - results.zip(hits) - end - def highlights(multiple: false) hits.map do |hit| hit_highlights(hit, multiple: multiple) @@ -190,7 +190,9 @@ module Searchkick end def with_highlights(multiple: false) - results.zip(highlights(multiple: multiple)) + with_hit do |result, hit| + [result, hit_highlights(hit, multiple: multiple)] + end end def misspellings? -- libgit2 0.21.0