eson-dsl 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/MethodReference.md +53 -0
  2. data/README.md +59 -113
  3. data/Rakefile +2 -2
  4. data/eson-dsl.gemspec +2 -1
  5. data/examples/queries/custom_boost_factor.rb +9 -0
  6. data/examples/queries/custom_filters_score.rb +15 -0
  7. data/examples/queries/custom_filters_score_with_script.rb +15 -0
  8. data/examples/queries/filtered.rb +8 -0
  9. data/examples/queries/indices.rb +12 -0
  10. data/examples/queries/match.rb +7 -0
  11. data/examples/queries/multi_match.rb +5 -0
  12. data/examples/queries/text.rb +2 -0
  13. data/lib/compat.rb +15 -0
  14. data/lib/eson-dsl.rb +35 -0
  15. data/lib/eson/modules/query_plugin.rb +4 -2
  16. data/lib/eson/search/and.rb +1 -5
  17. data/lib/eson/search/base_query.rb +5 -8
  18. data/lib/eson/search/bool.rb +2 -5
  19. data/lib/eson/search/boosting.rb +1 -4
  20. data/lib/eson/search/custom_boost_factor.rb +19 -0
  21. data/lib/eson/search/custom_filters_score.rb +42 -0
  22. data/lib/eson/search/date_histogram.rb +0 -2
  23. data/lib/eson/search/facet.rb +10 -5
  24. data/lib/eson/search/facets.rb +2 -1
  25. data/lib/eson/search/field_based.rb +1 -1
  26. data/lib/eson/search/filter.rb +1 -0
  27. data/lib/eson/search/filtered.rb +15 -0
  28. data/lib/eson/search/geo_distance.rb +2 -5
  29. data/lib/eson/search/has_child.rb +3 -5
  30. data/lib/eson/search/ids.rb +2 -4
  31. data/lib/eson/search/indices.rb +48 -0
  32. data/lib/eson/search/match.rb +10 -0
  33. data/lib/eson/search/multi_match.rb +21 -0
  34. data/lib/eson/search/prefix.rb +2 -4
  35. data/lib/eson/search/query.rb +1 -0
  36. data/lib/eson/search/term.rb +2 -4
  37. data/lib/eson/search/terms.rb +3 -5
  38. data/lib/eson/search/top_children.rb +2 -4
  39. data/test/search/facets/scoped.json +1 -1
  40. data/test/search/filters/and.rb +7 -11
  41. data/test/search/filters/bool.json +1 -1
  42. data/test/search/filters/bool.rb +17 -22
  43. data/test/search/filters/geo_bounding_box_hash.json +1 -1
  44. data/test/search/filters/geo_distance_hash.json +1 -1
  45. data/test/search/filters/geo_distance_range_hash.json +1 -1
  46. data/test/search/filters/geo_polygon_hash.json +1 -1
  47. data/test/search/filters/has_child.rb +7 -11
  48. data/test/search/filters/ids.rb +7 -11
  49. data/test/search/filters/not.rb +5 -9
  50. data/test/search/filters/or.rb +7 -11
  51. data/test/search/filters/range.rb +6 -11
  52. data/test/search/filters/type.json +1 -1
  53. data/test/search/filters/type.rb +1 -1
  54. data/test/search/filters_test.rb +2 -2
  55. data/test/search/queries/custom_boost_factor.json +10 -0
  56. data/test/search/queries/custom_filters_score.json +21 -0
  57. data/test/search/queries/filtered.rb +1 -0
  58. data/test/search/queries/filters_and_facets.json +1 -1
  59. data/test/search/queries/indices.json +13 -0
  60. data/test/search/queries/match.json +1 -0
  61. data/test/search/queries/multi_match.json +8 -0
  62. data/test/search/query_test.rb +70 -15
  63. metadata +29 -9
  64. data/TODO.md +0 -1
@@ -0,0 +1,53 @@
1
+ # Method reference
2
+
3
+ This is a full reference of all DSL methods with usage examples. The examples
4
+ can be found in `/examples`. As DSL elements can be filters or facets too, this list is alphabetic. If you want a grouped reference, please use the
5
+ elasticsearch reference.
6
+
7
+ {render:Eson::Search::And#and}
8
+ {render:Eson::Search::Bool#bool}
9
+ {render:Eson::Search::Boosting#boosting}
10
+ {render:Eson::Search::ConstantScore#constant_score}
11
+ {render:Eson::Search::CustomScore#custom_score}
12
+ {render:Eson::Search::DateHistogram#date_histogram}
13
+ {render:Eson::Search::DisMax#dis_max}
14
+ {render:Eson::Search::Exists#exists}
15
+ {render:Eson::Search::Filtered#filtered}
16
+ {render:Eson::Search::FilterFacet#filter}
17
+ {render:Eson::Search::FQuery#fquery}
18
+ {render:Eson::Search::Fuzzy#fuzzy}
19
+ {render:Eson::Search::FuzzyLikeThis#flt}
20
+ {render:Eson::Search::FuzzyLikeThisField#flt_field}
21
+ {render:Eson::Search::GeoBoundingBox#geo_bounding_box}
22
+ {render:Eson::Search::GeoDistance#geo_distance}
23
+ {render:Eson::Search::GeoDistanceRange#geo_distance_range}
24
+ {render:Eson::Search::GeoPolygon#geo_polygon}
25
+ {render:Eson::Search::HasChild#has_child}
26
+ {render:Eson::Search::Histogram#histogram}
27
+ {render:Eson::Search::Ids#ids}
28
+ {render:Eson::Search::MatchAll#match_all}
29
+ {render:Eson::Search::Missing#missing}
30
+ {render:Eson::Search::MoreLikeThis#mlt}
31
+ {render:Eson::Search::MoreLikeThis#mlt_field}
32
+ {render:Eson::Search::Nested#nested}
33
+ {render:Eson::Search::Not#not}
34
+ {render:Eson::Search::NumericRange#numeric_range}
35
+ {render:Eson::Search::Or#or}
36
+ {render:Eson::Search::Prefix#prefix}
37
+ {render:Eson::Search::QueryFacet#query}
38
+ {render:Eson::Search::QuerySting#query_string}
39
+ {render:Eson::Search::Range#range}
40
+ {render:Eson::Search::SpanFirst#span_first}
41
+ {render:Eson::Search::SpanNear#span_near}
42
+ {render:Eson::Search::SpanNot#span_not}
43
+ {render:Eson::Search::SpanOr#span_or}
44
+ {render:Eson::Search::SpanTerm#span_term}
45
+ {render:Eson::Search::Statistical#statistical}
46
+ {render:Eson::Search::SubQuery#sub_query}
47
+ {render:Eson::Search::Term#term}
48
+ {render:Eson::Search::Terms#terms}
49
+ {render:Eson::Search::TermsStats#terms_stats}
50
+ {render:Eson::Search::Text#text}
51
+ {render:Eson::Search::TopChildren#top_children}
52
+ {render:Eson::Search::Type#type}
53
+ {render:Eson::Search::Wildcard#wildcard}
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/Asquera/eson-dsl.png?branch=master)](http://travis-ci.org/Asquera/eson-dsl)
4
4
 
5
- This gem provides a full implementation of the ElasticSearch query DSL, independent of a client. At the moment, it encodes the query DSL of ElasticSearch 0.19
5
+ This gem provides a full implementation of the ElasticSearch query DSL, independent of a client. At the moment, it encodes the query DSL of ElasticSearch 0.19.
6
6
 
7
7
  Features:
8
8
 
@@ -13,31 +13,26 @@ Features:
13
13
 
14
14
  ## Usage
15
15
 
16
- ``` ruby
17
- require 'eson-dsl'
18
- require 'json'
19
-
20
- q = Eson::Search::BaseQuery.new do
21
- query do
22
- wildcard :user, :value => "kim*y", :boost => 2.0
23
- end
24
- filters do
25
- range :age, :from => 10, :to => 20
26
- end
27
- facets do
28
- histogram :hist1, :field => :age, :interval => 2
29
- end
30
- end
31
-
32
- JSON.dump(q.to_query_hash)
33
- ```
16
+ require 'eson-dsl'
17
+ require 'json'
18
+
19
+ q = Eson::Search::BaseQuery.new do
20
+ query do
21
+ wildcard :user, :value => "kim*y", :boost => 2.0
22
+ end
23
+ filters do
24
+ range :age, :from => 10, :to => 20
25
+ end
26
+ facets do
27
+ histogram :hist1, :field => :age, :interval => 2
28
+ end
29
+ end
30
+
31
+ JSON.dump(q.to_query_hash)
34
32
 
35
33
  This example yields:
36
34
 
37
- ```json
38
- {
39
- "query": {
40
- "filtered": {
35
+ {
41
36
  "query": {
42
37
  "wildcard": {
43
38
  "user": {
@@ -53,61 +48,28 @@ This example yields:
53
48
  "to": 20
54
49
  }
55
50
  }
56
- }
57
- }
58
- },
59
- "facets": {
60
- "hist1": {
61
- "histogram": {
62
- "field": "age",
63
- "interval": 2
64
- }
65
- }
66
- }
67
- }
68
- ```
69
-
70
- The query generator does its best to avoid extra work. For example, if filtering is all you want, you can omit the query part - a `filtered` query with a `match_all`-query will be generated automatically:
71
-
72
- ```ruby
73
- q.filter do
74
- term "foo", :value => 'bar'
75
- end
76
- ```
77
-
78
- will generate:
79
-
80
- ```ruby
81
- {
82
- "query": {
83
- "filtered": {
84
- "query": {
85
- "match_all": { }
86
51
  },
87
- "filter": {
88
- "term": {
89
- "foo": {
90
- "value": "bar"
52
+ "facets": {
53
+ "hist1": {
54
+ "histogram": {
55
+ "field": "age",
56
+ "interval": 2
91
57
  }
92
58
  }
93
59
  }
94
60
  }
95
- }
96
- }
97
- ```
61
+
62
+ For a list of all available generation methods along with examples, have a look at {file:eson-dsl/MethodReference.md}.
98
63
 
99
64
  ## Parameters
100
65
 
101
66
  Parameters can be passed to BaseQuery to allow generation of dynamic queries, without playing with variable visibility:
102
67
 
103
- ```ruby
104
- Eson::Search::BaseQuery.new(:search_string => "kim*y", :boost => 2.0) do
105
- query do
106
- wildcard :user, :value => param(:search_string), :boost => param(:boost)
107
- end
108
- end
109
-
110
- ```
68
+ Eson::Search::BaseQuery.new(:search_string => "kim*y", :boost => 2.0) do
69
+ query do
70
+ wildcard :user, :value => param(:search_string), :boost => param(:boost)
71
+ end
72
+ end
111
73
 
112
74
  ## Examples
113
75
 
@@ -117,63 +79,47 @@ See `examples` for all examples used in the test suite.
117
79
 
118
80
  Eson supports scoped facets in an object-oriented way. To scope a facet, call `scope` on it and pass the reference to a subquery. In practice, this requires a bit of trickery in Ruby 1.9, as it local variables are not propagated outside of the introducing block:
119
81
 
120
- ```ruby
121
- Eson::Search::BaseQuery.new do
122
- q = nil
123
- query do
124
- q = nested :path => :obj1, :score_mode => "avg" do
82
+ Eson::Search::BaseQuery.new do
83
+ q = nil
125
84
  query do
126
- match_all
85
+ q = nested :path => :obj1, :score_mode => "avg" do
86
+ query do
87
+ match_all
88
+ end
89
+ filters do
90
+ range :age, :from => 10, :to => 20
91
+ end
92
+ end
127
93
  end
128
- filters do
129
- range :age, :from => 10, :to => 20
94
+
95
+ facets do
96
+ (histogram :hist1, :field => :age, :interval => 2).scope(q, 'my_scope')
130
97
  end
131
98
  end
132
- end
133
-
134
- facets do
135
- (histogram :hist1, :field => :age, :interval => 2).scope(q, 'my_scope')
136
- end
137
- end
138
- ```
139
99
 
140
100
  ## Caveats
141
101
 
142
102
  `and`, `not` and `or` are Ruby keywords and can only be used as methods of the receiver is explicit. For that reason, you need to write the following to generate `and`- and `or`-filters:
143
103
 
144
- ```ruby
145
- q.filter do |f|
146
- f.or do
147
- term "name.first" => "Felix"
148
- term "name.first" => "Florian"
149
- end
150
- end
151
- ```
104
+ q.filter do |f|
105
+ f.or do
106
+ term "name.first" => "Felix"
107
+ term "name.first" => "Florian"
108
+ end
109
+ end
152
110
 
153
111
  and
154
112
 
155
- ```ruby
156
- q.filter do |f|
157
- f.and do #and is a keyword, so it needs a receiver
158
- range :post_date, {:from => "2010-03-01", :to => "2010-04-01"}
159
- prefix "name.second" => "ba"
160
- end
161
- end
162
- ```
113
+ q.filter do |f|
114
+ f.and do #and is a keyword, so it needs a receiver
115
+ range :post_date, {:from => "2010-03-01", :to => "2010-04-01"}
116
+ prefix "name.second" => "ba"
117
+ end
118
+ end
163
119
 
164
120
  Due to clever defaults, `and` can be omitted altogether:
165
121
 
166
- ```ruby
167
- q.filter do |f|
168
- range :post_date, {:from => "2010-03-01", :to => "2010-04-01"}
169
- prefix "name.second" => "ba"
170
- end
171
- ```
172
-
173
-
174
- # TODO
175
-
176
- * Indices-Query is missing
177
- * Custom-Boost-Query is missing)
178
- * More docs
179
- * Testing on Ruby 1.8
122
+ q.filter do |f|
123
+ range :post_date, {:from => "2010-03-01", :to => "2010-04-01"}
124
+ prefix "name.second" => "ba"
125
+ end
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'rubygems' unless defined?(Gem)
2
2
  require 'rubygems/specification'
3
- require 'rake/gempackagetask'
3
+ require 'rubygems/package_task'
4
4
  require 'rake/testtask'
5
5
 
6
6
  def gemspec
@@ -15,7 +15,7 @@ task :gemspec do
15
15
  gemspec.validate
16
16
  end
17
17
 
18
- Rake::GemPackageTask.new(gemspec) do |pkg|
18
+ Gem::PackageTask.new(gemspec) do |pkg|
19
19
  pkg.gem_spec = gemspec
20
20
  end
21
21
 
@@ -4,7 +4,8 @@ $:.push File.expand_path("../lib", __FILE__)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "eson-dsl"
7
- s.version = "0.7.0"
7
+ s.version = "0.8.0"
8
+
8
9
  s.platform = Gem::Platform::RUBY
9
10
  s.authors = ["Florian Gilcher"]
10
11
  s.email = ["florian.gilcher@asquera.de"]
@@ -0,0 +1,9 @@
1
+ Eson::Search::BaseQuery.new do
2
+ query do
3
+ custom_boost_factor :boost_factor => 5 do
4
+ query do
5
+ term "tag" => "wow"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ Eson::Search::BaseQuery.new do
2
+ query do
3
+ custom_filters_score :score_mode => :first do
4
+ query { match_all }
5
+ filters do
6
+ filter(:boost => 3) do
7
+ range :age, :from => 0 , :to => 10
8
+ end
9
+ filter(:boost => 2) do
10
+ range :age, :from => 10, :to => 20
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ Eson::Search::BaseQuery.new do
2
+ query do
3
+ custom_filters_score :score_mode => :first, :script => "3" do
4
+ query { match_all }
5
+ filters do
6
+ filter do
7
+ range :age, :from => 0 , :to => 10
8
+ end
9
+ filter do
10
+ range :age, :from => 10, :to => 20
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ Eson::Search::BaseQuery.new do
2
+ query do
3
+ filtered do
4
+ query { match_all }
5
+ filter { prefix "name.second" => "ba" }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ Eson::Search::BaseQuery.new do
2
+ query do
3
+ indices :index1, :index2 do
4
+ query do
5
+ term "tag" => "wow"
6
+ end
7
+ no_match_query do
8
+ term "tag" => "kow"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ # Since 0.19.9, #match replaces the #text query. Prefer #match over #text in
2
+ # any case.
3
+ Eson::Search::BaseQuery.new do
4
+ query do
5
+ match "message", :type => :phrase_prefix, :query => "this is a text"
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ Eson::Search::BaseQuery.new do
2
+ query do
3
+ multi_match "this is a test", :fields => ["subject", "message"]
4
+ end
5
+ end
@@ -1,3 +1,5 @@
1
+ # Since 0.19.9, #match replaces the #text query. Prefer #match over #text in
2
+ # any case.
1
3
  Eson::Search::BaseQuery.new do
2
4
  query do
3
5
  text :type => :phrase_prefix, :message => "this is a text"
@@ -0,0 +1,15 @@
1
+ # Since elasticsearch 0.19.9, `text` is called `match`
2
+ #
3
+ # For backwards compatibility reasons, `text` will be generated until
4
+ # all versions of ES below 0.19.9 are not supported anymore.
5
+ #
6
+ # `match` can be used in the DSL, though.
7
+ module Eson
8
+ module Search
9
+ module Queries
10
+ module QueryMethods
11
+ #alias :match :text
12
+ end
13
+ end
14
+ end
15
+ end
@@ -43,6 +43,7 @@ require 'eson/search/or'
43
43
  require 'eson/search/not'
44
44
  require 'eson/search/numeric_range'
45
45
  require 'eson/search/missing'
46
+ require 'eson/search/match'
46
47
  require 'eson/search/exists'
47
48
  require 'eson/search/type'
48
49
  require 'eson/search/fquery'
@@ -62,3 +63,37 @@ require 'eson/search/geo_bounding_box'
62
63
  require 'eson/search/geo_distance'
63
64
  require 'eson/search/geo_distance_range'
64
65
  require 'eson/search/geo_polygon'
66
+ require 'eson/search/multi_match'
67
+ require 'eson/search/indices'
68
+ require 'eson/search/custom_boost_factor'
69
+ require 'eson/search/custom_filters_score'
70
+ require 'eson/search/filtered'
71
+
72
+ #require 'compat'
73
+
74
+ ##
75
+ # @!macro [new] eson.filter
76
+ # @method $1
77
+ # @overload $1
78
+ # @yield the block containing further options and subfilters
79
+ # @return [self] the generated facet
80
+ # Generates a `$1` filter in a filter context.
81
+ # {include:file:eson-dsl/examples/filters/$1.rb}
82
+
83
+ ##
84
+ # @!macro [new] eson.query
85
+ # @method $1
86
+ # @overload $1
87
+ # @yield the block containing further options and subqueries
88
+ # @return [self] the generated facet
89
+ # Generates a `$1` query in a query context.
90
+ # {include:file:eson-dsl/examples/queries/$1.rb}
91
+
92
+ ##
93
+ # @!macro [new] eson.facet
94
+ # @method $1
95
+ # @overload $1
96
+ # @yield the block describing further options
97
+ # @return [self] the generated facet
98
+ # Generates a `$1` facet in a facets context.
99
+ # {include:file:eson-dsl/examples/facets/$1.rb}