Class: ElasticSearchFramework::Query
- Inherits:
-
Object
- Object
- ElasticSearchFramework::Query
show all
- Defined in:
- lib/elastic_search_framework/query.rb
Defined Under Namespace
Classes: InvalidQueryError
Instance Method Summary
collapse
Constructor Details
#initialize(index:) ⇒ Query
Returns a new instance of Query.
10
11
12
13
|
# File 'lib/elastic_search_framework/query.rb', line 10
def initialize(index:)
@index = index
@parts = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
15
16
17
18
|
# File 'lib/elastic_search_framework/query.rb', line 15
def method_missing(name)
@parts << { type: :field, value: name }
self
end
|
Instance Method Details
#and ⇒ Object
70
71
72
73
|
# File 'lib/elastic_search_framework/query.rb', line 70
def and
@parts << { type: :and }
self
end
|
#build ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/elastic_search_framework/query.rb', line 86
def build
@expression_string = ''
@parts.each do |p|
case p[:type]
when :field
@expression_string += ' ' + p[:value].to_s
when :condition
@expression_string += p[:expression].to_s + p[:value].to_s
when :exists
@expression_string += ' _exists_:' + p[:field].to_s
when :not_eq
@expression_string += ' NOT (' + p[:field].to_s + ':' + p[:value].to_s + ')'
when :and
@expression_string += ' AND'
when :or
@expression_string += ' OR'
else
raise 'Invalid query part'
end
end
return @expression_string.strip
end
|
#condition(expression:, value:) ⇒ Object
111
112
113
|
# File 'lib/elastic_search_framework/query.rb', line 111
def condition(expression:, value:)
@parts << { type: :condition, expression: expression, value: value }
end
|
#contains?(value) ⇒ Boolean
25
26
27
28
|
# File 'lib/elastic_search_framework/query.rb', line 25
def contains?(value)
condition(expression: ':', value: "*#{value}*")
self
end
|
#eq(value) ⇒ Object
20
21
22
23
|
# File 'lib/elastic_search_framework/query.rb', line 20
def eq(value)
condition(expression: ':', value: value)
self
end
|
#execute(limit: 10, count: false) ⇒ Object
80
81
82
83
84
|
# File 'lib/elastic_search_framework/query.rb', line 80
def execute(limit: 10, count: false)
query = build
repository = ElasticSearchFramework::Repository.new
repository.query(index: @index, expression: query, limit: limit, count: count)
end
|
#exists? ⇒ Boolean
60
61
62
63
64
65
66
67
68
|
# File 'lib/elastic_search_framework/query.rb', line 60
def exists?
field = @parts.last
unless field[:type] == :field
raise ::InvalidQueryError.new('The exists? query part can only be chained to a field.')
end
@parts.pop
@parts << { type: :exists?, field: field[:value] }
self
end
|
#gt(value) ⇒ Object
40
41
42
43
|
# File 'lib/elastic_search_framework/query.rb', line 40
def gt(value)
condition(expression: ':>', value: value)
self
end
|
#gt_eq(value) ⇒ Object
45
46
47
48
|
# File 'lib/elastic_search_framework/query.rb', line 45
def gt_eq(value)
condition(expression: ':>=', value: value)
self
end
|
#lt(value) ⇒ Object
50
51
52
53
|
# File 'lib/elastic_search_framework/query.rb', line 50
def lt(value)
condition(expression: ':<', value: value)
self
end
|
#lt_eq(value) ⇒ Object
55
56
57
58
|
# File 'lib/elastic_search_framework/query.rb', line 55
def lt_eq(value)
condition(expression: ':<=', value: value)
self
end
|
#not_eq(value) ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/elastic_search_framework/query.rb', line 30
def not_eq(value)
field = @parts.last
unless field[:type] == :field
raise ::InvalidQueryError.new('The not_eq query part can only be chained to a field.')
end
@parts.pop
@parts << { type: :not_eq, field: field[:value], value: value }
self
end
|
#or ⇒ Object
75
76
77
78
|
# File 'lib/elastic_search_framework/query.rb', line 75
def or
@parts << { type: :or }
self
end
|