es-elasticity 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/elasticity/document.rb +11 -5
- data/lib/elasticity/search.rb +3 -3
- data/lib/elasticity/version.rb +1 -1
- data/spec/units/document_spec.rb +5 -6
- data/spec/units/multi_search_spec.rb +6 -6
- data/spec/units/search_spec.rb +10 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 930171d9343f5f51201e859afbc8f227e108a855
|
4
|
+
data.tar.gz: 7a949337b0c6265451e00bf5bc8ac9eef3170321
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f9cc21bea9324244cc52ffee24d4f4b6feddfaf59cc01962ce13162f99ff1af50a740a85408426328c98dd2566caab4524d7525a585d9efe04f2e82ce9abd1d
|
7
|
+
data.tar.gz: 16752b85bdcd775b75b59e0e9b11a2a1b53bbaad7e44e40485e700888214df93d9978e1a0330055ebf7dc86ce6783010aef7905a9686e155529039b49017afb1
|
data/README.md
CHANGED
@@ -105,12 +105,16 @@ adults.active_recors(Database::User) # => Array of Database::User instances
|
|
105
105
|
|
106
106
|
## Roadmap
|
107
107
|
|
108
|
+
- [ ] Use mapping instead of mappings, we wanna be consistent to ES not to elasticsearch-ruby
|
109
|
+
- [ ] Better automatic index name and document type
|
110
|
+
- [ ] Support for multiple document types
|
108
111
|
- [ ] Write more detailed documentation section for:
|
109
112
|
- [ ] Model definition
|
110
113
|
- [ ] Indexing, Bulk Indexing and Delete By Query
|
111
114
|
- [ ] Search and Multi Search
|
112
115
|
- [ ] ActiveRecord integration
|
113
116
|
- [ ] Get rid of to_document, generate automatically based on attributes
|
117
|
+
- [ ] Add some delegations on Document to Index
|
114
118
|
- [ ] Define from_active_record interface
|
115
119
|
|
116
120
|
## Contributing
|
data/lib/elasticity/document.rb
CHANGED
@@ -70,7 +70,7 @@ module Elasticity
|
|
70
70
|
# Fetches one specific document from the index by ID.
|
71
71
|
def self.get(id)
|
72
72
|
if doc = index.get_document(document_type, id)
|
73
|
-
new(doc["_source"])
|
73
|
+
new(doc["_source"].merge(_id: doc['_id']))
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -88,22 +88,28 @@ module Elasticity
|
|
88
88
|
def self.bulk_index(documents)
|
89
89
|
index.bulk do |b|
|
90
90
|
documents.each do |doc|
|
91
|
-
b.index(self.document_type, doc.
|
91
|
+
b.index(self.document_type, doc._id, doc.to_document)
|
92
92
|
end
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
96
|
# Define common attributes for all documents
|
97
|
-
attr_accessor :
|
97
|
+
attr_accessor :_id
|
98
98
|
|
99
99
|
# Creates a new Document instance with the provided attributes.
|
100
100
|
def initialize(attributes = {})
|
101
101
|
super(attributes)
|
102
102
|
end
|
103
103
|
|
104
|
+
def attributes=(attributes)
|
105
|
+
attributes.each do |attr, value|
|
106
|
+
self.public_send("#{attr}=", value)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
104
110
|
# Defines equality by comparing the ID and values of each instance variable.
|
105
111
|
def ==(other)
|
106
|
-
return false if
|
112
|
+
return false if _id != other._id
|
107
113
|
|
108
114
|
instance_variables.all? do |ivar|
|
109
115
|
instance_variable_get(ivar) == other.instance_variable_get(ivar)
|
@@ -119,7 +125,7 @@ module Elasticity
|
|
119
125
|
|
120
126
|
# Update this object on the index, creating or updating the document.
|
121
127
|
def update
|
122
|
-
self.class.index.index_document(self.class.document_type,
|
128
|
+
self.class.index.index_document(self.class.document_type, _id, to_document)
|
123
129
|
end
|
124
130
|
end
|
125
131
|
end
|
data/lib/elasticity/search.rb
CHANGED
@@ -24,7 +24,7 @@ module Elasticity
|
|
24
24
|
# into ActiveRecord models using the provided relation.
|
25
25
|
def active_records(relation)
|
26
26
|
return @active_record if defined?(@active_record)
|
27
|
-
response = @index.search(@document_type, @body.merge(_source:
|
27
|
+
response = @index.search(@document_type, @body.merge(_source: false))
|
28
28
|
@active_record = Result.new(response, ActiveRecordMapper.new(relation))
|
29
29
|
end
|
30
30
|
|
@@ -95,7 +95,7 @@ module Elasticity
|
|
95
95
|
|
96
96
|
def map(hits)
|
97
97
|
hits.map do |hit|
|
98
|
-
@document_klass.new(hit["_source"])
|
98
|
+
@document_klass.new(hit["_source"].merge(_id: hit['_id']))
|
99
99
|
end
|
100
100
|
end
|
101
101
|
end
|
@@ -106,7 +106,7 @@ module Elasticity
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def map(hits)
|
109
|
-
ids = hits.map { |h| h["
|
109
|
+
ids = hits.map { |h| h["_id"] }
|
110
110
|
|
111
111
|
if ids.any?
|
112
112
|
id_col = "#{quote(@relation.table_name)}.#{quote(@relation.klass.primary_key)}"
|
data/lib/elasticity/version.rb
CHANGED
data/spec/units/document_spec.rb
CHANGED
@@ -3,7 +3,6 @@ require "elasticity/search"
|
|
3
3
|
RSpec.describe Elasticity::Document do
|
4
4
|
mappings = {
|
5
5
|
properties: {
|
6
|
-
id: { type: "integer" },
|
7
6
|
name: { type: "string" },
|
8
7
|
|
9
8
|
items: {
|
@@ -27,7 +26,7 @@ RSpec.describe Elasticity::Document do
|
|
27
26
|
attr_accessor :name, :items
|
28
27
|
|
29
28
|
def to_document
|
30
|
-
{
|
29
|
+
{ name: name, items: items }
|
31
30
|
end
|
32
31
|
end
|
33
32
|
end
|
@@ -78,9 +77,9 @@ RSpec.describe Elasticity::Document do
|
|
78
77
|
end
|
79
78
|
|
80
79
|
it "gets specific document from the index" do
|
81
|
-
doc = { "
|
80
|
+
doc = { "_id" => 1, "_source" => { "name" => "Foo", "items" => [{ "name" => "Item1" }]}}
|
82
81
|
expect(index).to receive(:get_document).with("class_name", 1).and_return(doc)
|
83
|
-
expect(subject.get(1)).to eq klass.new(
|
82
|
+
expect(subject.get(1)).to eq klass.new(_id: 1, name: "Foo", items: [{ "name" => "Item1" }])
|
84
83
|
end
|
85
84
|
|
86
85
|
it "deletes specific document from index" do
|
@@ -91,10 +90,10 @@ RSpec.describe Elasticity::Document do
|
|
91
90
|
end
|
92
91
|
|
93
92
|
context "instance" do
|
94
|
-
subject { klass.new
|
93
|
+
subject { klass.new _id: 1, name: "Foo", items: [{ name: "Item1" }] }
|
95
94
|
|
96
95
|
it "stores the document in the index" do
|
97
|
-
expect(index).to receive(:index_document).with("class_name", 1, {
|
96
|
+
expect(index).to receive(:index_document).with("class_name", 1, { name: "Foo", items: [{ name: "Item1" }] })
|
98
97
|
subject.update
|
99
98
|
end
|
100
99
|
end
|
@@ -5,10 +5,10 @@ RSpec.describe Elasticity::MultiSearch do
|
|
5
5
|
let :klass do
|
6
6
|
Class.new do
|
7
7
|
include ActiveModel::Model
|
8
|
-
attr_accessor :
|
8
|
+
attr_accessor :_id, :name
|
9
9
|
|
10
10
|
def ==(other)
|
11
|
-
self.
|
11
|
+
self._id == other._id && self.name == other.name
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -16,8 +16,8 @@ RSpec.describe Elasticity::MultiSearch do
|
|
16
16
|
let :response do
|
17
17
|
{
|
18
18
|
"responses" => [
|
19
|
-
{ "hits" => { "total" => 2, "hits" => [{"
|
20
|
-
{ "hits" => { "total" => 1, "hits" => [{"
|
19
|
+
{ "hits" => { "total" => 2, "hits" => [{ "_id" => 1, "_source" => { "name" => "foo" }}, { "_id" => 2, "_source" => { "name" => "bar" }}]}},
|
20
|
+
{ "hits" => { "total" => 1, "hits" => [{ "_id" => 3, "_source" => { "name" => "baz" }}]}},
|
21
21
|
]
|
22
22
|
}
|
23
23
|
end
|
@@ -31,7 +31,7 @@ RSpec.describe Elasticity::MultiSearch do
|
|
31
31
|
{ index: "index_second", type: "document_second", search: { search: :second } },
|
32
32
|
]).and_return(response)
|
33
33
|
|
34
|
-
expect(Array(subject[:first])). to eq [klass.new(
|
35
|
-
expect(Array(subject[:second])). to eq [klass.new(
|
34
|
+
expect(Array(subject[:first])). to eq [klass.new(_id: 1, name: "foo"), klass.new(_id: 2, name: "bar")]
|
35
|
+
expect(Array(subject[:second])). to eq [klass.new(_id: 3, name: "baz")]
|
36
36
|
end
|
37
37
|
end
|
data/spec/units/search_spec.rb
CHANGED
@@ -7,15 +7,15 @@ RSpec.describe "Search" do
|
|
7
7
|
|
8
8
|
let :full_response do
|
9
9
|
{ "hits" => { "total" => 2, "hits" => [
|
10
|
-
{"
|
11
|
-
{"
|
10
|
+
{ "_id" => 1, "_source" => { "name" => "foo" } },
|
11
|
+
{ "_id" => 2, "_source" => { "name" => "bar" } },
|
12
12
|
]}}
|
13
13
|
end
|
14
14
|
|
15
15
|
let :ids_response do
|
16
16
|
{ "hits" => { "total" => 2, "hits" => [
|
17
|
-
{
|
18
|
-
{
|
17
|
+
{ "_id" => 1 },
|
18
|
+
{ "_id" => 2 },
|
19
19
|
]}}
|
20
20
|
end
|
21
21
|
|
@@ -26,10 +26,10 @@ RSpec.describe "Search" do
|
|
26
26
|
let :klass do
|
27
27
|
Class.new do
|
28
28
|
include ActiveModel::Model
|
29
|
-
attr_accessor :
|
29
|
+
attr_accessor :_id, :name
|
30
30
|
|
31
31
|
def ==(other)
|
32
|
-
self.
|
32
|
+
self._id == other._id && self.name == other.name
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
@@ -43,7 +43,7 @@ RSpec.describe "Search" do
|
|
43
43
|
expect(index).to receive(:search).with(document_type, body).and_return(full_response)
|
44
44
|
|
45
45
|
docs = subject.documents(klass)
|
46
|
-
expected = [klass.new(
|
46
|
+
expected = [klass.new(_id: 1, name: "foo"), klass.new(_id: 2, name: "bar")]
|
47
47
|
|
48
48
|
expect(docs.total).to eq 2
|
49
49
|
expect(docs.size).to eq expected.size
|
@@ -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: false)).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: false)).and_return(empty_response)
|
79
79
|
|
80
80
|
relation = double(:relation)
|
81
81
|
expect(relation).to receive(:none).and_return(relation)
|
@@ -95,7 +95,7 @@ RSpec.describe "Search" do
|
|
95
95
|
|
96
96
|
it "automatically maps the documents into the provided Document class" do
|
97
97
|
expect(index).to receive(:search).with(document_type, body).and_return(full_response)
|
98
|
-
expect(Array(subject)).to eq [klass.new(
|
98
|
+
expect(Array(subject)).to eq [klass.new(_id: 1, name: "foo"), klass.new(_id: 2, name: "bar")]
|
99
99
|
end
|
100
100
|
|
101
101
|
it "delegates active_records for the underlying search" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: es-elasticity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Kochenburger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|