elastics 0.4.0 → 0.5.0
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/README.md +1 -1
- data/lib/elastics/active_record/helper_methods.rb +9 -12
- data/lib/elastics/active_record/search_result.rb +2 -2
- data/lib/elastics/model/helper_methods.rb +32 -1
- data/lib/elastics/query_helper.rb +13 -1
- data/lib/elastics/tasks/mappings.rb +25 -1
- data/lib/elastics/version.rb +1 -1
- data/lib/tasks/elastics.rake +5 -5
- 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: 4414829781480b6aaf6c1fa14bf0450a0e164906
|
4
|
+
data.tar.gz: c63d19ed109fae4244b4c9427814c4ffa1f10de6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43f6b27708081b51906e6768edb32cd54243974582c0beae93a2030f3b605d62e8f5c4a245c9b5dc8fa6a7de0e0590756404bfb748961ad66e513dc99bcb7bda
|
7
|
+
data.tar.gz: c02fc34b15851945a8b5da43e93326062c903383dca4a7a8e21134c8d199f92a1918bebcb2f71e32f9c08046c3e8520e97765c5045b3d52682788be9dd97c321
|
data/README.md
CHANGED
@@ -171,7 +171,7 @@ creates index with settings for each file from `indices` folder.
|
|
171
171
|
- `rake elastics:migrate` (`.migrate`)
|
172
172
|
puts mappings from `mappings` folder.
|
173
173
|
|
174
|
-
- `rake elastics:migrate
|
174
|
+
- `rake elastics:migrate!` (`.migrate!`)
|
175
175
|
performs full migration.
|
176
176
|
|
177
177
|
- `rake elastics:reindex` (`.reindex`)
|
@@ -10,26 +10,23 @@ module Elastics
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
+
# Performs `_search` request on type and instantiates result object.
|
14
|
+
# SearchResult is a default result class. It can be overriden with
|
15
|
+
# :result_class option.
|
13
16
|
def search_elastics(data = {}, options = {})
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
}
|
18
|
-
if routing = options[:routing]
|
19
|
-
request[:query] = {routing: routing}
|
20
|
-
end
|
21
|
-
SearchResult.new self, request_elastics(request), options
|
17
|
+
options[:result_class] ||= SearchResult
|
18
|
+
options[:model] = self
|
19
|
+
super
|
22
20
|
end
|
23
21
|
|
22
|
+
# Finds items by ids and returns array in the order in which ids were given.
|
23
|
+
# Every missing record is replaced with `nil` in the result.
|
24
24
|
def find_all_ordered(ids)
|
25
25
|
items_by_id = where(id: ids).index_by(&:id)
|
26
26
|
ids.map { |i| items_by_id[i] }
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
request_elastics(method: :get, id: :_mapping)
|
31
|
-
end
|
32
|
-
|
29
|
+
# Indexes all records in current scope.
|
33
30
|
def index_all_elastics(*args)
|
34
31
|
find_in_batches(*args) do |batch|
|
35
32
|
index_batch_elastics(batch)
|
@@ -21,18 +21,37 @@ module Elastics
|
|
21
21
|
}
|
22
22
|
end
|
23
23
|
|
24
|
+
# Proxies #request method to elastics client with specified index & type.
|
24
25
|
def request_elastics(params)
|
25
26
|
elastics.request(elastics_params.merge!(params))
|
26
27
|
end
|
27
28
|
|
29
|
+
# Proxies #bulk method to elastics client with specified index & type.
|
28
30
|
def bulk_elastics(params = {}, &block)
|
29
31
|
elastics.bulk(elastics_params.merge!(params), &block)
|
30
32
|
end
|
31
33
|
|
34
|
+
# Performs `_search` request on type and instantiates result object.
|
35
|
+
# Result::Search is a default result class. It can be overriden with
|
36
|
+
# :result_class option.
|
37
|
+
def search_elastics(data = {}, options = {})
|
38
|
+
request = {
|
39
|
+
id: :_search,
|
40
|
+
body: data,
|
41
|
+
}
|
42
|
+
if routing = options[:routing]
|
43
|
+
request[:query] = {routing: routing}
|
44
|
+
end
|
45
|
+
result_class = options[:result_class] || Result::Search
|
46
|
+
result_class.new request_elastics(request), options
|
47
|
+
end
|
48
|
+
|
49
|
+
# Performs `_refresh` request on index.
|
32
50
|
def refresh_elastics
|
33
51
|
request_elastics(method: :post, type: nil, id: :_refresh)
|
34
52
|
end
|
35
53
|
|
54
|
+
# Indexes given records using batch API.
|
36
55
|
def index_batch_elastics(batch)
|
37
56
|
bulk_elastics do |bulk|
|
38
57
|
batch.each do |record|
|
@@ -41,8 +60,20 @@ module Elastics
|
|
41
60
|
end
|
42
61
|
end
|
43
62
|
|
63
|
+
# Reindexes all records. It requires #find_in_batches method to be defined.
|
44
64
|
def reindex_elastics(options = {})
|
45
|
-
|
65
|
+
find_in_batches(options) do |batch|
|
66
|
+
index_batch_elastics(batch)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Deletes all records in type keeping its mapping using "Delete by query" API.
|
71
|
+
def clear_elastics
|
72
|
+
request_elastics method: :delete, id: :_query, body: {query: {match_all: {}}}
|
73
|
+
end
|
74
|
+
|
75
|
+
def elastics_mapping
|
76
|
+
request_elastics(id: :_mapping)
|
46
77
|
end
|
47
78
|
end
|
48
79
|
|
@@ -1,11 +1,15 @@
|
|
1
1
|
module Elastics
|
2
2
|
module QueryHelper
|
3
|
+
# Combines multiple filters into `and` filter. Returns unmodified input
|
4
|
+
# unless it was an array.
|
3
5
|
def normalize_filters(filters)
|
4
6
|
return filters unless filters.is_a?(Array)
|
5
7
|
return filters[0] if 2 > filters.size
|
6
8
|
{and: {filters: filters}}
|
7
9
|
end
|
8
10
|
|
11
|
+
# Wraps given query into `filtered` query if filter is present.
|
12
|
+
# Also replaces empty query with `match_all`.
|
9
13
|
def normalize_query(query, filters)
|
10
14
|
filter = normalize_filters filters
|
11
15
|
query ||= {match_all: {}}
|
@@ -16,12 +20,20 @@ module Elastics
|
|
16
20
|
}}
|
17
21
|
end
|
18
22
|
|
23
|
+
# Returns `term`(for scalar value) or `terms` (for array) query node
|
24
|
+
# for specified field.
|
19
25
|
def terms_query(field, val, options = {})
|
20
26
|
if val.is_a?(Array)
|
21
27
|
{terms: {field => val}.merge(options)}
|
22
28
|
else
|
23
|
-
|
29
|
+
{term: {field => val}}
|
24
30
|
end
|
25
31
|
end
|
32
|
+
|
33
|
+
# Returns `nil` if falsy value or empty array is given. Other way
|
34
|
+
# it returns term(s) query for it.
|
35
|
+
def terms_array_query(field, val, options = {})
|
36
|
+
terms_query(field, val, options) if val && (!val.is_a?(Array) || val.any?)
|
37
|
+
end
|
26
38
|
end
|
27
39
|
end
|
@@ -26,10 +26,34 @@ module Elastics
|
|
26
26
|
flatten.sort.
|
27
27
|
each_with_object({}) do |file, hash|
|
28
28
|
name = File.basename file, '.yml'
|
29
|
-
hash[name] = YAML.load_file(file)
|
29
|
+
hash[name] = fix_mapping(name, YAML.load_file(file))
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
# Adds missing type name in the top-level and updates properties definition.
|
34
|
+
# It allows to write
|
35
|
+
#
|
36
|
+
# properties:
|
37
|
+
# name: string
|
38
|
+
# project_id: integer
|
39
|
+
#
|
40
|
+
# instead of
|
41
|
+
#
|
42
|
+
# task:
|
43
|
+
# properties:
|
44
|
+
# name:
|
45
|
+
# type: string
|
46
|
+
# project_id:
|
47
|
+
# type: integer
|
48
|
+
def fix_mapping(name, mapping)
|
49
|
+
mapping = {name => mapping} unless mapping.keys == [name]
|
50
|
+
properties = mapping[name]['properties']
|
51
|
+
properties && properties.each do |field, val|
|
52
|
+
properties[field] = {type: val} if val.is_a?(String)
|
53
|
+
end
|
54
|
+
mapping
|
55
|
+
end
|
56
|
+
|
33
57
|
def types
|
34
58
|
@types ||= mappings.keys
|
35
59
|
end
|
data/lib/elastics/version.rb
CHANGED
data/lib/tasks/elastics.rake
CHANGED
@@ -29,11 +29,11 @@ namespace 'elastics' do
|
|
29
29
|
|
30
30
|
desc 'Creates indices and applies mappings. Full migration when param is present'
|
31
31
|
task migrate: :load_config do
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
Elastics::Tasks.migrate @elastics_options
|
33
|
+
end
|
34
|
+
|
35
|
+
task migrate!: :load_config do
|
36
|
+
Elastics::Tasks.migrate! @elastics_options
|
37
37
|
end
|
38
38
|
|
39
39
|
desc 'Reindex'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Melentiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|