daedal 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -1
  3. data/.travis.yml +7 -0
  4. data/Gemfile +9 -7
  5. data/Gemfile.lock +47 -32
  6. data/README.md +115 -56
  7. data/daedal.gemspec +0 -1
  8. data/lib/daedal.rb +41 -5
  9. data/lib/daedal/attributes/boost.rb +7 -0
  10. data/lib/daedal/attributes/facet.rb +13 -0
  11. data/lib/daedal/attributes/field.rb +7 -0
  12. data/lib/daedal/attributes/filter.rb +2 -2
  13. data/lib/daedal/attributes/filter_array.rb +4 -4
  14. data/lib/daedal/attributes/query.rb +2 -2
  15. data/lib/daedal/attributes/query_array.rb +2 -2
  16. data/lib/daedal/attributes/query_value.rb +19 -0
  17. data/lib/daedal/facets/facet.rb +15 -0
  18. data/lib/daedal/filters/and_filter.rb +2 -5
  19. data/lib/daedal/filters/bool_filter.rb +15 -0
  20. data/lib/daedal/filters/{base_filter.rb → filter.rb} +1 -3
  21. data/lib/daedal/filters/geo_distance_filter.rb +6 -9
  22. data/lib/daedal/filters/or_filter.rb +18 -0
  23. data/lib/daedal/filters/range_filter.rb +8 -8
  24. data/lib/daedal/filters/term_filter.rb +3 -6
  25. data/lib/daedal/filters/terms_filter.rb +3 -6
  26. data/lib/daedal/queries/bool_query.rb +8 -11
  27. data/lib/daedal/queries/constant_score_query.rb +4 -8
  28. data/lib/daedal/queries/dis_max_query.rb +5 -9
  29. data/lib/daedal/queries/filtered_query.rb +3 -8
  30. data/lib/daedal/queries/fuzzy_query.rb +24 -0
  31. data/lib/daedal/queries/match_all_query.rb +1 -3
  32. data/lib/daedal/queries/match_query.rb +11 -15
  33. data/lib/daedal/queries/multi_match_query.rb +12 -16
  34. data/lib/daedal/queries/nested_query.rb +7 -10
  35. data/lib/daedal/queries/prefix_query.rb +7 -9
  36. data/lib/daedal/queries/{base_query.rb → query.rb} +1 -4
  37. data/lib/daedal/queries/query_string_query.rb +32 -0
  38. data/lib/daedal/version.rb +1 -1
  39. data/spec/spec_helper.rb +4 -2
  40. data/spec/unit/daedal/filters/and_filter_spec.rb +0 -1
  41. data/spec/unit/daedal/filters/bool_filter_spec.rb +89 -0
  42. data/spec/unit/daedal/filters/geo_distance_filter_spec.rb +0 -1
  43. data/spec/unit/daedal/filters/or_filter_spec.rb +74 -0
  44. data/spec/unit/daedal/filters/range_filter_spec.rb +0 -1
  45. data/spec/unit/daedal/filters/term_filter_spec.rb +0 -1
  46. data/spec/unit/daedal/filters/terms_filter_spec.rb +4 -2
  47. data/spec/unit/daedal/queries/bool_query_spec.rb +1 -3
  48. data/spec/unit/daedal/queries/constant_score_query_spec.rb +0 -2
  49. data/spec/unit/daedal/queries/dis_max_query_spec.rb +1 -2
  50. data/spec/unit/daedal/queries/filtered_query_spec.rb +1 -3
  51. data/spec/unit/daedal/queries/fuzzy_query_spec.rb +89 -0
  52. data/spec/unit/daedal/queries/match_all_query_spec.rb +0 -1
  53. data/spec/unit/daedal/queries/match_query_spec.rb +2 -3
  54. data/spec/unit/daedal/queries/multi_match_query_spec.rb +2 -3
  55. data/spec/unit/daedal/queries/nested_query_spec.rb +0 -2
  56. data/spec/unit/daedal/queries/prefix_query_spec.rb +0 -1
  57. data/spec/unit/daedal/queries/query_string_spec.rb +296 -0
  58. metadata +18 -7
  59. data/lib/daedal/attributes.rb +0 -15
  60. data/lib/daedal/filters.rb +0 -11
  61. data/lib/daedal/queries.rb +0 -15
@@ -0,0 +1,24 @@
1
+ module Daedal
2
+ module Queries
3
+ """Class for the fuzzy query"""
4
+ class FuzzyQuery < Query
5
+
6
+ # required attributes
7
+ attribute :field, Daedal::Attributes::Field
8
+ attribute :query, Daedal::Attributes::QueryValue
9
+
10
+ # non required attributes
11
+ attribute :boost, Daedal::Attributes::Boost, required: false
12
+ attribute :min_similarity, Daedal::Attributes::QueryValue, required: false
13
+ attribute :prefix_length, Integer, required: false
14
+
15
+ def to_hash
16
+ result = {fuzzy: {field => {value: query}}}
17
+ options = {boost: boost, min_similarity: min_similarity, prefix_length: prefix_length}.select {|k,v| !v.nil?}
18
+ result[:fuzzy][field].merge!(options)
19
+
20
+ result
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,9 +1,7 @@
1
- require 'daedal/queries/base_query'
2
-
3
1
  module Daedal
4
2
  module Queries
5
3
  """Class for the match all query"""
6
- class MatchAllQuery < BaseQuery
4
+ class MatchAllQuery < Query
7
5
  def to_hash
8
6
  {match_all: {}}
9
7
  end
@@ -1,25 +1,21 @@
1
- require 'daedal/queries/base_query'
2
- require 'daedal/attributes'
3
-
4
1
  module Daedal
5
2
  module Queries
6
-
7
3
  """Class for the match query"""
8
- class MatchQuery < BaseQuery
4
+ class MatchQuery < Query
9
5
 
10
6
  # required attributes
11
- attribute :field, Symbol
12
- attribute :query, Symbol
7
+ attribute :field, Daedal::Attributes::Field
8
+ attribute :query, Daedal::Attributes::QueryValue
13
9
 
14
10
  # non required attributes
15
- attribute :operator, Attributes::Operator, required: false
16
- attribute :minimum_should_match, Integer, required: false
17
- attribute :cutoff_frequency, Float, required: false
18
- attribute :type, Attributes::MatchType, required: false
19
- attribute :analyzer, Symbol, required: false
20
- attribute :boost, Integer, required: false
21
- attribute :fuzziness, Float, required: false
22
- attribute :slop, Integer, required: false
11
+ attribute :operator, Daedal::Attributes::Operator, required: false
12
+ attribute :minimum_should_match, Integer, required: false
13
+ attribute :cutoff_frequency, Float, required: false
14
+ attribute :type, Daedal::Attributes::MatchType, required: false
15
+ attribute :analyzer, Symbol, required: false
16
+ attribute :boost, Daedal::Attributes::Boost, required: false
17
+ attribute :fuzziness, Float, required: false
18
+ attribute :slop, Integer, required: false
23
19
 
24
20
  def to_hash
25
21
 
@@ -1,26 +1,22 @@
1
- require 'daedal/queries/base_query'
2
- require 'daedal/attributes'
3
-
4
1
  module Daedal
5
2
  module Queries
6
-
7
3
  """Class for the multi match query"""
8
- class MultiMatchQuery < BaseQuery
4
+ class MultiMatchQuery < Query
9
5
 
10
6
  # required attributes
11
- attribute :query, Symbol
12
- attribute :fields, Array[Symbol]
7
+ attribute :query, Daedal::Attributes::QueryValue
8
+ attribute :fields, Array[Daedal::Attributes::Field]
13
9
 
14
10
  # non required attributes
15
- attribute :use_dis_max, Boolean, default: true
16
- attribute :tie_breaker, Float, default: 0.0
17
- attribute :operator, Attributes::Operator, required: false
18
- attribute :minimum_should_match, Integer, required: false
19
- attribute :cutoff_frequency, Float, required: false
20
- attribute :type, Attributes::MatchType, required: false
21
- attribute :analyzer, Symbol, required: false
22
- attribute :boost, Integer, required: false
23
- attribute :fuzziness, Float, required: false
11
+ attribute :use_dis_max, Boolean, required: false
12
+ attribute :tie_breaker, Float, required: false
13
+ attribute :operator, Daedal::Attributes::Operator, required: false
14
+ attribute :minimum_should_match, Integer, required: false
15
+ attribute :cutoff_frequency, Float, required: false
16
+ attribute :type, Daedal::Attributes::MatchType, required: false
17
+ attribute :analyzer, Symbol, required: false
18
+ attribute :boost, Daedal::Attributes::Boost, required: false
19
+ attribute :fuzziness, Float, required: false
24
20
 
25
21
  # Fields cannot be an empty array... should eventually refactor this kind of thing out of initialize
26
22
  def initialize(options={})
@@ -1,23 +1,20 @@
1
- require 'daedal/queries/base_query'
2
- require 'daedal/attributes'
3
-
4
1
  module Daedal
5
2
  module Queries
6
3
  """Class for the bool query"""
7
- class NestedQuery < BaseQuery
4
+ class NestedQuery < Query
8
5
 
9
6
  # required attributes
10
- attribute :path, Symbol
11
- attribute :query, Attributes::Query
7
+ attribute :path, Daedal::Attributes::Field
8
+ attribute :query, Daedal::Attributes::Query
12
9
 
13
10
  # non required attributes
14
- attribute :score_mode, Attributes::ScoreMode, required: false
15
- attribute :name, Symbol, required: false
11
+ attribute :score_mode, Daedal::Attributes::ScoreMode, required: false
12
+ attribute :name, Symbol, required: false
16
13
 
17
14
  def to_hash
18
15
  result = {nested: {path: path, query: query.to_hash}}
19
- options = {score_mode: score_mode, _name: name}
20
- result[:nested].merge!(options.select { |k,v| !v.nil? })
16
+ options = {score_mode: score_mode, _name: name}.select { |k,v| !v.nil? }
17
+ result[:nested].merge! options
21
18
 
22
19
  result
23
20
  end
@@ -1,22 +1,20 @@
1
- require 'daedal/queries/base_query'
2
- require 'daedal/attributes'
3
-
4
1
  module Daedal
5
2
  module Queries
6
3
  """Class for the prefix query"""
7
- class PrefixQuery < BaseQuery
4
+ class PrefixQuery < Query
8
5
 
9
6
  # required attributes
10
- attribute :field, Symbol
11
- attribute :query, Attributes::LowerCaseString
7
+ attribute :field, Daedal::Attributes::Field
8
+ attribute :query, Daedal::Attributes::LowerCaseString
12
9
 
13
10
  # non required attributes
14
- attribute :boost, Float, required: false
11
+ attribute :boost, Daedal::Attributes::Boost, required: false
15
12
 
16
13
  def to_hash
17
14
  result = {prefix: {field => query}}
18
- options = {boost: boost}
19
- result[:prefix].merge!(options.select { |k,v| !v.nil? })
15
+ unless boost.nil?
16
+ result[:prefix][:boost] = boost
17
+ end
20
18
 
21
19
  result
22
20
  end
@@ -1,13 +1,10 @@
1
- require 'virtus'
2
-
3
1
  module Daedal
4
2
  module Queries
5
-
6
3
  """Base class for queries. All other queries
7
4
  should inherit from this class directly in order to
8
5
  allow for data-type coercion for compound queries
9
6
  like the bool or dis max query"""
10
- class BaseQuery
7
+ class Query
11
8
  # Virtus coercion is set to strict so that errors
12
9
  # are returned when supplied fields cannot be properly
13
10
  # coerced.
@@ -0,0 +1,32 @@
1
+ module Daedal
2
+ module Queries
3
+ """Class for the bool query"""
4
+ class QueryStringQuery < Query
5
+
6
+ # required attributes
7
+ attribute :query, String
8
+
9
+ # non required attributes
10
+ attribute :default_field, Daedal::Attributes::Field, required: false
11
+ attribute :fields, Array[Daedal::Attributes::Field], required: false
12
+ attribute :default_operator, String, required: false
13
+ attribute :analyzer, Symbol, required: false
14
+ attribute :allow_leading_wildcard, Boolean, required: false
15
+ attribute :lowercase_expanded_terms, Boolean, required: false
16
+ attribute :enable_position_increments, Boolean, required: false
17
+ attribute :fuzzy_max_expansions, Integer, required: false
18
+ attribute :fuzzy_min_sim, Float, required: false
19
+ attribute :fuzzy_prefix_length, Integer, required: false
20
+ attribute :phrase_slop, Integer, required: false
21
+ attribute :boost, Daedal::Attributes::Boost, required: false
22
+ attribute :analyze_wildcard, Boolean, required: false
23
+ attribute :auto_generate_phrase_queries, Boolean, required: false
24
+ attribute :minimum_should_match, Integer, required: false
25
+ attribute :lenient, Boolean, required: false
26
+
27
+ def to_hash
28
+ {query_string: attributes.select {|k,v| (k == :fields and !v.empty?) or (k != :fields and !v.nil?)}}
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module Daedal
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -1,5 +1,5 @@
1
- require 'require_all'
2
- require 'json'
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
3
 
4
4
  def append_load_path(*paths)
5
5
  full_path = File.join([File.dirname(__FILE__), ".."] << paths)
@@ -9,6 +9,8 @@ end
9
9
  append_load_path('')
10
10
  append_load_path('lib')
11
11
 
12
+ require 'daedal'
13
+
12
14
  RSpec.configure do |config|
13
15
  config.expect_with :rspec do |c|
14
16
  c.syntax = :expect
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'daedal/filters'
3
2
 
4
3
  describe Daedal::Filters::AndFilter do
5
4
 
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe Daedal::Filters::BoolFilter do
4
+
5
+ subject do
6
+ Daedal::Filters::BoolFilter
7
+ end
8
+
9
+ let(:term_filter) do
10
+ Daedal::Filters::TermFilter.new field: :foo, term: :bar
11
+ end
12
+
13
+ context 'without an initial array of filters' do
14
+ let(:filter) do
15
+ subject.new
16
+ end
17
+ let(:hash_filter) do
18
+ {bool: {should: [], must: [], must_not: []}}
19
+ end
20
+ it 'will set must should and must_not to empty arrays' do
21
+ expect(filter.must).to eq []
22
+ expect(filter.should).to eq []
23
+ expect(filter.must_not).to eq []
24
+ end
25
+ it 'will have the correct hash and json representations' do
26
+ expect(filter.to_hash).to eq hash_filter
27
+ expect(filter.to_json).to eq hash_filter.to_json
28
+ end
29
+ end
30
+
31
+ context 'with initial arrays of filters specified' do
32
+ let(:filter) do
33
+ subject.new should: [term_filter], must: [term_filter, term_filter], must_not: [term_filter, term_filter, term_filter]
34
+ end
35
+ let(:hash_filter) do
36
+ {bool: {should: [term_filter.to_hash], must: [term_filter.to_hash, term_filter.to_hash], must_not: [term_filter.to_hash, term_filter.to_hash, term_filter.to_hash]}}
37
+ end
38
+
39
+ it 'will have the correct filter array set' do
40
+ expect(filter.should).to eq [term_filter]
41
+ expect(filter.must).to eq [term_filter, term_filter]
42
+ expect(filter.must_not).to eq [term_filter, term_filter, term_filter]
43
+ end
44
+ it 'will have the correct hash and json representations' do
45
+ expect(filter.to_hash).to eq hash_filter
46
+ expect(filter.to_json).to eq hash_filter.to_json
47
+ end
48
+ end
49
+
50
+ context 'when adding more filters' do
51
+ let(:filter) do
52
+ subject.new
53
+ end
54
+
55
+ before do
56
+ filter.should << term_filter
57
+ filter.must << term_filter
58
+ filter.must << term_filter
59
+ filter.must_not << term_filter
60
+ filter.must_not << term_filter
61
+ filter.must_not << term_filter
62
+ end
63
+
64
+ it 'will append the filters to their arrays' do
65
+ expect(filter.should).to eq [term_filter]
66
+ expect(filter.must).to eq [term_filter, term_filter]
67
+ expect(filter.must_not).to eq [term_filter, term_filter, term_filter]
68
+ end
69
+ end
70
+
71
+ context 'with non filters during initialization' do
72
+ it 'will raise an error' do
73
+ expect{subject.new should: [:foo]}.to raise_error(Virtus::CoercionError)
74
+ expect{subject.new must: [:foo]}.to raise_error(Virtus::CoercionError)
75
+ expect{subject.new must_not: [:foo]}.to raise_error(Virtus::CoercionError)
76
+ end
77
+ end
78
+
79
+ context 'when trying to add a non filter' do
80
+ let(:filter) do
81
+ subject.new
82
+ end
83
+ it 'will raise an error' do
84
+ expect{filter.should << :foo}.to raise_error(Virtus::CoercionError)
85
+ expect{filter.must << :foo}.to raise_error(Virtus::CoercionError)
86
+ expect{filter.must_not << :foo}.to raise_error(Virtus::CoercionError)
87
+ end
88
+ end
89
+ end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'daedal/filters'
3
2
 
4
3
  describe Daedal::Filters::GeoDistanceFilter do
5
4
 
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Daedal::Filters::OrFilter do
4
+
5
+ subject do
6
+ Daedal::Filters::OrFilter
7
+ end
8
+
9
+ let(:term_filter) do
10
+ Daedal::Filters::TermFilter.new field: :foo, term: :bar
11
+ end
12
+
13
+ context 'without an initial array of filters' do
14
+ let(:filter) do
15
+ subject.new
16
+ end
17
+ let(:hash_filter) do
18
+ nil
19
+ end
20
+ it 'will set must should and must_not to empty arrays' do
21
+ expect(filter.filters).to eq []
22
+ end
23
+ it 'will have the correct hash and json representations' do
24
+ expect(filter.to_hash).to eq hash_filter
25
+ expect(filter.to_json).to eq hash_filter.to_json
26
+ end
27
+ end
28
+
29
+ context 'with initial array of filters specified' do
30
+ let(:filter) do
31
+ subject.new filters: [term_filter]
32
+ end
33
+ let(:hash_filter) do
34
+ {:or => [term_filter.to_hash]}
35
+ end
36
+
37
+ it 'will have the correct filter array set' do
38
+ expect(filter.filters).to eq [term_filter]
39
+ end
40
+ it 'will have the correct hash and json representations' do
41
+ expect(filter.to_hash).to eq hash_filter
42
+ expect(filter.to_json).to eq hash_filter.to_json
43
+ end
44
+ end
45
+
46
+ context 'when adding more filters' do
47
+ let(:filter) do
48
+ subject.new
49
+ end
50
+
51
+ before do
52
+ filter.filters << term_filter
53
+ end
54
+
55
+ it 'will append the filters to their arrays' do
56
+ expect(filter.filters).to eq [term_filter]
57
+ end
58
+ end
59
+
60
+ context 'with non filters during initialization' do
61
+ it 'will raise an error' do
62
+ expect{subject.new filters: [:foo]}.to raise_error(Virtus::CoercionError)
63
+ end
64
+ end
65
+
66
+ context 'when trying to add a non filter' do
67
+ let(:filter) do
68
+ subject.new
69
+ end
70
+ it 'will raise an error' do
71
+ expect{filter.filters << :foo}.to raise_error(Virtus::CoercionError)
72
+ end
73
+ end
74
+ end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'daedal/filters'
3
2
 
4
3
  describe Daedal::Filters::RangeFilter do
5
4
 
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'daedal/filters'
3
2
 
4
3
  describe Daedal::Filters::TermFilter do
5
4
 
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'daedal/filters'
3
2
 
4
3
  describe Daedal::Filters::TermsFilter do
5
4
 
@@ -35,9 +34,12 @@ describe Daedal::Filters::TermsFilter do
35
34
  let(:filter) do
36
35
  subject.new(field: field, terms: terms.map {|t| t.to_s})
37
36
  end
37
+ let(:hash_filter) do
38
+ {terms: {field => terms.map {|t| t.to_s}}}
39
+ end
38
40
  it 'will populate the field and term attributes appropriately' do
39
41
  expect(filter.field).to eq field
40
- expect(filter.terms).to eq terms
42
+ expect(filter.terms).to eq terms.map {|t| t.to_s}
41
43
  end
42
44
  it 'will have the correct hash representation' do
43
45
  expect(filter.to_hash).to eq hash_filter