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
@@ -1,54 +0,0 @@
1
- require_relative "base"
2
-
3
- module Elasticquery
4
- module Filters
5
- class Search < Base
6
- OPERATORS = %w(and or)
7
- TYPES = %w(best_fields most_fields cross_fields phrase pharse_prefix)
8
-
9
- # Create new search subquery
10
- #
11
- # @params [String] query keyword
12
- # @params [Array<String>] fields to search with. Default to "_all"
13
- # @params [String] operator search option
14
- # @params [String] type search option
15
- def initialize(query, fields: "_all", operator: "and", type: "best_fields")
16
- @fields = fields
17
- @operator = operator
18
- @type = type
19
- @query = query
20
- end
21
-
22
- # Is current query valid to exec
23
- #
24
- # @return [Boolean]
25
- #
26
- # @example
27
- # filter = Elasticquery::Filters::Search.new 'hello'
28
- # filter.valid? #=> true
29
- def valid?
30
- OPERATORS.include?(@operator) &&
31
- TYPES.include?(@type) &&
32
- @query.present? &&
33
- ( Array === @fields || @fields == "_all" )
34
- end
35
-
36
- # Hash presentation of query.
37
- #
38
- # @return [Hash] presentation of filter.
39
- #
40
- # @example
41
- # r = Elasticquery::Filters::Search.new { fields: ['name', 'country'], query: 'belarus' }
42
- # r.to_hash #=> {query: {filtered: {query: {multi_match: {fields: ['name', 'country'], query: 'belarus'}}}}}
43
- def to_hash
44
- valid? ? {query: {filtered: {query: {multi_match: subquery}}}} : {}
45
- end
46
-
47
- private
48
-
49
- def subquery
50
- {fields: @fields, operator: @operator, type: @type, query: @query}
51
- end
52
- end
53
- end
54
- end
@@ -1,62 +0,0 @@
1
- require "test_helper"
2
- require "elasticquery/filters/search"
3
-
4
- class TestSearchFilter < MiniTest::Test
5
-
6
- def test_default_values_of_filter
7
- filter = Elasticquery::Filters::Search.new "hi"
8
- actual = filter.to_hash[:query][:filtered][:query][:multi_match]
9
- assert_equal actual, {fields: "_all", operator: "and", type: "best_fields", query: "hi"}
10
- end
11
-
12
- def test_passed_parameters
13
- assert_raises ArgumentError do
14
- Elasticquery::Filters::Search.new
15
- end
16
- end
17
-
18
- def test_empty_query_is_invalid
19
- filter = Elasticquery::Filters::Search.new " "
20
- assert filter.invalid?
21
- end
22
-
23
- def test_default_query_should_be_valid
24
- filter = Elasticquery::Filters::Search.new "hi"
25
- assert filter.valid?
26
- end
27
-
28
- def test_query_fields_validation
29
- filter = Elasticquery::Filters::Search.new "hi", fields: 42
30
- refute filter.valid?
31
- filter = Elasticquery::Filters::Search.new "hi", fields: %w(name body)
32
- assert filter.valid?
33
- end
34
-
35
- def test_operator_validation
36
- filter = Elasticquery::Filters::Search.new "hi", operator: "without"
37
- refute filter.valid?
38
- filter = Elasticquery::Filters::Search.new "hi", operator: "or"
39
- assert filter.valid?
40
- end
41
-
42
- def test_types_validation
43
- %w(most_fields cross_fields phrase pharse_prefix).each do |type|
44
- filter = Elasticquery::Filters::Search.new "hi", type: type
45
- assert filter.valid?
46
- end
47
- filter = Elasticquery::Filters::Search.new "hi", type: "random"
48
- refute filter.valid?
49
- end
50
-
51
- def test_to_hash_is_empty_if_invalid
52
- filter = Elasticquery::Filters::Search.new "hi", fields: 42
53
- refute filter.valid?
54
- assert_equal filter.to_hash, {}
55
- end
56
-
57
- def test_dup_with_return_new_search_query
58
- filter = Elasticquery::Filters::Search.new "hi", fields: 42
59
- new_filter = filter.dup_with "who"
60
- assert_kind_of Elasticquery::Filters::Search, new_filter
61
- end
62
- end