elasticord 1.0.1 → 1.0.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/dash_overlord/use_cases/v1/shared/search_and_paginate/apply_filters.rb +2 -0
- data/lib/elasticord.rb +5 -4
- data/lib/elasticord/base.rb +14 -38
- data/lib/elasticord/{entities → client}/array_with_meta_data.rb +1 -7
- data/lib/elasticord/client/base.rb +57 -0
- data/lib/elasticord/{orm → client}/create.rb +3 -3
- data/lib/elasticord/client/delete.rb +23 -0
- data/lib/elasticord/client/get.rb +34 -0
- data/lib/elasticord/{orm → client}/index.rb +3 -3
- data/lib/elasticord/{orm → client}/search_builder.rb +8 -12
- data/lib/elasticord/version.rb +1 -1
- data/spec/elasticord/base_spec.rb +102 -64
- data/spec/elasticord/client/base_spec.rb +112 -0
- data/spec/elasticord/{orm → client}/search_builder_spec.rb +33 -42
- metadata +12 -9
- data/lib/elasticord/orm/delete.rb +0 -23
- data/lib/elasticord/orm/get.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbe255262b51c074ad05a210ae821894cae53710
|
4
|
+
data.tar.gz: f5fa702ceffbb7051937446166e919fcf9f6204f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c350937a3105ac455fd96fa0c4bc8238551ea759732c58a7ebea5672f89efe1e56a0abb68b357603751ee9f07aa9e21b0b819da64369c03f1db8a65d79b0dc9d
|
7
|
+
data.tar.gz: eb4694da749d2a315a3d5458ccac26afa5aff1346224ab2d65079e3e5aeecdea2c41e5731d02d3b56b8eff219f26a5dca941994310f68f1b6c021e75ada66c85
|
data/Gemfile.lock
CHANGED
data/lib/elasticord.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'forwardable'
|
1
2
|
require 'elasticsearch'
|
2
3
|
|
3
4
|
require_relative 'elasticord/base'
|
@@ -5,12 +6,12 @@ require_relative 'elasticord/config'
|
|
5
6
|
require_relative 'elasticord/version'
|
6
7
|
|
7
8
|
module Elasticord
|
8
|
-
def self.
|
9
|
-
@
|
9
|
+
def self.elastic_search_client
|
10
|
+
@elastic_search_client ||= reset_elastic_search_client
|
10
11
|
end
|
11
12
|
|
12
|
-
def self.
|
13
|
-
@
|
13
|
+
def self.reset_elastic_search_client
|
14
|
+
@elastic_search_client = Elasticsearch::Client.new \
|
14
15
|
log: Elasticord::Config.get(:log)
|
15
16
|
end
|
16
17
|
|
data/lib/elasticord/base.rb
CHANGED
@@ -1,51 +1,27 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
|
3
|
-
require 'elasticord/
|
4
|
-
require 'elasticord/orm/index'
|
5
|
-
require 'elasticord/orm/create'
|
6
|
-
require 'elasticord/orm/delete'
|
7
|
-
require 'elasticord/orm/search_builder'
|
3
|
+
require 'elasticord/client/base'
|
8
4
|
|
9
5
|
module Elasticord
|
10
6
|
class Base < OpenStruct
|
11
7
|
module ClassMethods
|
12
|
-
|
13
|
-
result = ORM::Create.one(index, type, attributes)
|
8
|
+
extend Forwardable
|
14
9
|
|
15
|
-
|
10
|
+
def client(sub_type = nil)
|
11
|
+
full_type = [type, sub_type].compact.join('_')
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
self.new(attributes)
|
20
|
-
end
|
21
|
-
|
22
|
-
def get(id)
|
23
|
-
result = ORM::Get.one(index, type, id)
|
24
|
-
|
25
|
-
return false unless result
|
26
|
-
|
27
|
-
self.new(result.merge({ 'id' => id }))
|
28
|
-
end
|
29
|
-
|
30
|
-
def search(body = {})
|
31
|
-
none.load(body)
|
13
|
+
Client::Base.new \
|
14
|
+
Elasticord.elastic_search_client, self, index, full_type
|
32
15
|
end
|
33
16
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
def delete_all
|
43
|
-
ORM::Delete.all(index)
|
44
|
-
end
|
45
|
-
|
46
|
-
def refresh_index
|
47
|
-
ORM::Index.refresh(index)
|
48
|
-
end
|
17
|
+
def_delegators :client,
|
18
|
+
:get,
|
19
|
+
:none,
|
20
|
+
:create,
|
21
|
+
:search,
|
22
|
+
:delete_all,
|
23
|
+
:refresh_index,
|
24
|
+
:delete_by_query
|
49
25
|
|
50
26
|
def index
|
51
27
|
return @index if defined?(@index)
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module Elasticord
|
2
|
-
module
|
2
|
+
module Client
|
3
3
|
class ArrayWithMetaData < SimpleDelegator
|
4
4
|
attr_accessor :per_page, :current_page, :total_results
|
5
5
|
|
@@ -26,12 +26,6 @@ module Elasticord
|
|
26
26
|
def from
|
27
27
|
(current_page - 1) * size
|
28
28
|
end
|
29
|
-
|
30
|
-
protected
|
31
|
-
|
32
|
-
def parse_string_number(number)
|
33
|
-
number.class == String ? number.presence : number
|
34
|
-
end
|
35
29
|
end
|
36
30
|
end
|
37
31
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
require 'elasticord/client/get'
|
4
|
+
require 'elasticord/client/index'
|
5
|
+
require 'elasticord/client/create'
|
6
|
+
require 'elasticord/client/delete'
|
7
|
+
require 'elasticord/client/search_builder'
|
8
|
+
|
9
|
+
module Elasticord
|
10
|
+
module Client
|
11
|
+
class Base
|
12
|
+
def initialize(elastic_search_client, entity, index, type)
|
13
|
+
@elastic_search_client, @entity, @index, @type =
|
14
|
+
elastic_search_client, entity, index, type
|
15
|
+
end
|
16
|
+
|
17
|
+
def create(attributes = {})
|
18
|
+
result = Client::Create.one \
|
19
|
+
elastic_search_client, index, type, attributes
|
20
|
+
|
21
|
+
return false unless result
|
22
|
+
|
23
|
+
attributes[:id] ||= result['_id']
|
24
|
+
|
25
|
+
entity.new(attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get(id)
|
29
|
+
result = Client::Get.one(elastic_search_client, index, type, id)
|
30
|
+
|
31
|
+
return false unless result
|
32
|
+
|
33
|
+
entity.new(result.merge({ 'id' => id }))
|
34
|
+
end
|
35
|
+
|
36
|
+
def search(body_params = {})
|
37
|
+
none.load(body_params)
|
38
|
+
end
|
39
|
+
|
40
|
+
def none
|
41
|
+
Client::SearchBuilder.new(elastic_search_client, entity, index, type)
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete_all
|
45
|
+
Client::Delete.all(elastic_search_client, index)
|
46
|
+
end
|
47
|
+
|
48
|
+
def refresh_index
|
49
|
+
Client::Index.refresh(elastic_search_client, index)
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
|
54
|
+
attr_reader :elastic_search_client, :entity, :index, :type
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module Elasticord
|
2
|
-
module
|
2
|
+
module Client
|
3
3
|
module Create
|
4
4
|
module_function
|
5
5
|
|
6
6
|
REFRESH_INDEX_AFTER_CREATE = true
|
7
7
|
|
8
|
-
def one(index, type, attributes = {})
|
8
|
+
def one(elastic_search_client, index, type, attributes = {})
|
9
9
|
params = build_params(index, type, attributes)
|
10
10
|
|
11
11
|
begin
|
12
|
-
|
12
|
+
elastic_search_client.index params
|
13
13
|
rescue Elasticsearch::Transport::Transport::Errors::Conflict
|
14
14
|
return false
|
15
15
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Elasticord
|
2
|
+
module Client
|
3
|
+
module Delete
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def all(elastic_search_client, index)
|
7
|
+
begin
|
8
|
+
elastic_search_client.indices.delete index: index
|
9
|
+
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
10
|
+
return true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# def by_query(elastic_search_client, index, type, attributes = {})
|
15
|
+
# params = { index: index, body: attributes }
|
16
|
+
#
|
17
|
+
# params[:type] = type unless type == '_all'
|
18
|
+
#
|
19
|
+
# elastic_search_client.delete_by_query params
|
20
|
+
# end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Elasticord
|
2
|
+
module Client
|
3
|
+
module Get
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def one(elastic_search_client, index, type, id)
|
7
|
+
result = elastic_search_client.get \
|
8
|
+
index: index, type: type, ignore: 404, id: id
|
9
|
+
|
10
|
+
(result && result['found']) ? result['_source'] : false
|
11
|
+
end
|
12
|
+
|
13
|
+
# def by_query(elastic_search_client, index, type, body = {})
|
14
|
+
# data = { total_results: 0, data: [] }
|
15
|
+
# params = { index: index, type: type, body: body }
|
16
|
+
#
|
17
|
+
# begin
|
18
|
+
# results = elastic_search_client.search(params)
|
19
|
+
#
|
20
|
+
# hits = results['hits']
|
21
|
+
#
|
22
|
+
# return data if !hits || hits['total'].to_i < 1
|
23
|
+
#
|
24
|
+
# data[:data] = hits['hits'].map { |result| result['_source'] }
|
25
|
+
# data[:total_results] = hits['total']
|
26
|
+
# rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
27
|
+
# nil
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# data
|
31
|
+
# end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Elasticord
|
2
|
-
module
|
2
|
+
module Client
|
3
3
|
module Index
|
4
4
|
module_function
|
5
5
|
|
6
|
-
def refresh(index)
|
6
|
+
def refresh(elastic_search_client, index)
|
7
7
|
begin
|
8
|
-
|
8
|
+
elastic_search_client.indices.refresh index: index
|
9
9
|
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
10
10
|
return true
|
11
11
|
end
|
@@ -1,16 +1,16 @@
|
|
1
|
-
require '
|
2
|
-
require 'elasticord/entities/array_with_meta_data'
|
1
|
+
require 'elasticord/client/array_with_meta_data'
|
3
2
|
|
4
3
|
module Elasticord
|
5
|
-
module
|
4
|
+
module Client
|
6
5
|
class SearchBuilder
|
7
6
|
extend Forwardable
|
8
7
|
|
9
8
|
PAGE_DEFAULT = 1
|
10
9
|
PER_PAGE_DEFAULT = 10
|
11
10
|
|
12
|
-
def initialize(entity, index, type)
|
13
|
-
@entity, @index, @type =
|
11
|
+
def initialize(elastic_search_client, entity, index, type)
|
12
|
+
@elastic_search_client, @entity, @index, @type =
|
13
|
+
elastic_search_client, entity, index, type
|
14
14
|
end
|
15
15
|
|
16
16
|
def_delegators :results, :current_page, :per_page, :size, :from
|
@@ -71,25 +71,21 @@ module Elasticord
|
|
71
71
|
self
|
72
72
|
end
|
73
73
|
|
74
|
-
def result(*_args)
|
75
|
-
self
|
76
|
-
end
|
77
|
-
|
78
74
|
def body_params
|
79
75
|
@body_params ||= { size: size, from: from, query: { bool: {} } }
|
80
76
|
end
|
81
77
|
|
82
78
|
protected
|
83
79
|
|
84
|
-
attr_reader :
|
80
|
+
attr_reader :elastic_search_client, :entity, :index, :type
|
85
81
|
|
86
82
|
def results
|
87
|
-
@results ||=
|
83
|
+
@results ||= ArrayWithMetaData.new \
|
88
84
|
PAGE_DEFAULT, PER_PAGE_DEFAULT
|
89
85
|
end
|
90
86
|
|
91
87
|
def execute_search(body)
|
92
|
-
elastic_results =
|
88
|
+
elastic_results = elastic_search_client.search \
|
93
89
|
index: index, type: type, body: body_params.merge(body)
|
94
90
|
|
95
91
|
hits = elastic_results['hits']
|
data/lib/elasticord/version.rb
CHANGED
@@ -1,129 +1,167 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'elasticsearch'
|
3
2
|
|
4
3
|
describe Elasticord::Base, elasticsearch: true do
|
5
|
-
ElasticordChildClass1 = Class.new(described_class)
|
6
|
-
|
7
|
-
def elastic_search_client
|
8
|
-
@elastic_search_client ||= Elasticsearch::Client.new
|
9
|
-
end
|
10
|
-
|
11
|
-
def get_one_elastic_search_record(id)
|
12
|
-
result = elastic_search_client.get \
|
13
|
-
id: id,
|
14
|
-
type: ElasticordChildClass1.type,
|
15
|
-
index: ElasticordChildClass1.index,
|
16
|
-
ignore: 404
|
17
|
-
|
18
|
-
result
|
19
|
-
end
|
20
|
-
|
21
4
|
describe '.index' do
|
22
5
|
it 'described_class should return the current env' do
|
23
6
|
expect(described_class.index).to eq ENV['ENV']
|
24
7
|
end
|
25
8
|
|
26
9
|
context 'given a child class' do
|
27
|
-
|
28
|
-
|
10
|
+
let(:child_class) { Class.new(described_class) }
|
11
|
+
|
12
|
+
it 'child_class should return the same current env' do
|
13
|
+
expect(child_class.index).to eq ENV['ENV']
|
29
14
|
end
|
30
15
|
|
31
16
|
context 'given that the child class sets a new index' do
|
32
|
-
before
|
17
|
+
before do
|
18
|
+
child_class.index = 'child_class_new_index'
|
19
|
+
end
|
33
20
|
|
34
21
|
it 'described_class should return the current env' do
|
35
22
|
expect(described_class.index).to eq ENV['ENV']
|
36
23
|
end
|
37
24
|
|
38
|
-
it '
|
39
|
-
expect(
|
25
|
+
it 'child_class should return "child_class_new_index"' do
|
26
|
+
expect(child_class.index).to eq 'child_class_new_index'
|
40
27
|
end
|
41
28
|
end
|
42
29
|
end
|
43
30
|
end
|
44
31
|
|
45
32
|
describe '.type' do
|
46
|
-
it '
|
33
|
+
it 'should return "_all"' do
|
47
34
|
expect(described_class.type).to eq '_all'
|
48
35
|
end
|
49
36
|
|
50
|
-
context 'given a child class' do
|
51
|
-
|
52
|
-
|
37
|
+
context 'given a child class with a name' do
|
38
|
+
let(:child_class) do
|
39
|
+
Class.new(described_class) do
|
40
|
+
def self.name
|
41
|
+
'ChildClass'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should return that child class name' do
|
47
|
+
expect(child_class.type).to eq 'child_class'
|
53
48
|
end
|
54
49
|
end
|
55
50
|
end
|
56
51
|
|
57
|
-
describe '.
|
58
|
-
context '
|
59
|
-
let(:id) { '123' }
|
60
|
-
|
52
|
+
describe '.client' do
|
53
|
+
context 'without params' do
|
61
54
|
before do
|
62
|
-
@
|
55
|
+
@client = described_class.client
|
63
56
|
end
|
64
57
|
|
65
|
-
it '
|
66
|
-
|
58
|
+
it 'should reaturn an instance of Client::Base' do
|
59
|
+
expect(@client).to be_an_instance_of Elasticord::Client::Base
|
60
|
+
end
|
67
61
|
|
68
|
-
|
62
|
+
it 'reaturned client should have a protected #entity set up' do
|
63
|
+
expect(@client.send(:entity)).to eq described_class
|
69
64
|
end
|
70
65
|
|
71
|
-
it '
|
72
|
-
expect(@
|
66
|
+
it 'reaturned client should have a protected #index set up' do
|
67
|
+
expect(@client.send(:index)).to eq ENV['ENV']
|
73
68
|
end
|
74
69
|
|
75
|
-
it '
|
76
|
-
expect(@
|
70
|
+
it 'reaturned client should have a protected #type set up' do
|
71
|
+
expect(@client.send(:type)).to eq '_all'
|
77
72
|
end
|
78
73
|
end
|
79
74
|
|
80
|
-
context '
|
75
|
+
context 'with "project_1"' do
|
81
76
|
before do
|
82
|
-
@
|
77
|
+
@client = described_class.client 'project_1'
|
83
78
|
end
|
84
79
|
|
85
|
-
it '
|
86
|
-
|
80
|
+
it 'should reaturn an instance of Client::Base' do
|
81
|
+
expect(@client).to be_an_instance_of Elasticord::Client::Base
|
82
|
+
end
|
87
83
|
|
88
|
-
|
84
|
+
it 'reaturned client should have a protected #entity set up' do
|
85
|
+
expect(@client.send(:entity)).to eq described_class
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'reaturned client should have a protected #index set up' do
|
89
|
+
expect(@client.send(:index)).to eq ENV['ENV']
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'reaturned client should have a protected #type set up' do
|
93
|
+
expect(@client.send(:type)).to eq '_all_project_1'
|
89
94
|
end
|
90
95
|
end
|
91
96
|
end
|
92
97
|
|
93
98
|
describe '.search' do
|
94
|
-
it 'should pass all params to a
|
95
|
-
attributes = {
|
99
|
+
it 'should pass all params to a Client::Base instance' do
|
100
|
+
attributes = [{ name: 'test' }]
|
96
101
|
|
97
|
-
expect_any_instance_of(Elasticord::
|
98
|
-
receive(:
|
102
|
+
expect_any_instance_of(Elasticord::Client::Base).to \
|
103
|
+
receive(:search).with(*attributes)
|
99
104
|
|
100
|
-
|
105
|
+
described_class.search(*attributes)
|
101
106
|
end
|
102
107
|
end
|
103
108
|
|
104
109
|
describe '.get' do
|
105
|
-
|
106
|
-
|
107
|
-
result = ElasticordChildClass1.get '-1'
|
110
|
+
it 'should pass all params to a Client::Base instance' do
|
111
|
+
attributes = [1]
|
108
112
|
|
109
|
-
|
110
|
-
|
113
|
+
expect_any_instance_of(Elasticord::Client::Base).to \
|
114
|
+
receive(:get).with(*attributes)
|
115
|
+
|
116
|
+
described_class.get(*attributes)
|
111
117
|
end
|
118
|
+
end
|
112
119
|
|
113
|
-
|
114
|
-
|
115
|
-
|
120
|
+
describe '.none' do
|
121
|
+
it 'should pass all params to a Client::Base instance' do
|
122
|
+
expect_any_instance_of(Elasticord::Client::Base).to receive(:none)
|
116
123
|
|
117
|
-
|
118
|
-
|
124
|
+
described_class.none
|
125
|
+
end
|
126
|
+
end
|
119
127
|
|
120
|
-
|
121
|
-
|
122
|
-
|
128
|
+
describe '.create' do
|
129
|
+
it 'should pass all params to a Client::Base instance' do
|
130
|
+
attributes = [{ name: 'test' }]
|
123
131
|
|
124
|
-
|
125
|
-
|
126
|
-
|
132
|
+
expect_any_instance_of(Elasticord::Client::Base).to \
|
133
|
+
receive(:create).with(*attributes)
|
134
|
+
|
135
|
+
described_class.create(*attributes)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe '.delete_all' do
|
140
|
+
it 'should pass all params to a Client::Base instance' do
|
141
|
+
expect_any_instance_of(Elasticord::Client::Base).to \
|
142
|
+
receive(:delete_all)
|
143
|
+
|
144
|
+
described_class.delete_all
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe '.refresh_index' do
|
149
|
+
it 'should pass all params to a Client::Base instance' do
|
150
|
+
expect_any_instance_of(Elasticord::Client::Base).to \
|
151
|
+
receive(:refresh_index)
|
152
|
+
|
153
|
+
described_class.refresh_index
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe '.delete_by_query' do
|
158
|
+
it 'should pass all params to a Client::Base instance' do
|
159
|
+
attributes = [{ name: 'test' }]
|
160
|
+
|
161
|
+
expect_any_instance_of(Elasticord::Client::Base).to \
|
162
|
+
receive(:delete_by_query).with(*attributes)
|
163
|
+
|
164
|
+
described_class.delete_by_query(*attributes)
|
127
165
|
end
|
128
166
|
end
|
129
167
|
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticord::Client::Base, elasticsearch: true do
|
4
|
+
def get_one_elastic_search_record(id)
|
5
|
+
Elasticord.elastic_search_client.get \
|
6
|
+
id: id, type: 'child_class', index: ENV['ENV'], ignore: 404
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:described_instance) do
|
10
|
+
described_class.new \
|
11
|
+
Elasticord.elastic_search_client,
|
12
|
+
OpenStruct,
|
13
|
+
ENV['ENV'],
|
14
|
+
'child_class'
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#create' do
|
18
|
+
context 'given an ID' do
|
19
|
+
let(:id) { '123' }
|
20
|
+
|
21
|
+
before do
|
22
|
+
@child_class = described_instance.create id: id, title: 'child class'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'ElasticSearch should contain a record with that specific id' do
|
26
|
+
result = get_one_elastic_search_record id
|
27
|
+
|
28
|
+
expect(result['found']).to be true
|
29
|
+
end
|
30
|
+
|
31
|
+
it '@child_class.id should match' do
|
32
|
+
expect(@child_class.id).to eq id
|
33
|
+
end
|
34
|
+
|
35
|
+
it '@child_class should be an instance of OpenStruct' do
|
36
|
+
expect(@child_class).to be_an_instance_of OpenStruct
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'given no ID' do
|
41
|
+
before do
|
42
|
+
@child_class = described_instance.create title: 'child class'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'ElasticSearch DB should contain a record with that specific id' do
|
46
|
+
result = get_one_elastic_search_record @child_class.id
|
47
|
+
|
48
|
+
expect(result['found']).to be true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#search' do
|
54
|
+
it 'should pass all params to a SearchBuilder instance' do
|
55
|
+
attributes = { query: { match: { brand: 'oficial' } } }
|
56
|
+
|
57
|
+
expect_any_instance_of(Elasticord::Client::SearchBuilder).to \
|
58
|
+
receive(:load).with(attributes)
|
59
|
+
|
60
|
+
described_instance.search(attributes)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#get' do
|
65
|
+
context 'when the record does not exist' do
|
66
|
+
it 'should return false' do
|
67
|
+
result = described_instance.get '-1'
|
68
|
+
|
69
|
+
expect(result).to be false
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when the record exists' do
|
74
|
+
before do
|
75
|
+
@child_class = described_instance.create title: 'child class'
|
76
|
+
|
77
|
+
@result = described_instance.get @child_class.id
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should return an instance of OpenStruct' do
|
81
|
+
expect(@result).to be_an_instance_of OpenStruct
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should return the same ID it requested' do
|
85
|
+
expect(@result.id).to be @child_class.id
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#none' do
|
91
|
+
before do
|
92
|
+
@search_builder = described_instance.none
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should reaturn an instance of Client::SearchBuilder' do
|
96
|
+
expect(@search_builder).to \
|
97
|
+
be_an_instance_of Elasticord::Client::SearchBuilder
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'reaturned search_builder should have a protected #entity set up' do
|
101
|
+
expect(@search_builder.send(:entity)).to eq OpenStruct
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'reaturned search_builder should have a protected #index set up' do
|
105
|
+
expect(@search_builder.send(:index)).to eq ENV['ENV']
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'reaturned search_builder should have a protected #type set up' do
|
109
|
+
expect(@search_builder.send(:type)).to eq 'child_class'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -1,33 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'elasticsearch'
|
3
2
|
|
4
|
-
describe Elasticord::
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
ElasticordChildClass2.type
|
3
|
+
describe Elasticord::Client::SearchBuilder, elasticsearch: true do
|
4
|
+
let(:client_base) do
|
5
|
+
Elasticord::Client::Base.new \
|
6
|
+
Elasticord.elastic_search_client,
|
7
|
+
OpenStruct,
|
8
|
+
ENV['ENV'],
|
9
|
+
'child_class'
|
12
10
|
end
|
13
11
|
|
12
|
+
let(:described_instance) { client_base.none }
|
13
|
+
|
14
14
|
describe '#current_page' do
|
15
15
|
it 'should have a default value set' do
|
16
|
-
expect(
|
16
|
+
expect(described_instance.current_page).to \
|
17
17
|
be described_class::PAGE_DEFAULT
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
describe '#per_page' do
|
22
22
|
it 'should have a default value set' do
|
23
|
-
expect(
|
23
|
+
expect(described_instance.per_page).to \
|
24
24
|
be described_class::PER_PAGE_DEFAULT
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe '#body_params' do
|
29
29
|
it 'should have pagination params set' do
|
30
|
-
expect(
|
30
|
+
expect(described_instance.body_params).to match a_hash_including({
|
31
31
|
size: described_class::PER_PAGE_DEFAULT,
|
32
32
|
from: 0
|
33
33
|
})
|
@@ -35,7 +35,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
35
35
|
|
36
36
|
context 'when setting #page with 2' do
|
37
37
|
before do
|
38
|
-
@search_builder =
|
38
|
+
@search_builder = described_instance
|
39
39
|
@search_builder.page(2)
|
40
40
|
end
|
41
41
|
|
@@ -54,16 +54,9 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
describe '#result' do
|
58
|
-
it 'should return a SearchBuilder instance' do
|
59
|
-
expect(new_search_builder.result(distinct: true)).to \
|
60
|
-
be_an_instance_of described_class
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
57
|
describe '#page' do
|
65
58
|
def search_builder
|
66
|
-
@search_builder ||=
|
59
|
+
@search_builder ||= described_instance
|
67
60
|
end
|
68
61
|
|
69
62
|
context 'sending 3 as an argument' do
|
@@ -87,7 +80,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
87
80
|
|
88
81
|
describe '#per' do
|
89
82
|
def search_builder
|
90
|
-
@search_builder ||=
|
83
|
+
@search_builder ||= described_instance
|
91
84
|
end
|
92
85
|
|
93
86
|
context 'sending 15 as an argument' do
|
@@ -111,13 +104,13 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
111
104
|
|
112
105
|
context 'given that no record has been created' do
|
113
106
|
before do
|
114
|
-
|
115
|
-
|
107
|
+
client_base.delete_all
|
108
|
+
client_base.refresh_index
|
116
109
|
end
|
117
110
|
|
118
111
|
describe '#load' do
|
119
112
|
before do
|
120
|
-
@search_result =
|
113
|
+
@search_result = described_instance.load
|
121
114
|
end
|
122
115
|
|
123
116
|
it 'should return a pagination object with empty resources' do
|
@@ -129,14 +122,14 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
129
122
|
|
130
123
|
context 'given that 5 records have been created' do
|
131
124
|
before do
|
132
|
-
|
133
|
-
5.times {
|
134
|
-
|
125
|
+
client_base.delete_all
|
126
|
+
5.times { client_base.create(title: 'child class') }
|
127
|
+
client_base.refresh_index
|
135
128
|
end
|
136
129
|
|
137
130
|
describe '#load' do
|
138
131
|
before do
|
139
|
-
@search_result =
|
132
|
+
@search_result = described_instance.load
|
140
133
|
end
|
141
134
|
|
142
135
|
it 'should find 5 total_results' do
|
@@ -147,7 +140,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
147
140
|
expect(@search_result.length).to be 5
|
148
141
|
|
149
142
|
@search_result.each do |child_class|
|
150
|
-
expect(child_class).to be_an_instance_of
|
143
|
+
expect(child_class).to be_an_instance_of OpenStruct
|
151
144
|
end
|
152
145
|
end
|
153
146
|
end
|
@@ -155,26 +148,26 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
155
148
|
|
156
149
|
context 'given that 30 records have been created' do
|
157
150
|
before do
|
158
|
-
|
151
|
+
client_base.delete_all
|
159
152
|
|
160
153
|
24.times do
|
161
|
-
|
154
|
+
client_base.create \
|
162
155
|
title: 'child class', brand: 'oficial', locked: false
|
163
156
|
end
|
164
157
|
|
165
|
-
|
158
|
+
client_base.create(title: 'class', brand: 'oficial', locked: true)
|
166
159
|
|
167
160
|
5.times do
|
168
|
-
|
161
|
+
client_base.create(title: 'child', brand: 'unoficial', locked: false)
|
169
162
|
end
|
170
163
|
|
171
|
-
|
164
|
+
client_base.refresh_index
|
172
165
|
end
|
173
166
|
|
174
167
|
describe '#load' do
|
175
168
|
context 'when no pagination params are passed' do
|
176
169
|
before do
|
177
|
-
@search_result =
|
170
|
+
@search_result = described_instance.load({})
|
178
171
|
end
|
179
172
|
|
180
173
|
it 'should find 30 total_results' do
|
@@ -185,14 +178,14 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
185
178
|
expect(@search_result.length).to be 10
|
186
179
|
|
187
180
|
@search_result.each do |child_class|
|
188
|
-
expect(child_class).to be_an_instance_of
|
181
|
+
expect(child_class).to be_an_instance_of OpenStruct
|
189
182
|
end
|
190
183
|
end
|
191
184
|
end
|
192
185
|
|
193
186
|
context 'when #page(2) was called' do
|
194
187
|
before do
|
195
|
-
@search_builder =
|
188
|
+
@search_builder = described_instance.page(2)
|
196
189
|
end
|
197
190
|
|
198
191
|
context 'when #per(15) was called' do
|
@@ -207,8 +200,6 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
207
200
|
locked_eq: false
|
208
201
|
})
|
209
202
|
|
210
|
-
@search_builder = @search_builder.result(distinct: true)
|
211
|
-
|
212
203
|
@search_result = @search_builder.load
|
213
204
|
end
|
214
205
|
|
@@ -225,7 +216,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
225
216
|
|
226
217
|
context 'when single query params are passed' do
|
227
218
|
before do
|
228
|
-
@search_result =
|
219
|
+
@search_result = described_instance.load \
|
229
220
|
query: {
|
230
221
|
match: { brand: 'oficial' }
|
231
222
|
}
|
@@ -242,7 +233,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
|
|
242
233
|
|
243
234
|
context 'when multi query params are passed' do
|
244
235
|
before do
|
245
|
-
@search_result =
|
236
|
+
@search_result = described_instance.load \
|
246
237
|
query: {
|
247
238
|
bool: {
|
248
239
|
must: [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- StreetBees Dev Team
|
@@ -231,13 +231,14 @@ files:
|
|
231
231
|
- lib/dash_overlord/version.rb
|
232
232
|
- lib/elasticord.rb
|
233
233
|
- lib/elasticord/base.rb
|
234
|
+
- lib/elasticord/client/array_with_meta_data.rb
|
235
|
+
- lib/elasticord/client/base.rb
|
236
|
+
- lib/elasticord/client/create.rb
|
237
|
+
- lib/elasticord/client/delete.rb
|
238
|
+
- lib/elasticord/client/get.rb
|
239
|
+
- lib/elasticord/client/index.rb
|
240
|
+
- lib/elasticord/client/search_builder.rb
|
234
241
|
- lib/elasticord/config.rb
|
235
|
-
- lib/elasticord/entities/array_with_meta_data.rb
|
236
|
-
- lib/elasticord/orm/create.rb
|
237
|
-
- lib/elasticord/orm/delete.rb
|
238
|
-
- lib/elasticord/orm/get.rb
|
239
|
-
- lib/elasticord/orm/index.rb
|
240
|
-
- lib/elasticord/orm/search_builder.rb
|
241
242
|
- lib/elasticord/version.rb
|
242
243
|
- lib/tasks/create_dummy_data.rake
|
243
244
|
- spec/dash_overlord/use_cases/v1/admins/log_in_spec.rb
|
@@ -264,7 +265,8 @@ files:
|
|
264
265
|
- spec/dash_overlord/use_cases/v1/videos/index/elastic_search_spec.rb
|
265
266
|
- spec/dash_overlord/use_cases/v1/videos/index/postgres_spec.rb
|
266
267
|
- spec/elasticord/base_spec.rb
|
267
|
-
- spec/elasticord/
|
268
|
+
- spec/elasticord/client/base_spec.rb
|
269
|
+
- spec/elasticord/client/search_builder_spec.rb
|
268
270
|
- spec/factories/v1/admins.rb
|
269
271
|
- spec/factories/v1/bee_videos.rb
|
270
272
|
- spec/factories/v1/chart_configs.rb
|
@@ -324,7 +326,8 @@ test_files:
|
|
324
326
|
- spec/dash_overlord/use_cases/v1/videos/index/elastic_search_spec.rb
|
325
327
|
- spec/dash_overlord/use_cases/v1/videos/index/postgres_spec.rb
|
326
328
|
- spec/elasticord/base_spec.rb
|
327
|
-
- spec/elasticord/
|
329
|
+
- spec/elasticord/client/base_spec.rb
|
330
|
+
- spec/elasticord/client/search_builder_spec.rb
|
328
331
|
- spec/factories/v1/admins.rb
|
329
332
|
- spec/factories/v1/bee_videos.rb
|
330
333
|
- spec/factories/v1/chart_configs.rb
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Elasticord
|
2
|
-
module ORM
|
3
|
-
module Delete
|
4
|
-
module_function
|
5
|
-
|
6
|
-
def by_query(index, type, attributes = {})
|
7
|
-
params = { index: index, body: attributes }
|
8
|
-
|
9
|
-
params[:type] = type unless type == '_all'
|
10
|
-
|
11
|
-
Elasticord.client.delete_by_query params
|
12
|
-
end
|
13
|
-
|
14
|
-
def all(index)
|
15
|
-
begin
|
16
|
-
Elasticord.client.indices.delete index: index
|
17
|
-
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
18
|
-
return true
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/elasticord/orm/get.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
module Elasticord
|
2
|
-
module ORM
|
3
|
-
module Get
|
4
|
-
module_function
|
5
|
-
|
6
|
-
def one(index, type, id)
|
7
|
-
result = Elasticord.client.get \
|
8
|
-
index: index, type: type, ignore: 404, id: id
|
9
|
-
|
10
|
-
(!result || !result['found']) ? false : result['_source']
|
11
|
-
end
|
12
|
-
|
13
|
-
def by_query(index, type, body = {})
|
14
|
-
data = { total_results: 0, data: [] }
|
15
|
-
params = { index: index, type: type, body: body }
|
16
|
-
|
17
|
-
begin
|
18
|
-
results = Elasticord.client.search(params)
|
19
|
-
|
20
|
-
hits = results['hits']
|
21
|
-
|
22
|
-
return data if !hits || hits['total'].to_i < 1
|
23
|
-
|
24
|
-
data[:data] = hits['hits'].map { |result| result['_source'] }
|
25
|
-
data[:total_results] = hits['total']
|
26
|
-
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
27
|
-
nil
|
28
|
-
end
|
29
|
-
|
30
|
-
data
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|