-
Notifications
You must be signed in to change notification settings - Fork 801
/
Copy pathcequel.rb
78 lines (65 loc) · 2.41 KB
/
cequel.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module Elasticsearch
module Model
module Adapter
# An adapter for Cequel-based models
#
# @see https://github.com/cequel/cequel
#
module Cequel
Adapter.register self, lambda { |klass| !!defined?(::Cequel::Record) && klass.respond_to?(:ancestors) && klass.ancestors.include?(::Cequel::Record) }
module Records
# Return a `Cequel::RecordSet` instance
#
def records
pk = klass.key_column_names[0]
res = klass.where(pk => ids)
res.instance_exec(response.response['hits']['hits']) do |hits|
define_singleton_method :to_a do
self.entries.sort_by do |e|
hits.index do |hit|
hit['_id'].to_s == e.id.to_s
end
end
end
end
res
end
end
module Callbacks
# Handle index updates (creating, updating or deleting documents)
# when the model changes, by hooking into the lifecycle
#
# @see https://github.com/cequel/cequel/blob/master/lib/cequel/record/callbacks.rb
#
def self.included(base)
[:save, :create, :update].each do |item|
base.send("after_#{ item }", lambda { __elasticsearch__.index_document })
end
base.after_destroy { __elasticsearch__.delete_document }
end
end
module Importing
# Fetch batches of records from the database (used by the import method)
#
# @see http://api.rubyonrails.org/classes/ActiveRecord/Batches.html ActiveRecord::Batches.find_in_batches
#
def __find_in_batches(options = {}, &block)
query = options.delete(:query)
named_scope = options.delete(:scope)
preprocess = options.delete(:preprocess)
scope = self
scope = scope.__send__(named_scope) if named_scope
scope = scope.instance_exec(&query) if query
scope.find_in_batches(**options) do |batch|
batch = self.__send__(preprocess, batch) if preprocess
yield(batch) if batch.present?
end
end
def __transform
lambda { |model| { index: { _id: model.id, data: model.__elasticsearch__.as_indexed_json } } }
end
end
end
end
end
end