Commit b7e366a9dcd4ea72c3b53e46666465a4cd64c351

Authored by Rustam Sharshenov
1 parent 9c8170e2

Include indexed search_data on demand

lib/searchkick/model.rb
@@ -7,6 +7,8 @@ module Searchkick @@ -7,6 +7,8 @@ module Searchkick
7 class_eval do 7 class_eval do
8 cattr_reader :searchkick_options, :searchkick_env, :searchkick_klass 8 cattr_reader :searchkick_options, :searchkick_env, :searchkick_klass
9 9
  10 + attr_accessor :response_search_data
  11 +
10 callbacks = options.has_key?(:callbacks) ? options[:callbacks] : true 12 callbacks = options.has_key?(:callbacks) ? options[:callbacks] : true
11 13
12 class_variable_set :@@searchkick_options, options.dup 14 class_variable_set :@@searchkick_options, options.dup
lib/searchkick/query.rb
@@ -328,7 +328,7 @@ module Searchkick @@ -328,7 +328,7 @@ module Searchkick
328 328
329 # An empty array will cause only the _id and _type for each hit to be returned 329 # An empty array will cause only the _id and _type for each hit to be returned
330 # http://www.elasticsearch.org/guide/reference/api/search/fields/ 330 # http://www.elasticsearch.org/guide/reference/api/search/fields/
331 - if load 331 + if load and not options[:include_search_data]
332 payload[:fields] = [] 332 payload[:fields] = []
333 elsif options[:select] 333 elsif options[:select]
334 payload[:fields] = options[:select] 334 payload[:fields] = options[:select]
@@ -408,6 +408,7 @@ module Searchkick @@ -408,6 +408,7 @@ module Searchkick
408 per_page: @per_page, 408 per_page: @per_page,
409 padding: @padding, 409 padding: @padding,
410 load: @load, 410 load: @load,
  411 + include_search_data: options[:include_search_data] || false,
411 includes: options[:include] || options[:includes], 412 includes: options[:include] || options[:includes],
412 json: !options[:json].nil? 413 json: !options[:json].nil?
413 } 414 }
lib/searchkick/results.rb
@@ -41,19 +41,12 @@ module Searchkick @@ -41,19 +41,12 @@ module Searchkick
41 41
42 # sort 42 # sort
43 hits.map do |hit| 43 hits.map do |hit|
44 - results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s } 44 + result = results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s }
  45 + result.response_search_data = build_hashie_result hit if options[:include_search_data] and result
  46 + result
45 end.compact 47 end.compact
46 else 48 else
47 - hits.map do |hit|  
48 - result =  
49 - if hit["_source"]  
50 - hit.except("_source").merge(hit["_source"])  
51 - else  
52 - hit.except("fields").merge(hit["fields"])  
53 - end  
54 - result["id"] ||= result["_id"] # needed for legacy reasons  
55 - Hashie::Mash.new(result)  
56 - end 49 + hits.map { |hit| build_hashie_result hit }
57 end 50 end
58 end 51 end
59 end 52 end
@@ -143,5 +136,16 @@ module Searchkick @@ -143,5 +136,16 @@ module Searchkick
143 @response["hits"]["hits"] 136 @response["hits"]["hits"]
144 end 137 end
145 138
  139 + def build_hashie_result hit
  140 + result =
  141 + if hit["_source"]
  142 + hit.except("_source").merge(hit["_source"])
  143 + else
  144 + hit.except("fields").merge(hit["fields"])
  145 + end
  146 + result["id"] ||= result["_id"] # needed for legacy reasons
  147 + Hashie::Mash.new(result)
  148 + end
  149 +
146 end 150 end
147 end 151 end