es-elasticity 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4316eca2c0cf5953a781ed8233e13eb6b6ae90fd
4
- data.tar.gz: f37edd93770533fc4cb5c05b515438abf481aea9
3
+ metadata.gz: 5e2ae11b9e3555edb1f1ad24788f4066a0f489da
4
+ data.tar.gz: 355f78e40852e671ac9cb28d374d45426cf0f687
5
5
  SHA512:
6
- metadata.gz: dfc1d0d24057f8e8a3bb890ff6b2812d3db20dbd0972ba07aa957e454bdc61f261fba3eca868397db40e14c6ae649ffb3314312ae288b005c532a7e855bd14d9
7
- data.tar.gz: f76f11327b69c509442cda2a6788c5e13252e44a86146d6c5424981508fab1e480592f95a028aee8bf708768418de347255445e6c4d4a7bea3cfedcceb40dc4a
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
 
@@ -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 defined?(@index)
8
-
9
- index_name = self.name.underscore.pluralize
7
+ return @index if @index.present?
8
+ @index = Index.new(Elasticity.config.client, self.index_name)
9
+ end
10
10
 
11
- if namespace = Elasticity.config.namespace
12
- index_name = "#{namespace}_#{index_name}"
13
- end
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
- @index = Index.new(Elasticity.config.client, index_name)
16
- @index.create_if_undefined(settings: Elasticity.config.settings, mappings: @mappings)
17
- @index
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
- self.index.name
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
- self.name.underscore
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
@@ -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 @client.indices.exists(index: @name)
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 @client.indices.exists(index: @name)
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, body: search.body }
32
+ { index: search.index.name, type: search.document_type, search: search.body }
33
33
  end
34
34
 
35
35
  results = {}
@@ -66,21 +66,7 @@ module Elasticity
66
66
  @mapper = mapper
67
67
  end
68
68
 
69
- def [](idx)
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
@@ -1,3 +1,3 @@
1
1
  module Elasticity
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -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", body: { search: :first } },
31
- { index: "index_second", type: "document_second", body: { search: :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")]
@@ -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: ["id"])).and_return(ids_response)
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: ["id"])).and_return(empty_response)
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es-elasticity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Kochenburger