es-elasticity 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/elasticity/document.rb +32 -11
- data/lib/elasticity/index.rb +6 -2
- data/lib/elasticity/multi_search.rb +1 -1
- data/lib/elasticity/search.rb +13 -15
- data/lib/elasticity/version.rb +1 -1
- data/spec/units/document_spec.rb +0 -1
- data/spec/units/multi_search_spec.rb +2 -2
- data/spec/units/search_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e2ae11b9e3555edb1f1ad24788f4066a0f489da
|
4
|
+
data.tar.gz: 355f78e40852e671ac9cb28d374d45426cf0f687
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d913071dab846c9b9f4856fec74567c3154088a20f827afe9529880a184913bc67a8ff51afe38402d68524d5a41cf53c605dc68bd23edb849c90da41f8813757
|
7
|
+
data.tar.gz: f93771839ddf6e8ae78e0d305e8c1a869de0262c78677a65c0de1f6a9360ec633f014258264886d6e801bfaba7b0f5b458648c5cd4ac71d2ac02b8d1a0a3cb68
|
data/README.md
CHANGED
@@ -110,6 +110,8 @@ adults.active_recors(Database::User) # => Array of Database::User instances
|
|
110
110
|
- [ ] Indexing, Bulk Indexing and Delete By Query
|
111
111
|
- [ ] Search and Multi Search
|
112
112
|
- [ ] ActiveRecord integration
|
113
|
+
- [ ] Get rid of to_document, generate automatically based on attributes
|
114
|
+
- [ ] Define from_active_record interface
|
113
115
|
|
114
116
|
## Contributing
|
115
117
|
|
data/lib/elasticity/document.rb
CHANGED
@@ -4,28 +4,49 @@ module Elasticity
|
|
4
4
|
|
5
5
|
# Returns the instance of Elasticity::Index associated with this document.
|
6
6
|
def self.index
|
7
|
-
return @index if
|
8
|
-
|
9
|
-
|
7
|
+
return @index if @index.present?
|
8
|
+
@index = Index.new(Elasticity.config.client, self.index_name)
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
# Creates the index for this document
|
12
|
+
def self.create_index
|
13
|
+
self.index.create_if_undefined(settings: Elasticity.config.settings, mappings: { document_type => @mappings })
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
@
|
16
|
+
# Re-creates the index for this document
|
17
|
+
def self.recreate_index
|
18
|
+
self.index.recreate(settings: Elasticity.config.settings, mappings: { document_type => @mappings })
|
18
19
|
end
|
19
20
|
|
20
21
|
# The index name to be used for indexing and storing data for this document model.
|
21
22
|
# By default, it's the class name converted to underscore and plural.
|
22
23
|
def self.index_name
|
23
|
-
|
24
|
+
return @index_name if defined?(@index_name)
|
25
|
+
|
26
|
+
@index_name = self.name.underscore.pluralize
|
27
|
+
|
28
|
+
if namespace = Elasticity.config.namespace
|
29
|
+
@index_name = "#{namespace}_#{index_name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
@index_name
|
33
|
+
end
|
34
|
+
|
35
|
+
# Sets the index name to something else than the default
|
36
|
+
def self.index_name=(name)
|
37
|
+
@index_name = name
|
38
|
+
@index = nil
|
24
39
|
end
|
25
40
|
|
26
41
|
# The document type to be used, it's inferred by the class name.
|
27
42
|
def self.document_type
|
28
|
-
|
43
|
+
return @document_type if defined?(@document_type)
|
44
|
+
@document_type = self.name.demodulize.underscore
|
45
|
+
end
|
46
|
+
|
47
|
+
# Sets the document type to something different than the default
|
48
|
+
def self.document_type=(document_type)
|
49
|
+
@document_type = document_type
|
29
50
|
end
|
30
51
|
|
31
52
|
# Sets the mapping for this model, which will be used to create the associated index and
|
data/lib/elasticity/index.rb
CHANGED
@@ -7,13 +7,17 @@ module Elasticity
|
|
7
7
|
@name = index_name
|
8
8
|
end
|
9
9
|
|
10
|
+
def exists?
|
11
|
+
@client.indices.exists(index: @name)
|
12
|
+
end
|
13
|
+
|
10
14
|
def create(index_def)
|
11
15
|
args = { index: @name, body: index_def }
|
12
16
|
instrument("index_create", args) { @client.indices.create(args) }
|
13
17
|
end
|
14
18
|
|
15
19
|
def create_if_undefined(index_def)
|
16
|
-
create(index_def) unless
|
20
|
+
create(index_def) unless exists?
|
17
21
|
end
|
18
22
|
|
19
23
|
def delete
|
@@ -22,7 +26,7 @@ module Elasticity
|
|
22
26
|
end
|
23
27
|
|
24
28
|
def delete_if_defined
|
25
|
-
delete if
|
29
|
+
delete if exists?
|
26
30
|
end
|
27
31
|
|
28
32
|
def recreate(index_def = nil)
|
@@ -29,7 +29,7 @@ module Elasticity
|
|
29
29
|
|
30
30
|
def fetch
|
31
31
|
multi_body = @searches.map do |name, search, _|
|
32
|
-
{ index: search.index.name, type: search.document_type,
|
32
|
+
{ index: search.index.name, type: search.document_type, search: search.body }
|
33
33
|
end
|
34
34
|
|
35
35
|
results = {}
|
data/lib/elasticity/search.rb
CHANGED
@@ -66,21 +66,7 @@ module Elasticity
|
|
66
66
|
@mapper = mapper
|
67
67
|
end
|
68
68
|
|
69
|
-
|
70
|
-
mapping[idx]
|
71
|
-
end
|
72
|
-
|
73
|
-
def each(&block)
|
74
|
-
mapping.each(&block)
|
75
|
-
end
|
76
|
-
|
77
|
-
def to_ary
|
78
|
-
mapping.to_ary
|
79
|
-
end
|
80
|
-
|
81
|
-
def size
|
82
|
-
mapping.size
|
83
|
-
end
|
69
|
+
delegate :[], :each, :to_ary, :size, :+, :-, to: :mapping
|
84
70
|
|
85
71
|
# The total number of entries as returned by ES
|
86
72
|
def total
|
@@ -144,6 +130,18 @@ module Elasticity
|
|
144
130
|
@document_klass = document_klass
|
145
131
|
end
|
146
132
|
|
133
|
+
def index
|
134
|
+
@search.index
|
135
|
+
end
|
136
|
+
|
137
|
+
def document_type
|
138
|
+
@search.document_type
|
139
|
+
end
|
140
|
+
|
141
|
+
def body
|
142
|
+
@search.body
|
143
|
+
end
|
144
|
+
|
147
145
|
def active_records(relation)
|
148
146
|
@search.active_records(relation)
|
149
147
|
end
|
data/lib/elasticity/version.rb
CHANGED
data/spec/units/document_spec.rb
CHANGED
@@ -60,7 +60,6 @@ RSpec.describe Elasticity::Document do
|
|
60
60
|
Elasticity.config.client = client
|
61
61
|
|
62
62
|
expect(Elasticity::Index).to receive(:new).with(client, "elasticity_test_class_names").and_return(index)
|
63
|
-
expect(index).to receive(:create_if_undefined).with(settings: settings, mappings: mappings)
|
64
63
|
|
65
64
|
expect(subject.index).to be index
|
66
65
|
end
|
@@ -27,8 +27,8 @@ RSpec.describe Elasticity::MultiSearch do
|
|
27
27
|
subject.add(:second, Elasticity::Search.new(double(:index, name: "index_second"), "document_second", { search: :second }), documents: klass)
|
28
28
|
|
29
29
|
expect(Elasticity.config.client).to receive(:msearch).with(body: [
|
30
|
-
{ index: "index_first", type: "document_first",
|
31
|
-
{ index: "index_second", type: "document_second",
|
30
|
+
{ index: "index_first", type: "document_first", search: { search: :first } },
|
31
|
+
{ index: "index_second", type: "document_second", search: { search: :second } },
|
32
32
|
]).and_return(response)
|
33
33
|
|
34
34
|
expect(Array(subject[:first])). to eq [klass.new(id: 1, name: "foo"), klass.new(id: 2, name: "bar")]
|
data/spec/units/search_spec.rb
CHANGED
@@ -59,7 +59,7 @@ RSpec.describe "Search" do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "searches the index and return active record models" do
|
62
|
-
expect(index).to receive(:search).with(document_type, body.merge(_source: [
|
62
|
+
expect(index).to receive(:search).with(document_type, body.merge(_source: [])).and_return(ids_response)
|
63
63
|
|
64
64
|
relation = double(:relation,
|
65
65
|
connection: double(:connection),
|
@@ -75,7 +75,7 @@ RSpec.describe "Search" do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
it "return relation.none from activerecord relation with no matches" do
|
78
|
-
expect(index).to receive(:search).with(document_type, body.merge(_source: [
|
78
|
+
expect(index).to receive(:search).with(document_type, body.merge(_source: [])).and_return(empty_response)
|
79
79
|
|
80
80
|
relation = double(:relation)
|
81
81
|
expect(relation).to receive(:none).and_return(relation)
|