elastify 0.2.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0776e84590a5077aede923f0523f3565590174ee
4
- data.tar.gz: db4c2697ee68585beafa19121411e802ccddb3eb
3
+ metadata.gz: 9390c61d979851afb2b3ad0fbbdfc7f101ee60e7
4
+ data.tar.gz: f7cb608d84f1f8be941b3f31ede57a22f354e628
5
5
  SHA512:
6
- metadata.gz: 2187b6b1b38cb00326579c023c7e0c678cb87fb88b892315eaa961c7868ae221681aff0ca9a9796a03e2bfa0043233f990dff85043d68608b61e7e66ad027a14
7
- data.tar.gz: f34de86be910ce647fd68ee355cdc4fb12ce659d8713182bd0587a84707875171e62295fd1a3a55a80708faf429695a528b415cdbf78b9e9a062e605b7d4cbb8
6
+ metadata.gz: 2fce9c0bd9ff7830fa362ade0403963580a5428c2bc6a964178fa01756c84b995112df8cc0adc99aee56378c02af9aaf335ea1add2188a2bb739492a1a531aeb
7
+ data.tar.gz: 752e3f93e04c8624eec5ef23f73af6fa2c06e1485e6de0988ca53bdd027bd3b3522cfa88c873153d826087998f44be28fbe00c3385c783937449c8ae53765561
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /.idea/
@@ -22,14 +22,15 @@ module Elastify::ActiveRecordExtensions
22
22
  self.elastify_options[:map] = config.opt_mapping if config.opt_mapping.present?
23
23
  self.elastify_options[:decode] = config.opt_decode if config.opt_decode.present?
24
24
  self.elastify_options[:encode] = config.opt_encode if config.opt_encode.present?
25
+ self.elastify_options[:scroll_timeout] = config.opt_scroll_timeout if config.opt_scroll_timeout.present?
25
26
  end
26
27
 
27
- def elastify_search(dsl: nil, scroll_timer: "1m")
28
- return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).search(dsl, scroll_timer)
28
+ def elastify_search(dsl: nil, scroll_timeout: nil)
29
+ return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).search(dsl, scroll_timeout)
29
30
  end
30
31
 
31
- def elastify_scroll(scroll_id: nil, scroll_timer: "1m")
32
- return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).scroll(scroll_id, scroll_timer)
32
+ def elastify_scroll(scroll_id: nil, scroll_timeout: nil)
33
+ return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).scroll(scroll_id, scroll_timeout)
33
34
  end
34
35
  end
35
36
 
@@ -2,7 +2,7 @@ module Elastify
2
2
  module Configurators
3
3
  class Model
4
4
 
5
- attr_accessor :opt_index, :opt_type, :opt_mapping, :opt_encode, :opt_decode
5
+ attr_accessor :opt_index, :opt_type, :opt_mapping, :opt_encode, :opt_decode, :opt_scroll_timeout
6
6
 
7
7
  def index(index)
8
8
  @opt_index = index
@@ -23,6 +23,10 @@ module Elastify
23
23
  def decode(&block)
24
24
  @opt_decode = block if block_given?
25
25
  end
26
+
27
+ def scroll_timeout(scroll_timeout)
28
+ @opt_scroll_timeout = scroll_timeout
29
+ end
26
30
  end
27
31
  end
28
32
  end
@@ -2,6 +2,7 @@ module Elastify
2
2
  module Helpers
3
3
  module ElasticSearch
4
4
  class Connector
5
+
5
6
  def self.create(options, data)
6
7
  if data.blank?
7
8
  raise :elastify__create__required_data
@@ -10,9 +11,10 @@ module Elastify
10
11
  raise :elastify__create__required_data_id
11
12
  end
12
13
  url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
13
- response = JSON.parse(RestClient.put(url, data.to_json, {}))
14
+ JSON.parse(RestClient.put(url, data.to_json, {}))
14
15
  end
15
- def self.update options, data
16
+
17
+ def self.update(options, data)
16
18
  if data.blank?
17
19
  raise :elastify__update__required_data
18
20
  end
@@ -20,9 +22,10 @@ module Elastify
20
22
  raise :elastify__update__required_data_id
21
23
  end
22
24
  url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
23
- response = JSON.parse(RestClient.put(url, data.to_json, {})).to_hash
25
+ JSON.parse(RestClient.put(url, data.to_json, {})).to_hash
24
26
  end
25
- def self.destroy options, data
27
+
28
+ def self.destroy(options, data)
26
29
  if data.blank?
27
30
  raise :elastify__delete__required_data
28
31
  end
@@ -30,38 +33,44 @@ module Elastify
30
33
  raise :elastify__delete__required_data_id
31
34
  end
32
35
  url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
33
- response = JSON.parse(RestClient.delete(url)).to_hash
36
+ JSON.parse(RestClient.delete(url)).to_hash
34
37
  end
35
- def self.search options, dsl, scroll_timer
38
+
39
+ def self.search(options, dsl, scroll_timeout)
36
40
  if dsl.blank?
37
41
  raise :elastify__search__required_dsl
38
42
  end
39
43
  url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/_search"
40
- url += "?scroll=#{scroll_timer}" if scroll_timer.present?
41
- puts url
42
- response = Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
44
+ scroll_timeout ||= options[:scroll_timeout]
45
+ url += "?scroll=#{scroll_timeout}" if scroll_timeout.present?
46
+ Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
43
47
  end
44
- def self.scroll options, scroll_id, scroll_timer
48
+
49
+ def self.scroll(options, scroll_id, scroll_timeout)
45
50
  if scroll_id.blank?
46
51
  raise :elastify__search__required_scroll_id
47
52
  end
48
53
  url = "#{options[:base_url]}/_search/scroll"
49
- dsl = { scroll: scroll_timer, scroll_id: scroll_id }
50
- puts dsl.to_json
51
- response = Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
54
+ dsl = { scroll_id: scroll_id }
55
+ scroll_timeout ||= options[:scroll_timeout]
56
+ dsl[:scroll] = scroll_timeout if scroll_timeout.present?
57
+ Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
52
58
  end
53
- def self.create_index options
59
+
60
+ def self.create_index(options)
54
61
  url = "#{options[:base_url]}/#{options[:index]}"
55
- response = JSON.parse(RestClient.put(url, {}.to_json, {})).to_hash
62
+ JSON.parse(RestClient.put(url, {}.to_json, {})).to_hash
56
63
  end
57
- def self.destroy_index options
64
+
65
+ def self.destroy_index(options)
58
66
  url = "#{options[:base_url]}/#{options[:index]}"
59
- response = JSON.parse(RestClient.delete(url)).to_hash
67
+ JSON.parse(RestClient.delete(url)).to_hash
60
68
  end
61
- def self.create_mapping options
69
+
70
+ def self.create_mapping(options)
62
71
  url = "#{options[:base_url]}/#{options[:index]}/_mappings/#{options[:type]}"
63
72
  puts options[:map]
64
- response = JSON.parse(RestClient.put(url, options[:map].squish, {})).to_hash
73
+ JSON.parse(RestClient.put(url, options[:map].squish, {})).to_hash
65
74
  end
66
75
  end
67
76
  end
@@ -15,11 +15,11 @@ module Elastify
15
15
  def destroy(model)
16
16
  Connector.destroy(@options, model)
17
17
  end
18
- def search(dsl, scroll_timer = nil)
19
- Connector.search(@options, dsl, scroll_timer)
18
+ def search(dsl, scroll_timeout = nil)
19
+ Connector.search(@options, dsl, scroll_timeout)
20
20
  end
21
- def scroll(scroll_id, scroll_timer = nil)
22
- Connector.scroll(@options, scroll_id, scroll_timer)
21
+ def scroll(scroll_id, scroll_timeout = nil)
22
+ Connector.scroll(@options, scroll_id, scroll_timeout)
23
23
  end
24
24
  end
25
25
  end
@@ -0,0 +1,106 @@
1
+ module Elastify
2
+ module Helpers
3
+ class QueryBuilder
4
+
5
+ attr_accessor :queries, :filters, :sort, :scroll_size, :scroll_timeout
6
+
7
+ def initialize(scroll_size: 15, scroll_timeout: '1m')
8
+ @queries = []
9
+ @filters = []
10
+ @sort = {}
11
+ @scroll_size = scroll_size
12
+ @scroll_timeout = scroll_timeout
13
+ end
14
+
15
+ def search_term(fields, term)
16
+ @queries << { term: term, fields: fields.instance_of?(Array) ? fields : [fields] }
17
+ end
18
+
19
+ def filter(*args)
20
+ must_filter(*args)
21
+ end
22
+
23
+ def must_filter(fields, field_rules)
24
+ @filters << { type: :must, field_rules: field_rules, fields: fields.instance_of?(Array) ? fields : [fields] }
25
+ end
26
+
27
+ def should_filter(fields, field_rules)
28
+ @filters << { type: :should, field_rules: field_rules, fields: fields.instance_of?(Array) ? fields : [fields] }
29
+ end
30
+
31
+ def sort(field, direction)
32
+ @sort[field] = { order: direction }
33
+ end
34
+
35
+ def scroll(scroll_size, scroll_timeout)
36
+ @scroll_size = scroll_size
37
+ @scroll_timeout = scroll_timeout
38
+ end
39
+
40
+ def scroll_size(size)
41
+ @scroll_size = size
42
+ end
43
+
44
+ def scroll_timeout(timeout)
45
+ @scroll_timeout = timeout
46
+ end
47
+
48
+ def to_dsl
49
+ max_scroll_size = 100; @scroll_size ||= 15
50
+ @scroll_size = @scroll_size.to_i <= max_scroll_size ? @scroll_size : max_scroll_size
51
+ if @sort.blank?
52
+ @sort = @queries.blank? ? { id: { order: :desc } } : { _score: { order: :desc } }
53
+ end
54
+ dsl = { sort: @sort, size: @scroll_size, query: {} }
55
+ if @queries.blank? and @filters.blank?
56
+ dsl[:query][:match_all] = {}
57
+ return dsl
58
+ end
59
+ dsl[:query] = { bool: { must: [], must_not: [], should: [] } }
60
+ if @queries.present?
61
+ dsl[:query][:bool][:must] += @queries.map do |query|
62
+ { multi_match: { query: "#{query[:term].sanitize}", fields: query[:fields], operator: :and } }
63
+ end
64
+ end
65
+ if @filters.present?
66
+ @filters.select { |filter| filter[:type].equal?(:must) }.each do |filter|
67
+ filter[:fields].each do |field|
68
+ dsl[:query][:bool][:must] += map_has(field, filter[:field_rules][:has]) if filter[:field_rules][:has].present?
69
+ dsl[:query][:bool][:must] << map_in(field, filter[:field_rules][:in]) if filter[:field_rules][:in].present?
70
+ dsl[:query][:bool][:must_not] << map_in(field, filter[:field_rules][:not_in]) if filter[:field_rules][:not_in].present?
71
+ dsl[:query][:bool][:must] << map_range(field, filter[:field_rules][:range]) if filter[:field_rules][:range].present?
72
+ end
73
+ end
74
+ @filters.select { |filter| filter[:type].equal?(:should) }.each do |filter|
75
+ filter[:fields].each_with_index do |field, index|
76
+ dsl[:query][:bool][:should][index] ||= { bool: { must: [], must_not: [] } }
77
+ dsl[:query][:bool][:should][index][:bool][:must] += map_has(field, filter[:field_rules][:has]) if filter[:field_rules][:has].present?
78
+ dsl[:query][:bool][:should][index][:bool][:must] << map_in(field, filter[:field_rules][:in]) if filter[:field_rules][:in].present?
79
+ dsl[:query][:bool][:should][index][:bool][:must_not] << map_in(field, filter[:field_rules][:not_in]) if filter[:field_rules][:not_in].present?
80
+ dsl[:query][:bool][:should][index][:bool][:must] << map_range(field, filter[:field_rules][:range]) if filter[:field_rules][:range].present?
81
+ end
82
+ end
83
+ dsl[:query][:bool][:minimum_number_should_match] = 1 if dsl[:query][:bool][:should].present?
84
+ end
85
+ return dsl
86
+ end
87
+
88
+ private
89
+ def map_has(field, data)
90
+ data.map { |value| { term: { field.to_s => value } } }
91
+ end
92
+
93
+ def map_in(field, data)
94
+ { terms: { field.to_s => data } }
95
+ end
96
+
97
+ def map_not_in(field, data)
98
+ { terms: { field.to_s => data } }
99
+ end
100
+
101
+ def map_range(field, data)
102
+ { range: { field.to_s => { gte: data.first, lte: data.last, format: 'yyyy-MM-dd\'T\'HH:mm:ssZ' } } }
103
+ end
104
+ end
105
+ end
106
+ end
@@ -1,3 +1,3 @@
1
1
  module Elastify
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Bortolotti Ribeiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-23 00:00:00.000000000 Z
11
+ date: 2017-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -182,6 +182,7 @@ files:
182
182
  - lib/elastify/helpers/elastic_search/document.rb
183
183
  - lib/elastify/helpers/elastic_search/search_result.rb
184
184
  - lib/elastify/helpers/elastic_search/search_result_collection.rb
185
+ - lib/elastify/helpers/query_builder.rb
185
186
  - lib/elastify/version.rb
186
187
  homepage: https://github.com/brunobortolotti/elastify.git
187
188
  licenses: