From b7e366a9dcd4ea72c3b53e46666465a4cd64c351 Mon Sep 17 00:00:00 2001 From: Rustam Sharshenov Date: Thu, 30 Oct 2014 11:41:03 +0600 Subject: [PATCH] Include indexed search_data on demand --- lib/searchkick/model.rb | 2 ++ lib/searchkick/query.rb | 3 ++- lib/searchkick/results.rb | 26 +++++++++++++++----------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index f2cad7a..ee3b76f 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -7,6 +7,8 @@ module Searchkick class_eval do cattr_reader :searchkick_options, :searchkick_env, :searchkick_klass + attr_accessor :response_search_data + callbacks = options.has_key?(:callbacks) ? options[:callbacks] : true class_variable_set :@@searchkick_options, options.dup diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index a791028..85299a7 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -328,7 +328,7 @@ module Searchkick # An empty array will cause only the _id and _type for each hit to be returned # http://www.elasticsearch.org/guide/reference/api/search/fields/ - if load + if load and not options[:include_search_data] payload[:fields] = [] elsif options[:select] payload[:fields] = options[:select] @@ -408,6 +408,7 @@ module Searchkick per_page: @per_page, padding: @padding, load: @load, + include_search_data: options[:include_search_data] || false, includes: options[:include] || options[:includes], json: !options[:json].nil? } diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 79f1d1d..30f11d6 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -41,19 +41,12 @@ module Searchkick # sort hits.map do |hit| - results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s } + result = results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s } + result.response_search_data = build_hashie_result hit if options[:include_search_data] and result + result end.compact else - hits.map do |hit| - result = - if hit["_source"] - hit.except("_source").merge(hit["_source"]) - else - hit.except("fields").merge(hit["fields"]) - end - result["id"] ||= result["_id"] # needed for legacy reasons - Hashie::Mash.new(result) - end + hits.map { |hit| build_hashie_result hit } end end end @@ -143,5 +136,16 @@ module Searchkick @response["hits"]["hits"] end + def build_hashie_result hit + result = + if hit["_source"] + hit.except("_source").merge(hit["_source"]) + else + hit.except("fields").merge(hit["fields"]) + end + result["id"] ||= result["_id"] # needed for legacy reasons + Hashie::Mash.new(result) + end + end end -- libgit2 0.21.0