elasticquery 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-style.yml +1063 -0
- data/History.md +22 -0
- data/README.md +12 -16
- data/elasticquery.gemspec +1 -0
- data/lib/elasticquery/base.rb +4 -4
- data/lib/elasticquery/builder.rb +32 -0
- data/lib/elasticquery/filters/base.rb +2 -0
- data/lib/elasticquery/filters/not.rb +8 -8
- data/lib/elasticquery/filters/range.rb +12 -6
- data/lib/elasticquery/filters/search.rb +2 -1
- data/lib/elasticquery/filters/term.rb +15 -10
- data/lib/elasticquery/query.rb +4 -12
- data/lib/elasticquery/version.rb +2 -2
- data/lib/elasticquery.rb +1 -1
- data/test/base_test.rb +3 -3
- data/test/{queries/all_test.rb → builder_test.rb} +3 -3
- data/test/filters/not_test.rb +10 -6
- data/test/filters/range_test.rb +35 -13
- data/test/filters/search_test.rb +18 -13
- data/test/filters/term_test.rb +20 -5
- data/test/integration/chainable_call_test.rb +7 -7
- data/test/integration/not_case_test.rb +17 -3
- data/test/integration/queries_inheritence_test.rb +9 -23
- data/test/integration/range_case_test.rb +18 -5
- data/test/integration/search_case_test.rb +15 -9
- data/test/integration/term_case_test.rb +21 -7
- data/test/query_test.rb +15 -6
- data/test/test_helper.rb +2 -2
- metadata +22 -6
- data/lib/elasticquery/queries/all.rb +0 -34
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "elasticquery"
|
3
3
|
|
4
4
|
class TestNotCase < MiniTest::Test
|
5
5
|
class PostQuery < Elasticquery::Base
|
@@ -8,6 +8,13 @@ class TestNotCase < MiniTest::Test
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
+
class PostRangeQuery < Elasticquery::Base
|
12
|
+
filtered do |param|
|
13
|
+
term published: true
|
14
|
+
range.not :age, lte: params[:year]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
11
18
|
class InvalidQuery < Elasticquery::Base
|
12
19
|
filtered do |param|
|
13
20
|
term.not
|
@@ -16,7 +23,7 @@ class TestNotCase < MiniTest::Test
|
|
16
23
|
|
17
24
|
def test_invalid_previous_filter
|
18
25
|
query = InvalidQuery.new({}).build
|
19
|
-
assert_equal
|
26
|
+
assert_equal Elasticquery::Query::DEFAULT, query
|
20
27
|
end
|
21
28
|
|
22
29
|
def test_term_case
|
@@ -25,4 +32,11 @@ class TestNotCase < MiniTest::Test
|
|
25
32
|
expected = [{not: {filter: {term: {id: 1}}}}]
|
26
33
|
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
27
34
|
end
|
35
|
+
|
36
|
+
def test_range_case
|
37
|
+
params = {year: 2015}
|
38
|
+
actual = PostRangeQuery.new(params).build
|
39
|
+
expected = [{term: {published: true}}, {not: {filter: {range: {age: {lte: 2015}}}}}]
|
40
|
+
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
41
|
+
end
|
28
42
|
end
|
@@ -1,37 +1,31 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "elasticquery"
|
3
3
|
|
4
4
|
class TestQueriesInheritence < MiniTest::Test
|
5
5
|
class MultipleFilteredQuery < Elasticquery::Base
|
6
6
|
filtered do |params|
|
7
|
-
term :
|
7
|
+
term :"category.id" => params[:category_id]
|
8
8
|
end
|
9
9
|
|
10
10
|
filtered do |params|
|
11
|
-
term :
|
11
|
+
term :"author.name" => params[:author_name]
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
class ParentPostQuery < Elasticquery::Base
|
16
16
|
filtered do |params|
|
17
|
-
term :
|
17
|
+
term :"category.id" => params[:category_id]
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
class ChildPostQuery < ParentPostQuery
|
22
22
|
filtered do |params|
|
23
|
-
term :
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class ChildOverrideQuery < ParentPostQuery
|
28
|
-
filtered do |params|
|
29
|
-
term :'category.id' => params[:category_id_new]
|
23
|
+
term :"author.name" => params[:author_name]
|
30
24
|
end
|
31
25
|
end
|
32
26
|
|
33
27
|
def setup
|
34
|
-
@params = {category_id: 1, author_name:
|
28
|
+
@params = {category_id: 1, author_name: "Sergey"}
|
35
29
|
end
|
36
30
|
|
37
31
|
def test_parent_subqueries_builder
|
@@ -44,21 +38,13 @@ class TestQueriesInheritence < MiniTest::Test
|
|
44
38
|
def test_child_query_builder
|
45
39
|
query = ChildPostQuery.new @params
|
46
40
|
actual = query.build
|
47
|
-
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" =>
|
41
|
+
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => "Sergey"}}]
|
48
42
|
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
49
43
|
end
|
50
44
|
|
51
45
|
def test_concatenate_all_filters
|
52
46
|
actual = MultipleFilteredQuery.new(@params).build
|
53
|
-
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" =>
|
54
|
-
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_override_same_keys
|
58
|
-
params = { category_id: 1, author_name: 'Sergey', category_id_new: 14 }
|
59
|
-
query = ChildOverrideQuery.new params
|
60
|
-
actual = query.build
|
61
|
-
expected = [{term: {:"category.id" => 14}}]
|
47
|
+
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => "Sergey"}}]
|
62
48
|
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
63
49
|
end
|
64
50
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "elasticquery"
|
3
3
|
|
4
4
|
class TestRangeCase < MiniTest::Test
|
5
5
|
class HumanQuery < Elasticquery::Base
|
6
6
|
filtered do |params|
|
7
|
-
range :year, lte: params[:max_year], gte: params[:min_year]
|
7
|
+
range :year, lte: params[:max_year], gte: params[:min_year], execution: "fielddata"
|
8
8
|
range :revenue, lte: params[:max_revenue], gte: params[:min_revenue]
|
9
9
|
end
|
10
10
|
end
|
@@ -12,14 +12,27 @@ class TestRangeCase < MiniTest::Test
|
|
12
12
|
def test_simple_range
|
13
13
|
params = { min_year: 1960, max_year: 2015 }
|
14
14
|
actual = HumanQuery.new(params).build
|
15
|
-
expected = [{range: {year:{ lte: 2015, gte: 1960}}}]
|
15
|
+
expected = [{range: {year:{ lte: 2015, gte: 1960, execution: "fielddata"}}}]
|
16
16
|
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
17
17
|
end
|
18
18
|
|
19
|
+
def test_range_blank_values_should_be_ignored
|
20
|
+
params = { min_year: 1960, max_year: 2015, max_revenue: "", min_revenue: " " }
|
21
|
+
actual = HumanQuery.new(params).build
|
22
|
+
expected = [{range: {year:{ lte: 2015, gte: 1960, execution: "fielddata"}}}]
|
23
|
+
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_empty_range
|
27
|
+
params = {}
|
28
|
+
actual = HumanQuery.new(params).build
|
29
|
+
assert_equal Elasticquery::Query::DEFAULT, actual
|
30
|
+
end
|
31
|
+
|
19
32
|
def test_multiple_ranges
|
20
33
|
params = { min_year: 1960, max_year: 2015, min_revenue: 100 }
|
21
34
|
actual = HumanQuery.new(params).build
|
22
|
-
expected = [{range: {year:{ lte: 2015, gte: 1960}}}, {range: {revenue: {gte: 100}}}]
|
35
|
+
expected = [{range: {year:{ lte: 2015, gte: 1960, execution: "fielddata"}}}, {range: {revenue: {gte: 100}}}]
|
23
36
|
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
24
37
|
end
|
25
38
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "elasticquery"
|
3
3
|
|
4
4
|
class TestSearchQueryCase < MiniTest::Test
|
5
5
|
class PostQuery < Elasticquery::Base
|
6
6
|
filtered do |params|
|
7
|
-
term :
|
7
|
+
term :"category" => params[:category]
|
8
8
|
search params[:search]
|
9
9
|
end
|
10
10
|
end
|
@@ -25,23 +25,29 @@ class TestSearchQueryCase < MiniTest::Test
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_search_with_other_query
|
28
|
-
params = {category:
|
28
|
+
params = {category: "zoo", search: "hello"}
|
29
29
|
actual = PostQuery.new(params).build
|
30
|
-
expected_term = [{term: {category:
|
30
|
+
expected_term = [{term: {category: "zoo"}}]
|
31
31
|
assert_equal expected_term, actual[:query][:filtered][:filter][:and]
|
32
|
-
expected_search = { query:
|
32
|
+
expected_search = { query: "hello", fields: "_all", operator: "and", type: "best_fields"}
|
33
33
|
assert_equal expected_search, actual[:query][:filtered][:query][:multi_match]
|
34
34
|
end
|
35
35
|
|
36
|
+
def test_empty_search
|
37
|
+
params = {search: "" , fields: %w(title body), operator: "or"}
|
38
|
+
actual = PostOptionsQuery.new(params).build
|
39
|
+
assert_equal Elasticquery::Query::DEFAULT, actual
|
40
|
+
end
|
41
|
+
|
36
42
|
def test_all_options_to_configure
|
37
|
-
params = {search:
|
43
|
+
params = {search: "hello", fields: %w(title body), operator: "or"}
|
38
44
|
actual = PostOptionsQuery.new(params).build
|
39
|
-
expected = {fields: %w(title body), operator: "or", type: "phrase", query:
|
45
|
+
expected = {fields: %w(title body), operator: "or", type: "phrase", query: "hello"}
|
40
46
|
assert_equal expected, actual[:query][:filtered][:query][:multi_match]
|
41
47
|
end
|
42
48
|
|
43
49
|
def test_multiple_search_use_last_search_declaration
|
44
|
-
params = {search:
|
50
|
+
params = {search: "hello"}
|
45
51
|
actual = MultipleSearch.new(params).build
|
46
52
|
assert_equal "look for", actual[:query][:filtered][:query][:multi_match][:query]
|
47
53
|
end
|
@@ -1,24 +1,38 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "elasticquery"
|
3
3
|
|
4
4
|
class TestTermCase < MiniTest::Test
|
5
5
|
class PostQuery < Elasticquery::Base
|
6
6
|
filtered do |params|
|
7
|
-
term :
|
7
|
+
term :"category.id" => params[:category_id]
|
8
|
+
term :"author.name" => params[:author_name], _cache: false
|
8
9
|
end
|
9
10
|
end
|
10
11
|
|
11
12
|
def test_multiple_valid_terms
|
12
|
-
params = {category_id: 1, author_name:
|
13
|
+
params = {category_id: 1, author_name: "Sergey"}
|
13
14
|
actual = PostQuery.new(params).build
|
14
|
-
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" =>
|
15
|
+
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => "Sergey", _cache: false}}]
|
15
16
|
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
16
17
|
end
|
17
18
|
|
18
19
|
def test_extra_params
|
19
|
-
params = {
|
20
|
+
params = {category_id: 1, author_name: "Sergey", a: 1}
|
20
21
|
actual = PostQuery.new(params).build
|
21
|
-
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" =>
|
22
|
+
expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => "Sergey", _cache: false}}]
|
22
23
|
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
23
24
|
end
|
25
|
+
|
26
|
+
def test_blank_values_should_be_ignored
|
27
|
+
params = {category_id: 1, author_name: ""}
|
28
|
+
actual = PostQuery.new(params).build
|
29
|
+
expected = [{term: {:"category.id" => 1}}]
|
30
|
+
assert_equal expected, actual[:query][:filtered][:filter][:and]
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_empty_params
|
34
|
+
params = {}
|
35
|
+
actual = PostQuery.new(params).build
|
36
|
+
assert_equal Elasticquery::Query::DEFAULT, actual
|
37
|
+
end
|
24
38
|
end
|
data/test/query_test.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "elasticquery/query"
|
3
3
|
|
4
4
|
class TestQuery < MiniTest::Test
|
5
5
|
|
@@ -46,13 +46,22 @@ class TestQuery < MiniTest::Test
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_should_group_filters_in_array
|
49
|
-
@rule.expect :to_hash, query: {filtered: {filter: {and: {term: {a: 1,
|
49
|
+
@rule.expect :to_hash, query: {filtered: {filter: {and: [{term: {a: 1, _cache: true}}]}}}
|
50
50
|
@query << @rule
|
51
|
-
@rule.expect :to_hash, query: {filtered: {filter: {and: {term: {c: 3}}}}}
|
51
|
+
@rule.expect :to_hash, query: {filtered: {filter: {and: [{term: {c: 3}}]}}}
|
52
52
|
@query << @rule
|
53
|
-
@rule.expect :to_hash, query: {filtered: {filter: {and: {range: {x: 3}}}}}
|
53
|
+
@rule.expect :to_hash, query: {filtered: {filter: {and: [{range: {x: 3}}]}}}
|
54
54
|
@query << @rule
|
55
|
-
expected = {query: {filtered: {filter: {and: [{term: {a: 1
|
55
|
+
expected = {query: {filtered: {filter: {and: [{term: {a: 1, _cache: true}}, {term: {c: 3}}, {range: {x: 3}}]}}}}
|
56
|
+
assert_equal(expected, @query.to_hash)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_should_group_multiple_terms_conditions
|
60
|
+
@rule.expect :to_hash, query: {filtered: {filter: {and: [{terms: {a: %w(a b c)}}]}}}
|
61
|
+
@query << @rule
|
62
|
+
@rule.expect :to_hash, query: {filtered: {filter: {and: [{terms: {c: %w(x y z), execution: "bool"}}]}}}
|
63
|
+
@query << @rule
|
64
|
+
expected = {query: {filtered: {filter: {and: [{terms: {a: %w(a b c)}}, {terms: {c: %w(x y z), execution: "bool"}}]}}}}
|
56
65
|
assert_equal(expected, @query.to_hash)
|
57
66
|
end
|
58
67
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticquery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Kuchmistov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: deep_merge
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,23 +130,26 @@ extensions: []
|
|
116
130
|
extra_rdoc_files: []
|
117
131
|
files:
|
118
132
|
- ".gitignore"
|
133
|
+
- ".ruby-style.yml"
|
119
134
|
- ".travis.yml"
|
120
135
|
- Gemfile
|
136
|
+
- History.md
|
121
137
|
- LICENSE.txt
|
122
138
|
- README.md
|
123
139
|
- Rakefile
|
124
140
|
- elasticquery.gemspec
|
125
141
|
- lib/elasticquery.rb
|
126
142
|
- lib/elasticquery/base.rb
|
143
|
+
- lib/elasticquery/builder.rb
|
127
144
|
- lib/elasticquery/filters/base.rb
|
128
145
|
- lib/elasticquery/filters/not.rb
|
129
146
|
- lib/elasticquery/filters/range.rb
|
130
147
|
- lib/elasticquery/filters/search.rb
|
131
148
|
- lib/elasticquery/filters/term.rb
|
132
|
-
- lib/elasticquery/queries/all.rb
|
133
149
|
- lib/elasticquery/query.rb
|
134
150
|
- lib/elasticquery/version.rb
|
135
151
|
- test/base_test.rb
|
152
|
+
- test/builder_test.rb
|
136
153
|
- test/filters/not_test.rb
|
137
154
|
- test/filters/range_test.rb
|
138
155
|
- test/filters/search_test.rb
|
@@ -143,7 +160,6 @@ files:
|
|
143
160
|
- test/integration/range_case_test.rb
|
144
161
|
- test/integration/search_case_test.rb
|
145
162
|
- test/integration/term_case_test.rb
|
146
|
-
- test/queries/all_test.rb
|
147
163
|
- test/query_test.rb
|
148
164
|
- test/test_helper.rb
|
149
165
|
homepage: https://github.com/caulfield/elasticquery
|
@@ -166,12 +182,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
182
|
version: '0'
|
167
183
|
requirements: []
|
168
184
|
rubyforge_project:
|
169
|
-
rubygems_version: 2.
|
185
|
+
rubygems_version: 2.2.2
|
170
186
|
signing_key:
|
171
187
|
specification_version: 4
|
172
188
|
summary: Elasticsearch query builder.
|
173
189
|
test_files:
|
174
190
|
- test/base_test.rb
|
191
|
+
- test/builder_test.rb
|
175
192
|
- test/filters/not_test.rb
|
176
193
|
- test/filters/range_test.rb
|
177
194
|
- test/filters/search_test.rb
|
@@ -182,7 +199,6 @@ test_files:
|
|
182
199
|
- test/integration/range_case_test.rb
|
183
200
|
- test/integration/search_case_test.rb
|
184
201
|
- test/integration/term_case_test.rb
|
185
|
-
- test/queries/all_test.rb
|
186
202
|
- test/query_test.rb
|
187
203
|
- test/test_helper.rb
|
188
204
|
has_rdoc:
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'elasticquery/filters/term'
|
2
|
-
require 'elasticquery/filters/search'
|
3
|
-
require 'elasticquery/filters/not'
|
4
|
-
require 'elasticquery/filters/range'
|
5
|
-
|
6
|
-
require 'active_support/concern'
|
7
|
-
require 'active_support/core_ext/string/inflections'
|
8
|
-
|
9
|
-
module Elasticquery
|
10
|
-
module Queries
|
11
|
-
module All
|
12
|
-
extend ActiveSupport::Concern
|
13
|
-
|
14
|
-
filters = [
|
15
|
-
Filters::Term,
|
16
|
-
Filters::Search,
|
17
|
-
Filters::Not,
|
18
|
-
Filters::Range
|
19
|
-
]
|
20
|
-
|
21
|
-
included do
|
22
|
-
filters.each do |filter_class|
|
23
|
-
filter_name = filter_class.to_s.split("::").last.underscore
|
24
|
-
|
25
|
-
define_method filter_name do |*args|
|
26
|
-
filter = filter_class.new *args
|
27
|
-
query << filter
|
28
|
-
self
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|