elasticquery 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +12 -0
  3. data/README.md +151 -70
  4. data/Rakefile +1 -9
  5. data/elasticquery.gemspec +2 -2
  6. data/lib/elasticquery.rb +1 -0
  7. data/lib/elasticquery/base.rb +6 -48
  8. data/lib/elasticquery/builder.rb +74 -17
  9. data/lib/elasticquery/es.rb +32 -0
  10. data/lib/elasticquery/filters/base.rb +4 -0
  11. data/lib/elasticquery/filters/exists.rb +24 -0
  12. data/lib/elasticquery/filters/not.rb +7 -11
  13. data/lib/elasticquery/filters/range.rb +3 -7
  14. data/lib/elasticquery/filters/term.rb +4 -28
  15. data/lib/elasticquery/filters/terms.rb +23 -0
  16. data/lib/elasticquery/queries/base.rb +16 -0
  17. data/lib/elasticquery/queries/multi_match.rb +34 -0
  18. data/lib/elasticquery/query.rb +25 -36
  19. data/lib/elasticquery/version.rb +1 -1
  20. data/test/base_test.rb +4 -4
  21. data/test/builder_test.rb +24 -8
  22. data/test/es_test.rb +23 -0
  23. data/test/filters/exists_test.rb +30 -0
  24. data/test/filters/not_test.rb +23 -10
  25. data/test/filters/range_test.rb +10 -11
  26. data/test/filters/term_test.rb +6 -7
  27. data/test/filters/terms_test.rb +50 -0
  28. data/test/integration/chainable_call_test.rb +6 -6
  29. data/test/integration/exists_case_test.rb +56 -0
  30. data/test/integration/not_case_test.rb +11 -5
  31. data/test/integration/queries_inheritence_test.rb +12 -4
  32. data/test/integration/range_case_test.rb +4 -2
  33. data/test/integration/search_case_test.rb +20 -11
  34. data/test/integration/term_case_test.rb +4 -2
  35. data/test/integration/terms_case_test.rb +49 -0
  36. data/test/queries/multi_match_test.rb +49 -0
  37. data/test/query_test.rb +40 -40
  38. metadata +26 -27
  39. data/lib/elasticquery/filters/search.rb +0 -54
  40. data/test/filters/search_test.rb +0 -62
@@ -4,23 +4,31 @@ require "elasticquery"
4
4
  class TestQueriesInheritence < MiniTest::Test
5
5
  class MultipleFilteredQuery < Elasticquery::Base
6
6
  filtered do |params|
7
- term :"category.id" => params[:category_id]
7
+ filters do
8
+ term :"category.id" => params[:category_id]
9
+ end
8
10
  end
9
11
 
10
12
  filtered do |params|
11
- term :"author.name" => params[:author_name]
13
+ filters do
14
+ term :"author.name" => params[:author_name]
15
+ end
12
16
  end
13
17
  end
14
18
 
15
19
  class ParentPostQuery < Elasticquery::Base
16
20
  filtered do |params|
17
- term :"category.id" => params[:category_id]
21
+ filters do
22
+ term :"category.id" => params[:category_id]
23
+ end
18
24
  end
19
25
  end
20
26
 
21
27
  class ChildPostQuery < ParentPostQuery
22
28
  filtered do |params|
23
- term :"author.name" => params[:author_name]
29
+ filters do
30
+ term :"author.name" => params[:author_name]
31
+ end
24
32
  end
25
33
  end
26
34
 
@@ -4,8 +4,10 @@ require "elasticquery"
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], execution: "fielddata"
8
- range :revenue, lte: params[:max_revenue], gte: params[:min_revenue]
7
+ filters do
8
+ range :year, lte: params[:max_year], gte: params[:min_year], execution: "fielddata"
9
+ range :revenue, lte: params[:max_revenue], gte: params[:min_revenue]
10
+ end
9
11
  end
10
12
  end
11
13
 
@@ -4,23 +4,31 @@ require "elasticquery"
4
4
  class TestSearchQueryCase < MiniTest::Test
5
5
  class PostQuery < Elasticquery::Base
6
6
  filtered do |params|
7
- term :"category" => params[:category]
8
- search params[:search]
7
+ filters do
8
+ term :"category" => params[:category]
9
+ end
10
+ queries do
11
+ search params[:search]
12
+ end
9
13
  end
10
14
  end
11
15
 
12
16
  class PostOptionsQuery < Elasticquery::Base
13
17
  filtered do |params|
14
- search params[:search],
15
- fields: params[:fields],
16
- operator: params[:operator],
17
- type: "phrase"
18
+ queries do
19
+ search params[:search],
20
+ fields: params[:fields],
21
+ operator: params[:operator],
22
+ type: "phrase"
23
+ end
18
24
  end
19
25
  end
20
26
 
21
27
  class MultipleSearch < PostQuery
22
28
  filtered do |params|
23
- search "look for"
29
+ queries do
30
+ search "look for"
31
+ end
24
32
  end
25
33
  end
26
34
 
@@ -30,7 +38,7 @@ class TestSearchQueryCase < MiniTest::Test
30
38
  expected_term = [{term: {category: "zoo"}}]
31
39
  assert_equal expected_term, actual[:query][:filtered][:filter][:and]
32
40
  expected_search = { query: "hello", fields: "_all", operator: "and", type: "best_fields"}
33
- assert_equal expected_search, actual[:query][:filtered][:query][:multi_match]
41
+ assert_equal expected_search, actual[:query][:filtered][:query][:bool][:must][0][:multi_match]
34
42
  end
35
43
 
36
44
  def test_empty_search
@@ -43,12 +51,13 @@ class TestSearchQueryCase < MiniTest::Test
43
51
  params = {search: "hello", fields: %w(title body), operator: "or"}
44
52
  actual = PostOptionsQuery.new(params).build
45
53
  expected = {fields: %w(title body), operator: "or", type: "phrase", query: "hello"}
46
- assert_equal expected, actual[:query][:filtered][:query][:multi_match]
54
+ assert_equal expected, actual[:query][:filtered][:query][:bool][:must][0][:multi_match]
47
55
  end
48
56
 
49
- def test_multiple_search_use_last_search_declaration
57
+ def test_multiple_searches_should_be_combined
50
58
  params = {search: "hello"}
51
59
  actual = MultipleSearch.new(params).build
52
- assert_equal "look for", actual[:query][:filtered][:query][:multi_match][:query]
60
+ assert_equal "hello", actual[:query][:filtered][:query][:bool][:must][0][:multi_match][:query]
61
+ assert_equal "look for", actual[:query][:filtered][:query][:bool][:must][1][:multi_match][:query]
53
62
  end
54
63
  end
@@ -4,8 +4,10 @@ require "elasticquery"
4
4
  class TestTermCase < MiniTest::Test
5
5
  class PostQuery < Elasticquery::Base
6
6
  filtered do |params|
7
- term :"category.id" => params[:category_id]
8
- term :"author.name" => params[:author_name], _cache: false
7
+ filters do
8
+ term :"category.id" => params[:category_id]
9
+ term :"author.name" => params[:author_name], _cache: false
10
+ end
9
11
  end
10
12
  end
11
13
 
@@ -0,0 +1,49 @@
1
+ require "test_helper"
2
+ require "elasticquery"
3
+
4
+ class TestTermsCase < MiniTest::Test
5
+ class PostQuery < Elasticquery::Base
6
+ filtered do |params|
7
+ filters do
8
+ terms id: params[:id], name: params[:name]
9
+ end
10
+ end
11
+ end
12
+
13
+ class OverridedPostQuery < PostQuery
14
+ filtered do
15
+ filters do
16
+ term.not status: "pending"
17
+ where.not status: "waiting", status: params[:inactive_status]
18
+ end
19
+ queries do
20
+ search params[:q]
21
+ end
22
+ end
23
+ end
24
+
25
+ def test_individual_terms_filter
26
+ params = {id: 1, name: "name"}
27
+ actual = PostQuery.new(params).build
28
+ expected = [{terms: {id: 1, name: "name"}}]
29
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
30
+ end
31
+
32
+ def test_with_other_rules
33
+ params = {id: 1, name: "name", inactive_status: "closed", q: "Hello"}
34
+ actual = OverridedPostQuery.new(params).build
35
+
36
+ expected_query = {multi_match: {fields: "_all", operator: "and", type: "best_fields", query: "Hello"}}
37
+ expected_filters = [{:terms=>{:id=>1, :name=>"name"}}, {:not=>{:filter=>{:term=>{:status=>"pending"}}}}, {:not=>{:filter=>{:terms=>{:status=>"closed"}}}}]
38
+
39
+ assert_equal expected_query, actual[:query][:filtered][:query][:bool][:must][0]
40
+ assert_equal expected_filters, actual[:query][:filtered][:filter][:and]
41
+ end
42
+
43
+ def test_empty_params_skipping
44
+ params = {id: 1}
45
+ actual = OverridedPostQuery.new(params).build
46
+ expected_filters = [{terms: {id: 1}}, {not: {filter: {term: {status: "pending"}}}}]
47
+ assert_equal expected_filters, actual[:query][:filtered][:filter][:and]
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ require "test_helper"
2
+ require "elasticquery/queries/multi_match"
3
+
4
+ class TestMultiMatchQuery < MiniTest::Test
5
+
6
+ def test_default_values_of_filter
7
+ filter = Elasticquery::Queries::MultiMatch.new "hi"
8
+ assert_equal filter.to_hash, {multi_match: {fields: "_all", operator: "and", type: "best_fields", query: "hi"}}
9
+ end
10
+
11
+ def test_passed_parameters
12
+ assert_raises ArgumentError do
13
+ Elasticquery::Queries::MultiMatch.new
14
+ end
15
+ end
16
+
17
+ def test_empty_query_is_invalid
18
+ filter = Elasticquery::Queries::MultiMatch.new " "
19
+ assert filter.invalid?
20
+ end
21
+
22
+ def test_default_query_should_be_valid
23
+ filter = Elasticquery::Queries::MultiMatch.new "hi"
24
+ assert filter.valid?
25
+ end
26
+
27
+ def test_query_fields_validation
28
+ filter = Elasticquery::Queries::MultiMatch.new "hi", fields: 42
29
+ refute filter.valid?
30
+ filter = Elasticquery::Queries::MultiMatch.new "hi", fields: %w(name body)
31
+ assert filter.valid?
32
+ end
33
+
34
+ def test_operator_validation
35
+ filter = Elasticquery::Queries::MultiMatch.new "hi", operator: "without"
36
+ refute filter.valid?
37
+ filter = Elasticquery::Queries::MultiMatch.new "hi", operator: "or"
38
+ assert filter.valid?
39
+ end
40
+
41
+ def test_types_validation
42
+ %w(most_fields cross_fields phrase pharse_prefix).each do |type|
43
+ filter = Elasticquery::Queries::MultiMatch.new "hi", type: type
44
+ assert filter.valid?
45
+ end
46
+ filter = Elasticquery::Queries::MultiMatch.new "hi", type: "random"
47
+ refute filter.valid?
48
+ end
49
+ end
@@ -16,64 +16,64 @@ class TestQuery < MiniTest::Test
16
16
  assert_equal({query: {filtered: {query: {match_all:{}}}}}, @query.to_hash)
17
17
  end
18
18
 
19
- def test_default_query_should_be_matched_all
20
- assert @query.match_all?
19
+ def test_push_query_should_combine_queries_by_bool_must_query
20
+ @rule.expect :valid?, true
21
21
  @rule.expect :to_hash, {a: 1}
22
- @query << @rule
23
- refute @query.match_all?
24
- end
22
+ @query.push_query @rule
23
+ assert_equal({query: {filtered: {query: {bool: {must: [{a: 1}]}}}}}, @query.to_hash)
24
+
25
+ @rule.expect :valid?, true
26
+ @rule.expect :to_hash, {b: 1}
27
+ @query.push_query @rule
28
+ assert_equal({query: {filtered: {query: {bool: {must: [{a: 1}, {b: 1}]}}}}}, @query.to_hash)
25
29
 
26
- def test_pushing_of_hash_to_new_query
27
- @rule.expect :to_hash, {a: 2, b: 1}
28
- @query << @rule
29
- assert_equal({a:2, b: 1}, @query.to_hash)
30
+ assert_equal 2, @query.queries.size
31
+ assert @rule.verify
30
32
  end
31
33
 
32
- def test_pushing_of_subquery_to_existing_one
33
- @rule.expect :to_hash, {a: 2, b: 1}
34
- @query << @rule
35
- @rule.expect :to_hash, {a: 1}
36
- @query << @rule
37
- assert_equal({a:1, b: 1}, @query.to_hash)
34
+ def test_push_query_should_not_merge_if_query_is_invalid
35
+ @rule.expect :valid?, false
36
+ @query.push_query @rule
37
+ assert_equal(Elasticquery::Query::DEFAULT, @query.to_hash)
38
+
39
+ assert @rule.verify
38
40
  end
39
41
 
40
- def test_push_filters_to_variable
41
- @rule.expect :to_hash, {a: 2, b: 1}
42
- @query << @rule
42
+ def test_push_filter_should_combine_filters_by_and_condition
43
+ @rule.expect :valid?, true
43
44
  @rule.expect :to_hash, {a: 1}
44
- @query << @rule
45
+ @query.push_filter @rule
46
+ assert_equal({query: {filtered: {filter: {and: [{a: 1}]}}}}, @query.to_hash)
47
+
48
+ @rule.expect :valid?, true
49
+ @rule.expect :to_hash, {b: 1}
50
+ @query.push_filter @rule
51
+ assert_equal({query: {filtered: {filter: {and: [{a: 1}, {b: 1}]}}}}, @query.to_hash)
52
+
45
53
  assert_equal 2, @query.filters.size
54
+ assert @rule.verify
46
55
  end
47
56
 
48
- def test_should_group_filters_in_array
49
- @rule.expect :to_hash, query: {filtered: {filter: {and: [{term: {a: 1, _cache: true}}]}}}
50
- @query << @rule
51
- @rule.expect :to_hash, query: {filtered: {filter: {and: [{term: {c: 3}}]}}}
52
- @query << @rule
53
- @rule.expect :to_hash, query: {filtered: {filter: {and: [{range: {x: 3}}]}}}
54
- @query << @rule
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
57
+ def test_push_filter_should_not_merge_if_filter_is_invalid
58
+ @rule.expect :valid?, false
59
+ @query.push_filter @rule
60
+ assert_equal(Elasticquery::Query::DEFAULT, @query.to_hash)
58
61
 
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"}}]}}}}
65
- assert_equal(expected, @query.to_hash)
62
+ assert @rule.verify
66
63
  end
67
-
68
64
  class Elasticquery::Query
69
65
  # query context
70
66
  def forty_two
71
67
  42
72
68
  end
73
69
  end
74
- def test_merge_when_filters_is_lambda_uses_query_context
70
+
71
+ def test_merge_when_filters_is_lambda_uses_es_query_context
72
+ @rule.expect :valid?, true
75
73
  @rule.expect :to_hash, -> { {forty_two: forty_two} }
76
- @query << @rule
77
- assert_equal @query.to_hash, {forty_two: 42}
74
+ @query.push_filter @rule
75
+ assert_equal @query.to_hash, {query: {filtered: {filter: {and: [{forty_two: 42}]}}}}
76
+
77
+ assert @rule.verify
78
78
  end
79
79
  end
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.2
4
+ version: 0.1.3
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-06-15 00:00:00.000000000 Z
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,20 +24,6 @@ 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'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -53,7 +39,7 @@ dependencies:
53
39
  - !ruby/object:Gem::Version
54
40
  version: '1.6'
55
41
  - !ruby/object:Gem::Dependency
56
- name: yard
42
+ name: rake
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - ">="
@@ -67,7 +53,7 @@ dependencies:
67
53
  - !ruby/object:Gem::Version
68
54
  version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
- name: rake
56
+ name: minitest
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - ">="
@@ -81,7 +67,7 @@ dependencies:
81
67
  - !ruby/object:Gem::Version
82
68
  version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
- name: minitest
70
+ name: minitest-reporters
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - ">="
@@ -95,7 +81,7 @@ dependencies:
95
81
  - !ruby/object:Gem::Version
96
82
  version: '0'
97
83
  - !ruby/object:Gem::Dependency
98
- name: minitest-reporters
84
+ name: codeclimate-test-reporter
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
87
  - - ">="
@@ -109,7 +95,7 @@ dependencies:
109
95
  - !ruby/object:Gem::Version
110
96
  version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
- name: codeclimate-test-reporter
98
+ name: elasticsearch-model
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
101
  - - ">="
@@ -141,25 +127,34 @@ files:
141
127
  - lib/elasticquery.rb
142
128
  - lib/elasticquery/base.rb
143
129
  - lib/elasticquery/builder.rb
130
+ - lib/elasticquery/es.rb
144
131
  - lib/elasticquery/filters/base.rb
132
+ - lib/elasticquery/filters/exists.rb
145
133
  - lib/elasticquery/filters/not.rb
146
134
  - lib/elasticquery/filters/range.rb
147
- - lib/elasticquery/filters/search.rb
148
135
  - lib/elasticquery/filters/term.rb
136
+ - lib/elasticquery/filters/terms.rb
137
+ - lib/elasticquery/queries/base.rb
138
+ - lib/elasticquery/queries/multi_match.rb
149
139
  - lib/elasticquery/query.rb
150
140
  - lib/elasticquery/version.rb
151
141
  - test/base_test.rb
152
142
  - test/builder_test.rb
143
+ - test/es_test.rb
144
+ - test/filters/exists_test.rb
153
145
  - test/filters/not_test.rb
154
146
  - test/filters/range_test.rb
155
- - test/filters/search_test.rb
156
147
  - test/filters/term_test.rb
148
+ - test/filters/terms_test.rb
157
149
  - test/integration/chainable_call_test.rb
150
+ - test/integration/exists_case_test.rb
158
151
  - test/integration/not_case_test.rb
159
152
  - test/integration/queries_inheritence_test.rb
160
153
  - test/integration/range_case_test.rb
161
154
  - test/integration/search_case_test.rb
162
155
  - test/integration/term_case_test.rb
156
+ - test/integration/terms_case_test.rb
157
+ - test/queries/multi_match_test.rb
163
158
  - test/query_test.rb
164
159
  - test/test_helper.rb
165
160
  homepage: https://github.com/caulfield/elasticquery
@@ -174,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
169
  requirements:
175
170
  - - ">="
176
171
  - !ruby/object:Gem::Version
177
- version: '0'
172
+ version: 2.0.0
178
173
  required_rubygems_version: !ruby/object:Gem::Requirement
179
174
  requirements:
180
175
  - - ">="
@@ -182,23 +177,27 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
177
  version: '0'
183
178
  requirements: []
184
179
  rubyforge_project:
185
- rubygems_version: 2.2.2
180
+ rubygems_version: 2.4.5
186
181
  signing_key:
187
182
  specification_version: 4
188
183
  summary: Elasticsearch query builder.
189
184
  test_files:
190
185
  - test/base_test.rb
191
186
  - test/builder_test.rb
187
+ - test/es_test.rb
188
+ - test/filters/exists_test.rb
192
189
  - test/filters/not_test.rb
193
190
  - test/filters/range_test.rb
194
- - test/filters/search_test.rb
195
191
  - test/filters/term_test.rb
192
+ - test/filters/terms_test.rb
196
193
  - test/integration/chainable_call_test.rb
194
+ - test/integration/exists_case_test.rb
197
195
  - test/integration/not_case_test.rb
198
196
  - test/integration/queries_inheritence_test.rb
199
197
  - test/integration/range_case_test.rb
200
198
  - test/integration/search_case_test.rb
201
199
  - test/integration/term_case_test.rb
200
+ - test/integration/terms_case_test.rb
201
+ - test/queries/multi_match_test.rb
202
202
  - test/query_test.rb
203
203
  - test/test_helper.rb
204
- has_rdoc: