Commit eca9be0b2a335db0b57b26b09157fc3e97a1a54c
1 parent
31f7ba50
Exists in
master
and in
19 other branches
Renamed RecordIndexer to RecordData [skip ci]
Showing
4 changed files
with
131 additions
and
131 deletions
Show diff stats
lib/searchkick.rb
@@ -11,7 +11,7 @@ require "searchkick/model" | @@ -11,7 +11,7 @@ require "searchkick/model" | ||
11 | require "searchkick/multi_search" | 11 | require "searchkick/multi_search" |
12 | require "searchkick/query" | 12 | require "searchkick/query" |
13 | require "searchkick/reindex_queue" | 13 | require "searchkick/reindex_queue" |
14 | -require "searchkick/record_indexer" | 14 | +require "searchkick/record_data" |
15 | require "searchkick/results" | 15 | require "searchkick/results" |
16 | require "searchkick/version" | 16 | require "searchkick/version" |
17 | 17 |
lib/searchkick/index.rb
@@ -110,11 +110,11 @@ module Searchkick | @@ -110,11 +110,11 @@ module Searchkick | ||
110 | end | 110 | end |
111 | 111 | ||
112 | def search_id(record) | 112 | def search_id(record) |
113 | - RecordIndexer.new(self, record).search_id | 113 | + RecordData.new(self, record).search_id |
114 | end | 114 | end |
115 | 115 | ||
116 | def document_type(record) | 116 | def document_type(record) |
117 | - RecordIndexer.new(self, record).document_type | 117 | + RecordData.new(self, record).document_type |
118 | end | 118 | end |
119 | 119 | ||
120 | def reindex_record(record) | 120 | def reindex_record(record) |
@@ -432,15 +432,15 @@ module Searchkick | @@ -432,15 +432,15 @@ module Searchkick | ||
432 | end | 432 | end |
433 | 433 | ||
434 | def bulk_index_helper(records) | 434 | def bulk_index_helper(records) |
435 | - Searchkick.indexer.queue(records.map { |r| RecordIndexer.new(self, r).index_data }) | 435 | + Searchkick.indexer.queue(records.map { |r| RecordData.new(self, r).index_data }) |
436 | end | 436 | end |
437 | 437 | ||
438 | def bulk_delete_helper(records) | 438 | def bulk_delete_helper(records) |
439 | - Searchkick.indexer.queue(records.reject { |r| r.id.blank? }.map { |r| RecordIndexer.new(self, r).delete_data }) | 439 | + Searchkick.indexer.queue(records.reject { |r| r.id.blank? }.map { |r| RecordData.new(self, r).delete_data }) |
440 | end | 440 | end |
441 | 441 | ||
442 | def bulk_update_helper(records, method_name) | 442 | def bulk_update_helper(records, method_name) |
443 | - Searchkick.indexer.queue(records.map { |r| RecordIndexer.new(self, r).update_data(method_name) }) | 443 | + Searchkick.indexer.queue(records.map { |r| RecordData.new(self, r).update_data(method_name) }) |
444 | end | 444 | end |
445 | 445 | ||
446 | def batches_key | 446 | def batches_key |
@@ -0,0 +1,125 @@ | @@ -0,0 +1,125 @@ | ||
1 | +module Searchkick | ||
2 | + class RecordData | ||
3 | + EXCLUDED_ATTRIBUTES = ["_id", "_type"] | ||
4 | + | ||
5 | + attr_reader :index, :record | ||
6 | + | ||
7 | + def initialize(index, record) | ||
8 | + @index = index | ||
9 | + @record = record | ||
10 | + end | ||
11 | + | ||
12 | + def index_data | ||
13 | + data = record_data | ||
14 | + data[:data] = search_data | ||
15 | + {index: data} | ||
16 | + end | ||
17 | + | ||
18 | + def update_data(method_name) | ||
19 | + data = record_data | ||
20 | + data[:data] = {doc: search_data(method_name)} | ||
21 | + {update: data} | ||
22 | + end | ||
23 | + | ||
24 | + def delete_data | ||
25 | + {delete: record_data} | ||
26 | + end | ||
27 | + | ||
28 | + def search_id | ||
29 | + id = record.respond_to?(:search_document_id) ? record.search_document_id : record.id | ||
30 | + id.is_a?(Numeric) ? id : id.to_s | ||
31 | + end | ||
32 | + | ||
33 | + def document_type(ignore_type = false) | ||
34 | + index.klass_document_type(record.class, ignore_type) | ||
35 | + end | ||
36 | + | ||
37 | + private | ||
38 | + | ||
39 | + def record_data | ||
40 | + data = { | ||
41 | + _index: index.name, | ||
42 | + _id: search_id, | ||
43 | + _type: document_type | ||
44 | + } | ||
45 | + data[:_routing] = record.search_routing if record.respond_to?(:search_routing) | ||
46 | + data | ||
47 | + end | ||
48 | + | ||
49 | + def search_data(method_name = nil) | ||
50 | + partial_reindex = !method_name.nil? | ||
51 | + options = record.class.searchkick_options | ||
52 | + | ||
53 | + # remove _id since search_id is used instead | ||
54 | + source = record.send(method_name || :search_data).each_with_object({}) { |(k, v), memo| memo[k.to_s] = v; memo }.except(*EXCLUDED_ATTRIBUTES) | ||
55 | + | ||
56 | + # conversions | ||
57 | + if options[:conversions] | ||
58 | + Array(options[:conversions]).map(&:to_s).each do |conversions_field| | ||
59 | + if source[conversions_field] | ||
60 | + source[conversions_field] = source[conversions_field].map { |k, v| {query: k, count: v} } | ||
61 | + end | ||
62 | + end | ||
63 | + end | ||
64 | + | ||
65 | + # hack to prevent generator field doesn't exist error | ||
66 | + if options[:suggest] | ||
67 | + options[:suggest].map(&:to_s).each do |field| | ||
68 | + source[field] = nil if !source[field] && !partial_reindex | ||
69 | + end | ||
70 | + end | ||
71 | + | ||
72 | + # locations | ||
73 | + if options[:locations] | ||
74 | + options[:locations].map(&:to_s).each do |field| | ||
75 | + if source[field] | ||
76 | + if !source[field].is_a?(Hash) && (source[field].first.is_a?(Array) || source[field].first.is_a?(Hash)) | ||
77 | + # multiple locations | ||
78 | + source[field] = source[field].map { |a| location_value(a) } | ||
79 | + else | ||
80 | + source[field] = location_value(source[field]) | ||
81 | + end | ||
82 | + end | ||
83 | + end | ||
84 | + end | ||
85 | + | ||
86 | + if !source.key?("type") && record.class.searchkick_klass.searchkick_options[:inheritance] | ||
87 | + source["type"] = document_type(true) | ||
88 | + end | ||
89 | + | ||
90 | + cast_big_decimal(source) | ||
91 | + | ||
92 | + source | ||
93 | + end | ||
94 | + | ||
95 | + def location_value(value) | ||
96 | + if value.is_a?(Array) | ||
97 | + value.map(&:to_f).reverse | ||
98 | + elsif value.is_a?(Hash) | ||
99 | + {lat: value[:lat].to_f, lon: value[:lon].to_f} | ||
100 | + else | ||
101 | + value | ||
102 | + end | ||
103 | + end | ||
104 | + | ||
105 | + # change all BigDecimal values to floats due to | ||
106 | + # https://github.com/rails/rails/issues/6033 | ||
107 | + # possible loss of precision :/ | ||
108 | + def cast_big_decimal(obj) | ||
109 | + case obj | ||
110 | + when BigDecimal | ||
111 | + obj.to_f | ||
112 | + when Hash | ||
113 | + obj.each do |k, v| | ||
114 | + obj[k] = cast_big_decimal(v) | ||
115 | + end | ||
116 | + when Enumerable | ||
117 | + obj.map do |v| | ||
118 | + cast_big_decimal(v) | ||
119 | + end | ||
120 | + else | ||
121 | + obj | ||
122 | + end | ||
123 | + end | ||
124 | + end | ||
125 | +end |
lib/searchkick/record_indexer.rb
@@ -1,125 +0,0 @@ | @@ -1,125 +0,0 @@ | ||
1 | -module Searchkick | ||
2 | - class RecordIndexer | ||
3 | - EXCLUDED_ATTRIBUTES = ["_id", "_type"] | ||
4 | - | ||
5 | - attr_reader :index, :record | ||
6 | - | ||
7 | - def initialize(index, record) | ||
8 | - @index = index | ||
9 | - @record = record | ||
10 | - end | ||
11 | - | ||
12 | - def index_data | ||
13 | - data = record_data | ||
14 | - data[:data] = search_data | ||
15 | - {index: data} | ||
16 | - end | ||
17 | - | ||
18 | - def update_data(method_name) | ||
19 | - data = record_data | ||
20 | - data[:data] = {doc: search_data(method_name)} | ||
21 | - {update: data} | ||
22 | - end | ||
23 | - | ||
24 | - def delete_data | ||
25 | - {delete: record_data} | ||
26 | - end | ||
27 | - | ||
28 | - def document_type(ignore_type = false) | ||
29 | - index.klass_document_type(record.class, ignore_type) | ||
30 | - end | ||
31 | - | ||
32 | - def search_id | ||
33 | - id = record.respond_to?(:search_document_id) ? record.search_document_id : record.id | ||
34 | - id.is_a?(Numeric) ? id : id.to_s | ||
35 | - end | ||
36 | - | ||
37 | - private | ||
38 | - | ||
39 | - def record_data | ||
40 | - data = { | ||
41 | - _index: index.name, | ||
42 | - _id: search_id, | ||
43 | - _type: document_type | ||
44 | - } | ||
45 | - data[:_routing] = record.search_routing if record.respond_to?(:search_routing) | ||
46 | - data | ||
47 | - end | ||
48 | - | ||
49 | - def search_data(method_name = nil) | ||
50 | - partial_reindex = !method_name.nil? | ||
51 | - options = record.class.searchkick_options | ||
52 | - | ||
53 | - # remove _id since search_id is used instead | ||
54 | - source = record.send(method_name || :search_data).each_with_object({}) { |(k, v), memo| memo[k.to_s] = v; memo }.except(*EXCLUDED_ATTRIBUTES) | ||
55 | - | ||
56 | - # conversions | ||
57 | - if options[:conversions] | ||
58 | - Array(options[:conversions]).map(&:to_s).each do |conversions_field| | ||
59 | - if source[conversions_field] | ||
60 | - source[conversions_field] = source[conversions_field].map { |k, v| {query: k, count: v} } | ||
61 | - end | ||
62 | - end | ||
63 | - end | ||
64 | - | ||
65 | - # hack to prevent generator field doesn't exist error | ||
66 | - if options[:suggest] | ||
67 | - options[:suggest].map(&:to_s).each do |field| | ||
68 | - source[field] = nil if !source[field] && !partial_reindex | ||
69 | - end | ||
70 | - end | ||
71 | - | ||
72 | - # locations | ||
73 | - if options[:locations] | ||
74 | - options[:locations].map(&:to_s).each do |field| | ||
75 | - if source[field] | ||
76 | - if !source[field].is_a?(Hash) && (source[field].first.is_a?(Array) || source[field].first.is_a?(Hash)) | ||
77 | - # multiple locations | ||
78 | - source[field] = source[field].map { |a| location_value(a) } | ||
79 | - else | ||
80 | - source[field] = location_value(source[field]) | ||
81 | - end | ||
82 | - end | ||
83 | - end | ||
84 | - end | ||
85 | - | ||
86 | - if !source.key?("type") && record.class.searchkick_klass.searchkick_options[:inheritance] | ||
87 | - source["type"] = document_type(true) | ||
88 | - end | ||
89 | - | ||
90 | - cast_big_decimal(source) | ||
91 | - | ||
92 | - source | ||
93 | - end | ||
94 | - | ||
95 | - def location_value(value) | ||
96 | - if value.is_a?(Array) | ||
97 | - value.map(&:to_f).reverse | ||
98 | - elsif value.is_a?(Hash) | ||
99 | - {lat: value[:lat].to_f, lon: value[:lon].to_f} | ||
100 | - else | ||
101 | - value | ||
102 | - end | ||
103 | - end | ||
104 | - | ||
105 | - # change all BigDecimal values to floats due to | ||
106 | - # https://github.com/rails/rails/issues/6033 | ||
107 | - # possible loss of precision :/ | ||
108 | - def cast_big_decimal(obj) | ||
109 | - case obj | ||
110 | - when BigDecimal | ||
111 | - obj.to_f | ||
112 | - when Hash | ||
113 | - obj.each do |k, v| | ||
114 | - obj[k] = cast_big_decimal(v) | ||
115 | - end | ||
116 | - when Enumerable | ||
117 | - obj.map do |v| | ||
118 | - cast_big_decimal(v) | ||
119 | - end | ||
120 | - else | ||
121 | - obj | ||
122 | - end | ||
123 | - end | ||
124 | - end | ||
125 | -end |