eson-dsl 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.md +20 -0
- data/README.md +179 -0
- data/Rakefile +30 -0
- data/TODO.md +1 -0
- data/eson-dsl.gemspec +21 -0
- data/examples/facets/date_histogram.rb +5 -0
- data/examples/facets/filter.rb +7 -0
- data/examples/facets/geo_distance.rb +14 -0
- data/examples/facets/histogram.rb +5 -0
- data/examples/facets/query.rb +7 -0
- data/examples/facets/range.rb +8 -0
- data/examples/facets/scoped.rb +17 -0
- data/examples/facets/statistical.rb +5 -0
- data/examples/facets/term_stats.rb +9 -0
- data/examples/facets/terms.rb +8 -0
- data/examples/filters/and.rb +11 -0
- data/examples/filters/and_without_keyword.rb +9 -0
- data/examples/filters/bool.rb +16 -0
- data/examples/filters/exists.rb +9 -0
- data/examples/filters/fquery.rb +11 -0
- data/examples/filters/geo_bounding_box.rb +8 -0
- data/examples/filters/geo_distance.rb +8 -0
- data/examples/filters/geo_distance_alternate.rb +9 -0
- data/examples/filters/geo_distance_range.rb +10 -0
- data/examples/filters/geo_polygon.rb +10 -0
- data/examples/filters/has_child.rb +9 -0
- data/examples/filters/ids.rb +8 -0
- data/examples/filters/missing.rb +9 -0
- data/examples/filters/not.rb +10 -0
- data/examples/filters/numeric_range.rb +9 -0
- data/examples/filters/or.rb +11 -0
- data/examples/filters/range.rb +5 -0
- data/examples/filters/type.rb +5 -0
- data/examples/queries/bool.rb +16 -0
- data/examples/queries/boosting.rb +12 -0
- data/examples/queries/constant_score.rb +9 -0
- data/examples/queries/constant_score_with_filter.rb +9 -0
- data/examples/queries/custom_score.rb +15 -0
- data/examples/queries/dis_max.rb +10 -0
- data/examples/queries/field.rb +5 -0
- data/examples/queries/filters_and_facets.rb +11 -0
- data/examples/queries/flt.rb +7 -0
- data/examples/queries/flt_field.rb +7 -0
- data/examples/queries/fuzzy.rb +5 -0
- data/examples/queries/fuzzy_complex.rb +5 -0
- data/examples/queries/has_child.rb +9 -0
- data/examples/queries/has_child_complex.rb +12 -0
- data/examples/queries/ids.rb +5 -0
- data/examples/queries/match_all.rb +5 -0
- data/examples/queries/mlt.rb +7 -0
- data/examples/queries/mlt_field.rb +7 -0
- data/examples/queries/nested.rb +12 -0
- data/examples/queries/prefix.rb +5 -0
- data/examples/queries/prefix_short.rb +5 -0
- data/examples/queries/query_string.rb +6 -0
- data/examples/queries/query_string_with_multiple_fields.rb +7 -0
- data/examples/queries/query_string_without_default_field.rb +5 -0
- data/examples/queries/range.rb +5 -0
- data/examples/queries/span_first.rb +7 -0
- data/examples/queries/span_near.rb +11 -0
- data/examples/queries/span_not.rb +8 -0
- data/examples/queries/span_or.rb +11 -0
- data/examples/queries/span_term.rb +5 -0
- data/examples/queries/term.rb +5 -0
- data/examples/queries/term_with_parameters.rb +5 -0
- data/examples/queries/terms.rb +5 -0
- data/examples/queries/text.rb +5 -0
- data/examples/queries/top_children.rb +7 -0
- data/examples/queries/wildcard.rb +5 -0
- data/examples/queries/wildcard_short.rb +5 -0
- data/lib/eson/modules/query_plugin.rb +44 -0
- data/lib/eson/search/and.rb +27 -0
- data/lib/eson/search/base_query.rb +39 -0
- data/lib/eson/search/bool.rb +51 -0
- data/lib/eson/search/boosting.rb +45 -0
- data/lib/eson/search/constant_score.rb +21 -0
- data/lib/eson/search/custom_score.rb +19 -0
- data/lib/eson/search/date_histogram.rb +13 -0
- data/lib/eson/search/dis_max.rb +22 -0
- data/lib/eson/search/exists.rb +18 -0
- data/lib/eson/search/facet.rb +39 -0
- data/lib/eson/search/facets.rb +37 -0
- data/lib/eson/search/field.rb +16 -0
- data/lib/eson/search/field_based.rb +24 -0
- data/lib/eson/search/filter.rb +28 -0
- data/lib/eson/search/filter_facet.rb +13 -0
- data/lib/eson/search/filters.rb +69 -0
- data/lib/eson/search/fquery.rb +22 -0
- data/lib/eson/search/fuzzy.rb +29 -0
- data/lib/eson/search/fuzzy_like_this.rb +16 -0
- data/lib/eson/search/fuzzy_like_this_field.rb +20 -0
- data/lib/eson/search/geo_bounding_box.rb +35 -0
- data/lib/eson/search/geo_distance.rb +34 -0
- data/lib/eson/search/geo_distance_range.rb +29 -0
- data/lib/eson/search/geo_polygon.rb +32 -0
- data/lib/eson/search/has_child.rb +29 -0
- data/lib/eson/search/histogram.rb +20 -0
- data/lib/eson/search/ids.rb +27 -0
- data/lib/eson/search/match_all.rb +21 -0
- data/lib/eson/search/missing.rb +21 -0
- data/lib/eson/search/more_like_this.rb +16 -0
- data/lib/eson/search/more_like_this_field.rb +20 -0
- data/lib/eson/search/nested.rb +24 -0
- data/lib/eson/search/node.rb +28 -0
- data/lib/eson/search/not.rb +23 -0
- data/lib/eson/search/numeric_range.rb +20 -0
- data/lib/eson/search/or.rb +23 -0
- data/lib/eson/search/parametrized.rb +15 -0
- data/lib/eson/search/prefix.rb +22 -0
- data/lib/eson/search/queries.rb +65 -0
- data/lib/eson/search/query.rb +28 -0
- data/lib/eson/search/query_facet.rb +16 -0
- data/lib/eson/search/query_field.rb +33 -0
- data/lib/eson/search/query_group.rb +34 -0
- data/lib/eson/search/query_string.rb +20 -0
- data/lib/eson/search/range.rb +26 -0
- data/lib/eson/search/span_first.rb +27 -0
- data/lib/eson/search/span_near.rb +29 -0
- data/lib/eson/search/span_not.rb +29 -0
- data/lib/eson/search/span_or.rb +29 -0
- data/lib/eson/search/span_term.rb +18 -0
- data/lib/eson/search/statistical.rb +20 -0
- data/lib/eson/search/sub_query.rb +7 -0
- data/lib/eson/search/term.rb +22 -0
- data/lib/eson/search/terms.rb +22 -0
- data/lib/eson/search/terms_stats.rb +20 -0
- data/lib/eson/search/text.rb +23 -0
- data/lib/eson/search/top_children.rb +28 -0
- data/lib/eson/search/type.rb +20 -0
- data/lib/eson/search/wildcard.rb +18 -0
- data/lib/eson-dsl.rb +64 -0
- data/log4j.properties +18 -0
- data/test/search/facet_test.rb +92 -0
- data/test/search/facets/date_histogram.documents +1 -0
- data/test/search/facets/date_histogram.mapping +1 -0
- data/test/search/facets/date_histogram.rb +13 -0
- data/test/search/facets/geo_distance.documents +1 -0
- data/test/search/facets/geo_distance.mapping +8 -0
- data/test/search/facets/geo_distance.rb +1 -0
- data/test/search/facets/histogram.documents +1 -0
- data/test/search/facets/histogram.rb +13 -0
- data/test/search/facets/query.documents +1 -0
- data/test/search/facets/query.rb +14 -0
- data/test/search/facets/range.documents +1 -0
- data/test/search/facets/range.rb +17 -0
- data/test/search/facets/scoped.documents +1 -0
- data/test/search/facets/scoped.json +1 -0
- data/test/search/facets/scoped.mapping +9 -0
- data/test/search/facets/statistical.documents +1 -0
- data/test/search/facets/statistical.rb +12 -0
- data/test/search/facets/tag_facet.documents +1 -0
- data/test/search/facets/tag_facet.rb +14 -0
- data/test/search/facets/terms.rb +14 -0
- data/test/search/facets/terms_stats.documents +1 -0
- data/test/search/facets/terms_stats.json +1 -0
- data/test/search/facets/terms_stats.rb +13 -0
- data/test/search/filters/and.rb +15 -0
- data/test/search/filters/bool.json +1 -0
- data/test/search/filters/bool.rb +27 -0
- data/test/search/filters/exists.json +1 -0
- data/test/search/filters/exists.rb +1 -0
- data/test/search/filters/filter_query.rb +14 -0
- data/test/search/filters/geo_bounding_box_hash.documents +1 -0
- data/test/search/filters/geo_bounding_box_hash.json +1 -0
- data/test/search/filters/geo_bounding_box_hash.mapping +8 -0
- data/test/search/filters/geo_distance_hash.documents +1 -0
- data/test/search/filters/geo_distance_hash.json +1 -0
- data/test/search/filters/geo_distance_hash.mapping +8 -0
- data/test/search/filters/geo_distance_range_hash.documents +1 -0
- data/test/search/filters/geo_distance_range_hash.json +1 -0
- data/test/search/filters/geo_distance_range_hash.mapping +8 -0
- data/test/search/filters/geo_polygon_hash.documents +1 -0
- data/test/search/filters/geo_polygon_hash.json +1 -0
- data/test/search/filters/geo_polygon_hash.mapping +8 -0
- data/test/search/filters/has_child.blog.mapping +1 -0
- data/test/search/filters/has_child.blog_tag.mapping +1 -0
- data/test/search/filters/has_child.rb +15 -0
- data/test/search/filters/ids.rb +14 -0
- data/test/search/filters/missing.rb +1 -0
- data/test/search/filters/not.rb +16 -0
- data/test/search/filters/numeric_range.documents +1 -0
- data/test/search/filters/numeric_range.rb +1 -0
- data/test/search/filters/or.rb +15 -0
- data/test/search/filters/range.rb +16 -0
- data/test/search/filters/type.json +1 -0
- data/test/search/filters/type.rb +1 -0
- data/test/search/filters_test.rb +166 -0
- data/test/search/queries/bool.json +1 -0
- data/test/search/queries/bool.rb +21 -0
- data/test/search/queries/boosting.json +1 -0
- data/test/search/queries/boosting.rb +17 -0
- data/test/search/queries/constant_score.rb +10 -0
- data/test/search/queries/constant_score_filter.rb +10 -0
- data/test/search/queries/custom_score.documents +1 -0
- data/test/search/queries/custom_score.rb +14 -0
- data/test/search/queries/dis_max.rb +15 -0
- data/test/search/queries/field.rb +7 -0
- data/test/search/queries/filters_and_facets.documents +1 -0
- data/test/search/queries/filters_and_facets.json +1 -0
- data/test/search/queries/flt.rb +9 -0
- data/test/search/queries/flt_field.rb +10 -0
- data/test/search/queries/fuzzy_complex.rb +5 -0
- data/test/search/queries/fuzzy_simple.rb +5 -0
- data/test/search/queries/has_child.blog.mapping +1 -0
- data/test/search/queries/has_child.blog_tag.mapping +1 -0
- data/test/search/queries/has_child.rb +10 -0
- data/test/search/queries/has_child_complex.blog.mapping +1 -0
- data/test/search/queries/has_child_complex.blog_tag.mapping +1 -0
- data/test/search/queries/has_child_complex.rb +17 -0
- data/test/search/queries/ids.rb +1 -0
- data/test/search/queries/match_all.rb +1 -0
- data/test/search/queries/mlt.json +1 -0
- data/test/search/queries/mlt.rb +9 -0
- data/test/search/queries/mlt_field.rb +10 -0
- data/test/search/queries/nested.json +1 -0
- data/test/search/queries/nested.mapping +9 -0
- data/test/search/queries/phrase_prefix.rb +8 -0
- data/test/search/queries/prefix.rb +1 -0
- data/test/search/queries/prefix_short.rb +1 -0
- data/test/search/queries/query_string.rb +8 -0
- data/test/search/queries/query_string_with_multiple_fields.rb +9 -0
- data/test/search/queries/query_string_without_default_field.rb +7 -0
- data/test/search/queries/range.rb +11 -0
- data/test/search/queries/span_near.json +1 -0
- data/test/search/queries/span_not.json +1 -0
- data/test/search/queries/span_or.json +1 -0
- data/test/search/queries/span_term.json +1 -0
- data/test/search/queries/term.json +1 -0
- data/test/search/queries/term.rb +1 -0
- data/test/search/queries/terms.rb +1 -0
- data/test/search/queries/text.json +1 -0
- data/test/search/queries/text.rb +8 -0
- data/test/search/queries/top_children.blog.mapping +1 -0
- data/test/search/queries/top_children.blog_tag.mapping +1 -0
- data/test/search/queries/top_children.rb +11 -0
- data/test/search/queries/wildcard.rb +1 -0
- data/test/search/queries/wildcard_short.rb +1 -0
- data/test/search/query_test.rb +311 -0
- data/test/test_config.rb +157 -0
- metadata +413 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
module Eson
|
2
|
+
module QueryPlugin
|
3
|
+
def query
|
4
|
+
if block_given?
|
5
|
+
@query ||= Eson::Search::BaseQuery.new
|
6
|
+
@query.instance_exec(@query, &Proc.new)
|
7
|
+
end
|
8
|
+
|
9
|
+
@query
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(*args)
|
13
|
+
if query.respond_to?(:to_query_hash)
|
14
|
+
self.params = query.to_query_hash
|
15
|
+
end
|
16
|
+
|
17
|
+
super(*args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(sym, *args, &block)
|
21
|
+
if query.query.respond_to?(sym)
|
22
|
+
query.query.send(sym, *args, &block)
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.plugin_for(protocol)
|
29
|
+
[protocol::Search,
|
30
|
+
protocol::Count,
|
31
|
+
protocol::DeleteByQuery,
|
32
|
+
protocol::Percolate]
|
33
|
+
end
|
34
|
+
|
35
|
+
def handle_block(&block)
|
36
|
+
# in ruby 1.8.7, `lambda {}.arity == -1`
|
37
|
+
if (block.arity == 0) || (block.arity == -1)
|
38
|
+
query(&block)
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class And
|
4
|
+
include Filters
|
5
|
+
include FilterMethods
|
6
|
+
include Filter
|
7
|
+
|
8
|
+
##
|
9
|
+
# @macro and
|
10
|
+
# @yield the block describing all subfilters
|
11
|
+
# Generates a `$1` filter.
|
12
|
+
# {include:file:eson-dsl/examples/filters/$1.rb}
|
13
|
+
short_name 'and'
|
14
|
+
|
15
|
+
attr_accessor :options
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
self.options = options
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_query_hash
|
22
|
+
{ name => filters.to_query_hash }
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class BaseQuery
|
4
|
+
include QueryGroup
|
5
|
+
include Facets
|
6
|
+
include Parametrized
|
7
|
+
|
8
|
+
def initialize(args = {})
|
9
|
+
self.args = args
|
10
|
+
instance_exec(self, &Proc.new) if block_given?
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_query_hash
|
14
|
+
if (!queries || queries.empty?)
|
15
|
+
hash = {:query => {:match_all => {}}}
|
16
|
+
else
|
17
|
+
hash = {:query => queries.to_query_hash }
|
18
|
+
end
|
19
|
+
|
20
|
+
if filters && !filters.empty?
|
21
|
+
hash = {
|
22
|
+
:query => {
|
23
|
+
:filtered => {
|
24
|
+
:query => hash[:query],
|
25
|
+
:filter => filters.to_query_hash
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
if facets.length > 0
|
32
|
+
hash[:facets] = facets.to_query_hash
|
33
|
+
end
|
34
|
+
|
35
|
+
hash
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class Bool
|
4
|
+
include Filter
|
5
|
+
include Query
|
6
|
+
|
7
|
+
# @macro bool
|
8
|
+
# @yield the block containing all subqueries
|
9
|
+
# Generates a `$1` filter or query, depending on context.
|
10
|
+
# {include:file:eson-dsl/examples/filters/$1.rb}
|
11
|
+
# {include:file:eson-dsl/examples/queries/$1.rb}
|
12
|
+
short_name :bool
|
13
|
+
|
14
|
+
attr_accessor :options, :conditions
|
15
|
+
|
16
|
+
def initialize(options = {})
|
17
|
+
self.options = options
|
18
|
+
end
|
19
|
+
|
20
|
+
def conditions
|
21
|
+
@conditions ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def must(&block)
|
25
|
+
q = (conditions[:must] ||= SubQuery.new(args))
|
26
|
+
q.send(context, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def must_not(&block)
|
30
|
+
q = (conditions[:must_not] ||= SubQuery.new(args))
|
31
|
+
q.send(context, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def should(&block)
|
35
|
+
q = (conditions[:should] ||= SubQuery.new(args))
|
36
|
+
q.send(context, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_query_hash
|
40
|
+
result = {}
|
41
|
+
|
42
|
+
conditions.each do |k,v|
|
43
|
+
result[k] = v.to_query_hash
|
44
|
+
end
|
45
|
+
|
46
|
+
{name => result.merge(options)}
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class Boosting
|
4
|
+
include Query
|
5
|
+
|
6
|
+
##
|
7
|
+
# @macro eson.query
|
8
|
+
# @yield the block describing all subqueries
|
9
|
+
# Generates a `$1` query.
|
10
|
+
# {include:file:eson-dsl/examples/queries/$1.rb}
|
11
|
+
short_name :boosting
|
12
|
+
|
13
|
+
attr_accessor :options, :conditions
|
14
|
+
|
15
|
+
def initialize(options = {})
|
16
|
+
self.options = options
|
17
|
+
end
|
18
|
+
|
19
|
+
def boosts
|
20
|
+
@boosts ||= {}
|
21
|
+
end
|
22
|
+
|
23
|
+
def positive(&block)
|
24
|
+
q = (boosts[:positive] ||= SubQuery.new(args))
|
25
|
+
q.query(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def negative(&block)
|
29
|
+
q = (boosts[:negative] ||= SubQuery.new(args))
|
30
|
+
q.query(&block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_query_hash
|
34
|
+
result = {}
|
35
|
+
|
36
|
+
boosts.each do |k,v|
|
37
|
+
result[k] = v.to_query_hash
|
38
|
+
end
|
39
|
+
|
40
|
+
{name => result.merge(options)}
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class ConstantScore
|
4
|
+
|
5
|
+
include Filters
|
6
|
+
include Queries
|
7
|
+
|
8
|
+
include Query
|
9
|
+
|
10
|
+
# @macro eson.query
|
11
|
+
short_name :constant_score
|
12
|
+
|
13
|
+
attr_accessor :options
|
14
|
+
|
15
|
+
def initialize(options = {})
|
16
|
+
self.options = options
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class CustomScore
|
4
|
+
|
5
|
+
include Queries
|
6
|
+
include Query
|
7
|
+
|
8
|
+
# @macro eson.query
|
9
|
+
short_name :custom_score
|
10
|
+
|
11
|
+
attr_accessor :options
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
self.options = options
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class DisMax
|
4
|
+
include Query
|
5
|
+
include Queries
|
6
|
+
|
7
|
+
# @macro eson.query
|
8
|
+
short_name :dis_max
|
9
|
+
|
10
|
+
attr_accessor :options
|
11
|
+
|
12
|
+
def initialize(options = {})
|
13
|
+
self.options = options
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_query_hash
|
17
|
+
{name => {:queries => queries.to_query_hash}.merge(options) }
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class Exists < FieldBased
|
4
|
+
include Filter
|
5
|
+
|
6
|
+
# @macro eson.filter
|
7
|
+
short_name :exists
|
8
|
+
|
9
|
+
def to_query_hash
|
10
|
+
if field
|
11
|
+
{name => field.to_query_hash}
|
12
|
+
else
|
13
|
+
{name => options}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
module Facet
|
4
|
+
module ClassMethods
|
5
|
+
def short_name(name)
|
6
|
+
define_method :name do
|
7
|
+
name
|
8
|
+
end
|
9
|
+
Facets.register name, self
|
10
|
+
|
11
|
+
super if defined? super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.included(base)
|
16
|
+
base.class_eval do
|
17
|
+
extend ClassMethods
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_accessor :args
|
22
|
+
|
23
|
+
def param(name)
|
24
|
+
args[name] || (raise "Parameter #{name} not given")
|
25
|
+
end
|
26
|
+
|
27
|
+
def scope(query, name)
|
28
|
+
raise "Cannot scope unless Query is a NestedQuery" unless Nested === query
|
29
|
+
query.options[:_scope] = name
|
30
|
+
self.options[:scope] = name
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_missing(name, arg)
|
34
|
+
warn("#{name} called as a facet option")
|
35
|
+
self.options[name] = arg
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
module Facets
|
4
|
+
class FacetArray < Array
|
5
|
+
include Parametrized
|
6
|
+
|
7
|
+
def initialize(args)
|
8
|
+
self.args = args
|
9
|
+
instance_exec(self, &Proc.new) if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_query_hash
|
13
|
+
h = {}
|
14
|
+
self.each do |k,v|
|
15
|
+
h[k] = v.to_query_hash
|
16
|
+
end
|
17
|
+
h
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.register(name, klass)
|
22
|
+
FacetArray.__send__(:define_method, name) do |facet_name, *args, &block|
|
23
|
+
o = klass.new(*args)
|
24
|
+
o.args = self.args
|
25
|
+
o.instance_exec(o, &block) if block
|
26
|
+
self << [facet_name, o]
|
27
|
+
o
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def facets(&block)
|
32
|
+
@facets ||= FacetArray.new(args, &block)
|
33
|
+
end
|
34
|
+
alias :facet :facets
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Eson
|
2
|
+
module Search
|
3
|
+
class FieldBased
|
4
|
+
attr_accessor :field
|
5
|
+
attr_accessor :options
|
6
|
+
|
7
|
+
def initialize(*args)
|
8
|
+
if args.length == 1
|
9
|
+
self.options = args.first
|
10
|
+
else
|
11
|
+
self.field = QueryField.new(*args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_query_hash
|
16
|
+
if field
|
17
|
+
{name => field.to_query_hash}
|
18
|
+
else
|
19
|
+
{name => options}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|