elasticquery 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.md ADDED
@@ -0,0 +1,22 @@
1
+ ## Elasticquery (master)
2
+
3
+ ## Elasticquery 0.1.2 (16 June 2015)
4
+
5
+ * Fix `range.not`. Exception was raised
6
+
7
+ * Skip blank values in filtering
8
+ `term name: " "`, `range :age, lte: ""`, and `search nil` return all records
9
+
10
+ * Create `history.md` file
11
+
12
+ * Add options support for `range` and `term`.
13
+ `term` supports `_cache` option. `range` supports both `_cache` and `execution`
14
+
15
+ ## Elasticquery 0.1.1 (02 Mar 2015)
16
+
17
+ * Fix typo in gem description
18
+ * Add rubygems bange
19
+
20
+ ## Elasticquery 0.1.0 (02 Mar 2015)
21
+
22
+ * Initial version. Supported filters: `term`, `range`, `term.not`, `range.not`. Support multi match query. Simple inheritence support. Chain calls.
data/README.md CHANGED
@@ -23,7 +23,7 @@ gem install elasticquery
23
23
  ## Getting Started
24
24
  ### First instruction
25
25
 
26
- Elasticsearch was designed to be customized as you need to. Providing simple methods it allows you to build power and flexible form objects. For example:
26
+ Elasticquery was designed to be customized as you need to. Providing simple methods it allows you to build power and flexible form objects. Simplicity - is the main goal. For example:
27
27
 
28
28
  ```ruby
29
29
  class MyQuery < Elasticquery::Base
@@ -48,8 +48,6 @@ Article.search query.build # => be happy
48
48
  2. [MultiMatch][es_search] filter. [Usage][search_examples]
49
49
  3. [Range][es_range] filter. [Usage][range_examples]
50
50
 
51
- #### Note! After first releases of elasticuqery it has scarce support of options and methods. Pull requests and issues with your ideas are welcome!
52
-
53
51
  ### Extended instruction
54
52
  There are multiple ways to organize your query, using chaining calls, or custom filters. Better custom filters support in progress now.
55
53
 
@@ -102,15 +100,6 @@ end
102
100
 
103
101
  ChildQuery.build({user_id: 1, category_id: 14}) => # the same as in previous example
104
102
  ```
105
- - Custom filter methods
106
- ```ruby
107
- in progress...
108
- ```
109
-
110
- - Custom filter classes
111
- ```ruby
112
- in progress...
113
- ```
114
103
 
115
104
  ### Usage
116
105
  #### term
@@ -124,6 +113,9 @@ term category: 'Soul', user: 'Aaron'
124
113
 
125
114
  # Term exclusion
126
115
  term.not category: 'Rap'
116
+
117
+ # Blank values are skipped. This query returns all records
118
+ term name: " "
127
119
  ```
128
120
 
129
121
  #### search (multimatch)
@@ -136,6 +128,9 @@ search 'developers', fields: "_all", operator: "and", type: "best_fields"
136
128
 
137
129
  # Configure fields
138
130
  search 'Jordan', fields: ['first_name', 'last_name'], operator: "or"
131
+
132
+ # Blank values are skipped. This query returns all records
133
+ search ''
139
134
  ```
140
135
 
141
136
  #### range
@@ -148,12 +143,13 @@ range :volume, gte: 1, lte: 100
148
143
 
149
144
  # Range exclusion
150
145
  range.not :size, gte: 32, lte: 128
151
- ```
152
-
153
- ## Contributing
154
- 1. I'm happy to see any method you can be part of this.
155
146
 
147
+ # Blank values are skipped. This query returns all records
148
+ range.not :age, lte: ' ', gte: nil
156
149
 
150
+ # _cache and execution options support
151
+ range :volume, gte: 1, lte: 100, _cache: true, execution: "fielddata"
152
+ ```
157
153
  [elasticsearch_rails]: https://github.com/elasticsearch/elasticsearch-rails
158
154
  [demo]: http://elasticquery-demo.herokuapp.com
159
155
  [bundler]: http://bundler.io/
data/elasticquery.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "activesupport"
22
+ spec.add_dependency "deep_merge"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.6"
24
25
  spec.add_development_dependency "yard"
@@ -1,11 +1,11 @@
1
- require 'active_support/core_ext/class/attribute'
2
- require 'elasticquery/query'
3
- require 'elasticquery/queries/all'
1
+ require "active_support/core_ext/class/attribute"
2
+ require "elasticquery/query"
3
+ require "elasticquery/builder"
4
4
 
5
5
  # Base class for query builder. Superclass of your all builders
6
6
  module Elasticquery
7
7
  class Base
8
- include Elasticquery::Queries::All
8
+ include Elasticquery::Builder
9
9
 
10
10
  class_attribute :filters
11
11
  attr_reader :params, :query
@@ -0,0 +1,32 @@
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 Builder
11
+ extend ActiveSupport::Concern
12
+
13
+ filters = [
14
+ Filters::Term,
15
+ Filters::Search,
16
+ Filters::Not,
17
+ Filters::Range
18
+ ]
19
+
20
+ included do
21
+ filters.each do |filter_class|
22
+ filter_name = filter_class.to_s.split("::").last.underscore
23
+
24
+ define_method filter_name do |*args|
25
+ filter = filter_class.new *args
26
+ query << filter
27
+ self
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
1
3
  module Elasticquery
2
4
  module Filters
3
5
  class Base
@@ -1,25 +1,25 @@
1
- require_relative 'base'
1
+ require_relative "base"
2
2
 
3
3
  module Elasticquery
4
4
  module Filters
5
5
  class Not < Base
6
6
 
7
- def initialize(condition = {})
8
- @condition = condition
7
+ def initialize(*args)
8
+ @args = args
9
9
  end
10
10
 
11
11
  def dup_with(*args)
12
- raise StandardError, 'Cannot use Filters::Not twice'
12
+ raise StandardError, "Cannot use Filters::Not twice"
13
13
  end
14
14
 
15
15
  def to_hash
16
- condition = @condition
16
+ args = @args
17
17
  -> do
18
- filter = filters.last.dup_with condition
18
+ filter = filters.last.dup_with *args
19
19
  if filter.valid?
20
20
  subquery = filter.to_hash
21
- q = subquery[:query][:filtered][:filter][:and]
22
- {query: {filtered: {filter: {and: {not: {filter: q}}}}}}
21
+ q = subquery[:query][:filtered][:filter][:and][0]
22
+ {query: {filtered: {filter: {and: [{not: {filter: q}}]}}}}
23
23
  else
24
24
  {}
25
25
  end
@@ -1,25 +1,31 @@
1
- require_relative 'base'
1
+ require_relative "base"
2
2
 
3
3
  module Elasticquery
4
4
  module Filters
5
5
  class Range < Base
6
+ OPTIONS = %i(_cache execution)
6
7
 
7
- def initialize(field, gte: nil, lte: nil)
8
+ def initialize(field=nil, gte: nil, lte: nil, **options)
9
+ @options = options.extract! *OPTIONS
8
10
  @field, @gte, @lte = field, gte, lte
9
11
  end
10
12
 
11
13
  def valid?
12
- @gte || @lte
14
+ @gte.present? || @lte.present?
13
15
  end
14
16
 
15
17
  def to_hash
16
- valid? ? {query: {filtered: {filter: {and: {range: range}}}}} : {}
18
+ if valid?
19
+ {query: {filtered: {filter: {and: [{range: range_with_options}]}}}}
20
+ else
21
+ {}
22
+ end
17
23
  end
18
24
 
19
25
  private
20
26
 
21
- def range
22
- {@field => {}}.tap do |r|
27
+ def range_with_options
28
+ {@field => @options}.tap do |r|
23
29
  r[@field][:lte] = @lte unless @lte.nil?
24
30
  r[@field][:gte] = @gte unless @gte.nil?
25
31
  end
@@ -1,4 +1,4 @@
1
- require_relative 'base'
1
+ require_relative "base"
2
2
 
3
3
  module Elasticquery
4
4
  module Filters
@@ -29,6 +29,7 @@ module Elasticquery
29
29
  def valid?
30
30
  OPERATORS.include?(@operator) &&
31
31
  TYPES.include?(@type) &&
32
+ @query.present? &&
32
33
  ( Array === @fields || @fields == "_all" )
33
34
  end
34
35
 
@@ -1,21 +1,22 @@
1
- require_relative 'base'
1
+ require_relative "base"
2
2
 
3
3
  module Elasticquery
4
4
  module Filters
5
5
  class Term < Base
6
+ OPTIONS = %i(_cache)
6
7
 
7
- # Create filtered -> filter filter for elasticsearch
8
- # builder
8
+ # Create term filter for elasticsearch builder
9
9
  #
10
10
  # @param [Hash] condition passed to define
11
11
  #
12
12
  # @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filtering_without_a_query
13
- def initialize(condition={})
14
- @condition = condition
13
+ def initialize(data={})
14
+ @options = data.extract! *OPTIONS
15
+ @condition = data
15
16
  end
16
17
 
17
18
  # Is passed condition valid. Passed condition must have
18
- # one key
19
+ # one key and present value
19
20
  #
20
21
  # @return [Boolean] is condition have at least one key
21
22
  #
@@ -23,7 +24,7 @@ module Elasticquery
23
24
  # rule = Elasticquery::Rules::Term.new
24
25
  # rule.valid? #=> false
25
26
  def valid?
26
- @condition.keys.any?
27
+ @condition.keys.size == 1 && @condition.values[0].present?
27
28
  end
28
29
 
29
30
  # Hash presentation of query.
@@ -31,10 +32,14 @@ module Elasticquery
31
32
  # @return [Hash] presentation of filter.
32
33
  #
33
34
  # @example
34
- # r = Elasticquery::filters::Term.new { name: "John" }
35
- # r.to_hash #=> {query: {filtered: {filter: {and: {name: 'John'}}}}}
35
+ # valid = Elasticquery::Filters::Term.new {name: "John"}
36
+ # valid.to_hash #=> {query: {filtered: {filter: {and: [{term: {name: 'John'}}]}}}
36
37
  def to_hash
37
- valid? ? {query: {filtered: {filter: {and: {term: @condition}}}}} : {}
38
+ if valid?
39
+ {query: {filtered: {filter: {and: [{term: @condition.merge(@options)}]}}}}
40
+ else
41
+ {}
42
+ end
38
43
  end
39
44
  end
40
45
  end
@@ -1,4 +1,4 @@
1
- require 'active_support/core_ext/hash/deep_merge'
1
+ require "deep_merge/rails_compat"
2
2
 
3
3
  module Elasticquery
4
4
  class Query
@@ -22,16 +22,8 @@ module Elasticquery
22
22
  #
23
23
  # @return [Hash] hash presentation of query
24
24
  def to_hash
25
- q = @query.dup
26
- if Enumerable === _filters
27
- flatted_filters = _filters.flat_map do |type, filters|
28
- filters.flat_map do |key, value|
29
- {type => {key => value}}
30
- end
31
- end
32
- q[:query][:filtered][:filter][:and] = flatted_filters
33
- end
34
- q
25
+ return DEFAULT if @query == {}
26
+ @query
35
27
  end
36
28
 
37
29
  # Merge filter to query. If current query is `matche_all`ed
@@ -59,7 +51,7 @@ module Elasticquery
59
51
  def merge(filter)
60
52
  hash = filter.to_hash
61
53
  subquery = hash.respond_to?(:call) ? instance_exec(&hash) : hash
62
- @query.deep_merge! subquery
54
+ @query.deeper_merge! subquery
63
55
  end
64
56
 
65
57
  def _filters
@@ -1,4 +1,4 @@
1
1
  module Elasticquery
2
- # current gem version
3
- VERSION = "0.1.1"
2
+ # Current gem version
3
+ VERSION = "0.1.2"
4
4
  end
data/lib/elasticquery.rb CHANGED
@@ -2,5 +2,5 @@ require "elasticquery/version"
2
2
 
3
3
  # Base gem module
4
4
  module Elasticquery
5
- autoload :Base, 'elasticquery/base'
5
+ autoload :Base, "elasticquery/base"
6
6
  end
data/test/base_test.rb CHANGED
@@ -1,5 +1,5 @@
1
- require 'test_helper'
2
- require 'elasticquery/base'
1
+ require "test_helper"
2
+ require "elasticquery/base"
3
3
 
4
4
  class TestBase < MiniTest::Test
5
5
 
@@ -52,7 +52,7 @@ class TestBase < MiniTest::Test
52
52
 
53
53
  def test_should_execute_code_in_instance_context
54
54
  klass = Class.new(Elasticquery::Base)
55
- klass.send(:define_method, :_error_) { raise StandardError, 'from instance context'}
55
+ klass.send(:define_method, :_error_) { raise StandardError, "from instance context"}
56
56
  klass.filtered { _error_ }
57
57
 
58
58
  assert_raises(StandardError) { klass.new.build }
@@ -1,8 +1,8 @@
1
- require 'test_helper'
2
- require 'elasticquery/queries/all'
1
+ require "test_helper"
2
+ require "elasticquery/builder"
3
3
 
4
4
  class TestAllBaseQueries < MiniTest::Test
5
- include Elasticquery::Queries::All
5
+ include Elasticquery::Builder
6
6
 
7
7
  attr_accessor :query
8
8
 
@@ -1,5 +1,5 @@
1
- require 'test_helper'
2
- require 'elasticquery/filters/not'
1
+ require "test_helper"
2
+ require "elasticquery/filters/not"
3
3
 
4
4
  class NotFilter < MiniTest::Test
5
5
  attr_reader :filters
@@ -20,13 +20,15 @@ class NotFilter < MiniTest::Test
20
20
  def test_to_hash_change_last_term_filter_value
21
21
  filter_class = MiniTest::Mock.new
22
22
  filter_class.expect :valid?, true
23
- filter_class.expect :to_hash, {query: {filtered: {filter: {and: {term: {a: 1, b: 2}}}}}}
23
+ expected = {query: {filtered: {filter: {and: [{term: {a: 1, b: 2}}]}}}}
24
+ filter_class.expect :to_hash, expected
24
25
  previous_filter = MiniTest::Mock.new
25
26
  previous_filter.expect :dup_with, filter_class, [{a: 1}]
26
27
 
27
28
  @filters = [previous_filter]
28
29
  result = instance_exec &@filter.to_hash
29
- assert_equal result, query: {filtered: {filter: {and: {not: {filter: {term: {a: 1, b: 2}}}}}}}
30
+ expected = {query: {filtered: {filter: {and: [{not: {filter: {term: {a: 1, b: 2}}}}]}}}}
31
+ assert_equal result, expected
30
32
 
31
33
  assert filter_class.verify
32
34
  assert previous_filter.verify
@@ -35,13 +37,15 @@ class NotFilter < MiniTest::Test
35
37
  def test_to_hash_change_last_range_filter_value
36
38
  filter_class = MiniTest::Mock.new
37
39
  filter_class.expect :valid?, true
38
- filter_class.expect :to_hash, {query: {filtered: {filter: {and: {range: {a: {lte: 1}}}}}}}
40
+ expected = {query: {filtered: {filter: {and: [{range: {a: {lte: 1}}}]}}}}
41
+ filter_class.expect :to_hash, expected
39
42
  previous_filter = MiniTest::Mock.new
40
43
  previous_filter.expect :dup_with, filter_class, [{a: 1}]
41
44
 
42
45
  @filters = [previous_filter]
43
46
  result = instance_exec &@filter.to_hash
44
- assert_equal result, query: {filtered: {filter: {and: {not: {filter: {range: {a: {lte: 1}}}}}}}}
47
+ expected = {query: {filtered: {filter: {and: [{not: {filter: {range: {a: {lte: 1}}}}}]}}}}
48
+ assert_equal result, expected
45
49
 
46
50
  assert filter_class.verify
47
51
  assert previous_filter.verify
@@ -1,44 +1,66 @@
1
- require 'test_helper'
2
- require 'elasticquery/filters/range'
1
+ require "test_helper"
2
+ require "elasticquery/filters/range"
3
3
 
4
4
  class RangeFilter < MiniTest::Test
5
5
  def setup
6
- @filter = Elasticquery::Filters::Range.new 'year', lte: 2015, gte: 1990
7
- @lte_filter = Elasticquery::Filters::Range.new 'age', lte: 58
8
- @gte_filter = Elasticquery::Filters::Range.new 'age', gte: 18
6
+ @filter = Elasticquery::Filters::Range.new "year", lte: 2015, gte: 1990
7
+ @lte_filter = Elasticquery::Filters::Range.new "age", lte: 58
8
+ @gte_filter = Elasticquery::Filters::Range.new "age", gte: 18
9
9
  end
10
10
 
11
11
  def test_filter_valid_if_has_at_least_lte_or_gte_option
12
- filter = Elasticquery::Filters::Range.new 'year', lte: 1
12
+ filter = Elasticquery::Filters::Range.new "year", lte: 1
13
13
  assert filter.valid?
14
- filter = Elasticquery::Filters::Range.new 'year', gte: 0
14
+ filter = Elasticquery::Filters::Range.new "year", gte: 0
15
15
  assert filter.valid?
16
16
  end
17
17
 
18
+ def empty_values_is_invalid
19
+ filter = Elasticquery::Filters::Range.new "year", lte: " "
20
+ assert filter.invalid?
21
+ filter = Elasticquery::Filters::Range.new "year", gte: nil, lte: ""
22
+ assert filte.invalid?
23
+ end
24
+
18
25
  def test_filter_invalid_if_has_no_options
19
- filter = Elasticquery::Filters::Range.new 'year'
26
+ filter = Elasticquery::Filters::Range.new "year"
20
27
  refute filter.valid?
21
28
  end
22
29
 
23
30
  def test_to_hash_should_return_query
24
31
  filter = Elasticquery::Filters::Range.new :year, lte: 1, gte: 0
25
- assert_equal({query: {filtered: {filter: {and: {range: {year: {lte: 1, gte: 0}}}}}}}, filter.to_hash)
32
+ assert_equal({query: {filtered: {filter: {and: [{range: {year: {lte: 1, gte: 0}}}]}}}}, filter.to_hash)
26
33
  end
27
34
 
28
35
  def test_to_hash_with_one_param
29
36
  filter = Elasticquery::Filters::Range.new :year, lte: 1
30
- assert_equal({query: {filtered: {filter: {and: {range: {year: {lte: 1}}}}}}}, filter.to_hash)
37
+ assert_equal({query: {filtered: {filter: {and: [{range: {year: {lte: 1}}}]}}}}, filter.to_hash)
38
+ end
39
+
40
+ def test_to_hash_support_cache_option
41
+ filter = Elasticquery::Filters::Range.new :year, lte: 1, _cache: true
42
+ assert_equal({query: {filtered: {filter: {and: [{range: {year: {_cache: true, lte: 1}}}]}}}}, filter.to_hash)
43
+ end
44
+
45
+ def test_to_hash_support_execution_option
46
+ filter = Elasticquery::Filters::Range.new :year, lte: 1, gte: 0, execution: "fielddata"
47
+ assert_equal({query: {filtered: {filter: {and: [{range: {year: {lte: 1, gte: 0, execution: "fielddata"}}}]}}}}, filter.to_hash)
48
+ end
49
+
50
+ def test_to_hash_skip_unnesessary_options
51
+ filter = Elasticquery::Filters::Range.new :year, lte: 1, _cache: true, extra: false
52
+ assert_equal({query: {filtered: {filter: {and: [{range: {year: { _cache: true, lte: 1}}}]}}}}, filter.to_hash)
31
53
  end
32
54
 
33
55
  def test_to_hash_is_empty_if_invalid
34
- filter = Elasticquery::Filters::Range.new 'year'
56
+ filter = Elasticquery::Filters::Range.new "year"
35
57
  refute filter.valid?
36
58
  assert_equal filter.to_hash, {}
37
59
  end
38
60
 
39
61
  def test_dup_with_return_new_range_filter
40
- filter = Elasticquery::Filters::Range.new 'year'
41
- new_filter = filter.dup_with 'month', gte: 12
62
+ filter = Elasticquery::Filters::Range.new "year"
63
+ new_filter = filter.dup_with "month", gte: 12
42
64
  assert_kind_of Elasticquery::Filters::Range, new_filter
43
65
  end
44
66
  end
@@ -1,10 +1,10 @@
1
- require 'test_helper'
2
- require 'elasticquery/filters/search'
1
+ require "test_helper"
2
+ require "elasticquery/filters/search"
3
3
 
4
4
  class TestSearchFilter < MiniTest::Test
5
5
 
6
6
  def test_default_values_of_filter
7
- filter = Elasticquery::Filters::Search.new 'hi'
7
+ filter = Elasticquery::Filters::Search.new "hi"
8
8
  actual = filter.to_hash[:query][:filtered][:query][:multi_match]
9
9
  assert_equal actual, {fields: "_all", operator: "and", type: "best_fields", query: "hi"}
10
10
  end
@@ -15,43 +15,48 @@ class TestSearchFilter < MiniTest::Test
15
15
  end
16
16
  end
17
17
 
18
+ def test_empty_query_is_invalid
19
+ filter = Elasticquery::Filters::Search.new " "
20
+ assert filter.invalid?
21
+ end
22
+
18
23
  def test_default_query_should_be_valid
19
- filter = Elasticquery::Filters::Search.new 'hi'
24
+ filter = Elasticquery::Filters::Search.new "hi"
20
25
  assert filter.valid?
21
26
  end
22
27
 
23
28
  def test_query_fields_validation
24
- filter = Elasticquery::Filters::Search.new 'hi', fields: 42
29
+ filter = Elasticquery::Filters::Search.new "hi", fields: 42
25
30
  refute filter.valid?
26
- filter = Elasticquery::Filters::Search.new 'hi', fields: %w(name body)
31
+ filter = Elasticquery::Filters::Search.new "hi", fields: %w(name body)
27
32
  assert filter.valid?
28
33
  end
29
34
 
30
35
  def test_operator_validation
31
- filter = Elasticquery::Filters::Search.new 'hi', operator: 'without'
36
+ filter = Elasticquery::Filters::Search.new "hi", operator: "without"
32
37
  refute filter.valid?
33
- filter = Elasticquery::Filters::Search.new 'hi', operator: 'or'
38
+ filter = Elasticquery::Filters::Search.new "hi", operator: "or"
34
39
  assert filter.valid?
35
40
  end
36
41
 
37
42
  def test_types_validation
38
43
  %w(most_fields cross_fields phrase pharse_prefix).each do |type|
39
- filter = Elasticquery::Filters::Search.new 'hi', type: type
44
+ filter = Elasticquery::Filters::Search.new "hi", type: type
40
45
  assert filter.valid?
41
46
  end
42
- filter = Elasticquery::Filters::Search.new 'hi', type: 'random'
47
+ filter = Elasticquery::Filters::Search.new "hi", type: "random"
43
48
  refute filter.valid?
44
49
  end
45
50
 
46
51
  def test_to_hash_is_empty_if_invalid
47
- filter = Elasticquery::Filters::Search.new 'hi', fields: 42
52
+ filter = Elasticquery::Filters::Search.new "hi", fields: 42
48
53
  refute filter.valid?
49
54
  assert_equal filter.to_hash, {}
50
55
  end
51
56
 
52
57
  def test_dup_with_return_new_search_query
53
- filter = Elasticquery::Filters::Search.new 'hi', fields: 42
54
- new_filter = filter.dup_with 'who'
58
+ filter = Elasticquery::Filters::Search.new "hi", fields: 42
59
+ new_filter = filter.dup_with "who"
55
60
  assert_kind_of Elasticquery::Filters::Search, new_filter
56
61
  end
57
62
  end
@@ -1,5 +1,5 @@
1
- require 'test_helper'
2
- require 'elasticquery/filters/term'
1
+ require "test_helper"
2
+ require "elasticquery/filters/term"
3
3
 
4
4
  class TestTermfilter < MiniTest::Test
5
5
 
@@ -13,14 +13,29 @@ class TestTermfilter < MiniTest::Test
13
13
  refute filter.valid?
14
14
  end
15
15
 
16
- def test_valid_filter_should_have_2_and_more_keys
16
+ def test_invalid_filter_should_have_2_and_more_keys
17
17
  filter = Elasticquery::Filters::Term.new a: 1, b: 2
18
+ refute filter.valid?
19
+ end
20
+
21
+ def test_empty_condition_is_invalid
22
+ filter = Elasticquery::Filters::Term.new a: ""
23
+ refute filter.valid?
24
+ end
25
+
26
+ def test_valid_filter_could_have_cache_option
27
+ filter = Elasticquery::Filters::Term.new a: 1, _cache: false
18
28
  assert filter.valid?
19
29
  end
20
30
 
31
+ def test_to_hash_should_pass__cache_options
32
+ filter = Elasticquery::Filters::Term.new a: 1, _cache: true
33
+ assert_equal({query: {filtered: {filter: {and: [{term: {a: 1, _cache: true}}]}}}}, filter.to_hash)
34
+ end
35
+
21
36
  def test_to_hash_should_return_es_query
22
- filter = Elasticquery::Filters::Term.new a: 1, b: 2
23
- assert_equal({query: {filtered: {filter: {and: {term: {a: 1, b: 2}}}}}}, filter.to_hash)
37
+ filter = Elasticquery::Filters::Term.new a: 1
38
+ assert_equal({query: {filtered: {filter: {and: [{term: {a: 1}}]}}}}, filter.to_hash)
24
39
  end
25
40
 
26
41
  def test_to_hash_is_empty_if_invalid
@@ -1,5 +1,5 @@
1
- require 'test_helper'
2
- require 'elasticquery'
1
+ require "test_helper"
2
+ require "elasticquery"
3
3
 
4
4
  class TestChainableCalls < MiniTest::Test
5
5
 
@@ -12,17 +12,17 @@ class TestChainableCalls < MiniTest::Test
12
12
  end
13
13
 
14
14
  def test_apply_query
15
- query = @query.term(a: 1).term(a: 3)
16
- assert_equal query.build, query: {filtered: {filter: {and: [{term: {a: 3}}]}}}
15
+ query = @query.term(a: 1).term(b: 3)
16
+ assert_equal query.build, query: {filtered: {filter: {and: [{term: {a: 1}}, {term: {b: 3}}]}}}
17
17
  end
18
18
 
19
19
  def test_with_not_filter
20
- query = @query.search('hi').term.not(a: 1)
21
- assert_equal query.build, {query: {filtered: {query: {multi_match: {fields: "_all", operator: "and", type: "best_fields", query: "hi"}}, filter: {and: [{not: {filter: {term: {a: 1}}}}]}}}}
20
+ query = @query.search("hi").term.not(a: 1, _cache: true)
21
+ assert_equal query.build, {query: {filtered: {query: {multi_match: {fields: "_all", operator: "and", type: "best_fields", query: "hi"}}, filter: {and: [{not: {filter: {term: {a: 1, _cache: true}}}}]}}}}
22
22
  end
23
23
 
24
24
  def test_apply_kinds_of_query
25
- query = @query.term(a: 1).search('hello', operator: 'or').build
25
+ query = @query.term(a: 1).search("hello", operator: "or").build
26
26
  terms = query[:query][:filtered][:filter][:and]
27
27
  search = query[:query][:filtered][:query][:multi_match]
28
28
  assert_equal terms, [{term: {a: 1}}]