daedal 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -1
- data/.travis.yml +7 -0
- data/Gemfile +9 -7
- data/Gemfile.lock +47 -32
- data/README.md +115 -56
- data/daedal.gemspec +0 -1
- data/lib/daedal.rb +41 -5
- data/lib/daedal/attributes/boost.rb +7 -0
- data/lib/daedal/attributes/facet.rb +13 -0
- data/lib/daedal/attributes/field.rb +7 -0
- data/lib/daedal/attributes/filter.rb +2 -2
- data/lib/daedal/attributes/filter_array.rb +4 -4
- data/lib/daedal/attributes/query.rb +2 -2
- data/lib/daedal/attributes/query_array.rb +2 -2
- data/lib/daedal/attributes/query_value.rb +19 -0
- data/lib/daedal/facets/facet.rb +15 -0
- data/lib/daedal/filters/and_filter.rb +2 -5
- data/lib/daedal/filters/bool_filter.rb +15 -0
- data/lib/daedal/filters/{base_filter.rb → filter.rb} +1 -3
- data/lib/daedal/filters/geo_distance_filter.rb +6 -9
- data/lib/daedal/filters/or_filter.rb +18 -0
- data/lib/daedal/filters/range_filter.rb +8 -8
- data/lib/daedal/filters/term_filter.rb +3 -6
- data/lib/daedal/filters/terms_filter.rb +3 -6
- data/lib/daedal/queries/bool_query.rb +8 -11
- data/lib/daedal/queries/constant_score_query.rb +4 -8
- data/lib/daedal/queries/dis_max_query.rb +5 -9
- data/lib/daedal/queries/filtered_query.rb +3 -8
- data/lib/daedal/queries/fuzzy_query.rb +24 -0
- data/lib/daedal/queries/match_all_query.rb +1 -3
- data/lib/daedal/queries/match_query.rb +11 -15
- data/lib/daedal/queries/multi_match_query.rb +12 -16
- data/lib/daedal/queries/nested_query.rb +7 -10
- data/lib/daedal/queries/prefix_query.rb +7 -9
- data/lib/daedal/queries/{base_query.rb → query.rb} +1 -4
- data/lib/daedal/queries/query_string_query.rb +32 -0
- data/lib/daedal/version.rb +1 -1
- data/spec/spec_helper.rb +4 -2
- data/spec/unit/daedal/filters/and_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/bool_filter_spec.rb +89 -0
- data/spec/unit/daedal/filters/geo_distance_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/or_filter_spec.rb +74 -0
- data/spec/unit/daedal/filters/range_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/term_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/terms_filter_spec.rb +4 -2
- data/spec/unit/daedal/queries/bool_query_spec.rb +1 -3
- data/spec/unit/daedal/queries/constant_score_query_spec.rb +0 -2
- data/spec/unit/daedal/queries/dis_max_query_spec.rb +1 -2
- data/spec/unit/daedal/queries/filtered_query_spec.rb +1 -3
- data/spec/unit/daedal/queries/fuzzy_query_spec.rb +89 -0
- data/spec/unit/daedal/queries/match_all_query_spec.rb +0 -1
- data/spec/unit/daedal/queries/match_query_spec.rb +2 -3
- data/spec/unit/daedal/queries/multi_match_query_spec.rb +2 -3
- data/spec/unit/daedal/queries/nested_query_spec.rb +0 -2
- data/spec/unit/daedal/queries/prefix_query_spec.rb +0 -1
- data/spec/unit/daedal/queries/query_string_spec.rb +296 -0
- metadata +18 -7
- data/lib/daedal/attributes.rb +0 -15
- data/lib/daedal/filters.rb +0 -11
- 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,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 <
|
4
|
+
class MatchQuery < Query
|
9
5
|
|
10
6
|
# required attributes
|
11
|
-
attribute :field,
|
12
|
-
attribute :query,
|
7
|
+
attribute :field, Daedal::Attributes::Field
|
8
|
+
attribute :query, Daedal::Attributes::QueryValue
|
13
9
|
|
14
10
|
# non required attributes
|
15
|
-
attribute :operator,
|
16
|
-
attribute :minimum_should_match,
|
17
|
-
attribute :cutoff_frequency,
|
18
|
-
attribute :type,
|
19
|
-
attribute :analyzer,
|
20
|
-
attribute :boost,
|
21
|
-
attribute :fuzziness,
|
22
|
-
attribute :slop,
|
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 <
|
4
|
+
class MultiMatchQuery < Query
|
9
5
|
|
10
6
|
# required attributes
|
11
|
-
attribute :query,
|
12
|
-
attribute :fields,
|
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,
|
16
|
-
attribute :tie_breaker,
|
17
|
-
attribute :operator,
|
18
|
-
attribute :minimum_should_match,
|
19
|
-
attribute :cutoff_frequency,
|
20
|
-
attribute :type,
|
21
|
-
attribute :analyzer,
|
22
|
-
attribute :boost,
|
23
|
-
attribute :fuzziness,
|
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 <
|
4
|
+
class NestedQuery < Query
|
8
5
|
|
9
6
|
# required attributes
|
10
|
-
attribute :path,
|
11
|
-
attribute :query,
|
7
|
+
attribute :path, Daedal::Attributes::Field
|
8
|
+
attribute :query, Daedal::Attributes::Query
|
12
9
|
|
13
10
|
# non required attributes
|
14
|
-
attribute :score_mode,
|
15
|
-
attribute :name,
|
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!
|
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 <
|
4
|
+
class PrefixQuery < Query
|
8
5
|
|
9
6
|
# required attributes
|
10
|
-
attribute :field,
|
11
|
-
attribute :query,
|
7
|
+
attribute :field, Daedal::Attributes::Field
|
8
|
+
attribute :query, Daedal::Attributes::LowerCaseString
|
12
9
|
|
13
10
|
# non required attributes
|
14
|
-
attribute :boost,
|
11
|
+
attribute :boost, Daedal::Attributes::Boost, required: false
|
15
12
|
|
16
13
|
def to_hash
|
17
14
|
result = {prefix: {field => query}}
|
18
|
-
|
19
|
-
|
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
|
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
|
data/lib/daedal/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require '
|
2
|
-
|
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
|
@@ -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
|
@@ -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::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
|