eson-dsl 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MethodReference.md +53 -0
- data/README.md +59 -113
- data/Rakefile +2 -2
- data/eson-dsl.gemspec +2 -1
- data/examples/queries/custom_boost_factor.rb +9 -0
- data/examples/queries/custom_filters_score.rb +15 -0
- data/examples/queries/custom_filters_score_with_script.rb +15 -0
- data/examples/queries/filtered.rb +8 -0
- data/examples/queries/indices.rb +12 -0
- data/examples/queries/match.rb +7 -0
- data/examples/queries/multi_match.rb +5 -0
- data/examples/queries/text.rb +2 -0
- data/lib/compat.rb +15 -0
- data/lib/eson-dsl.rb +35 -0
- data/lib/eson/modules/query_plugin.rb +4 -2
- data/lib/eson/search/and.rb +1 -5
- data/lib/eson/search/base_query.rb +5 -8
- data/lib/eson/search/bool.rb +2 -5
- data/lib/eson/search/boosting.rb +1 -4
- data/lib/eson/search/custom_boost_factor.rb +19 -0
- data/lib/eson/search/custom_filters_score.rb +42 -0
- data/lib/eson/search/date_histogram.rb +0 -2
- data/lib/eson/search/facet.rb +10 -5
- data/lib/eson/search/facets.rb +2 -1
- data/lib/eson/search/field_based.rb +1 -1
- data/lib/eson/search/filter.rb +1 -0
- data/lib/eson/search/filtered.rb +15 -0
- data/lib/eson/search/geo_distance.rb +2 -5
- data/lib/eson/search/has_child.rb +3 -5
- data/lib/eson/search/ids.rb +2 -4
- data/lib/eson/search/indices.rb +48 -0
- data/lib/eson/search/match.rb +10 -0
- data/lib/eson/search/multi_match.rb +21 -0
- data/lib/eson/search/prefix.rb +2 -4
- data/lib/eson/search/query.rb +1 -0
- data/lib/eson/search/term.rb +2 -4
- data/lib/eson/search/terms.rb +3 -5
- data/lib/eson/search/top_children.rb +2 -4
- data/test/search/facets/scoped.json +1 -1
- data/test/search/filters/and.rb +7 -11
- data/test/search/filters/bool.json +1 -1
- data/test/search/filters/bool.rb +17 -22
- data/test/search/filters/geo_bounding_box_hash.json +1 -1
- data/test/search/filters/geo_distance_hash.json +1 -1
- data/test/search/filters/geo_distance_range_hash.json +1 -1
- data/test/search/filters/geo_polygon_hash.json +1 -1
- data/test/search/filters/has_child.rb +7 -11
- data/test/search/filters/ids.rb +7 -11
- data/test/search/filters/not.rb +5 -9
- data/test/search/filters/or.rb +7 -11
- data/test/search/filters/range.rb +6 -11
- data/test/search/filters/type.json +1 -1
- data/test/search/filters/type.rb +1 -1
- data/test/search/filters_test.rb +2 -2
- data/test/search/queries/custom_boost_factor.json +10 -0
- data/test/search/queries/custom_filters_score.json +21 -0
- data/test/search/queries/filtered.rb +1 -0
- data/test/search/queries/filters_and_facets.json +1 -1
- data/test/search/queries/indices.json +13 -0
- data/test/search/queries/match.json +1 -0
- data/test/search/queries/multi_match.json +8 -0
- data/test/search/query_test.rb +70 -15
- metadata +29 -9
- data/TODO.md +0 -1
data/MethodReference.md
ADDED
@@ -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
|
[](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
|
-
|
17
|
-
require '
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
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
|
-
"
|
88
|
-
"
|
89
|
-
"
|
90
|
-
"
|
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
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
-
|
121
|
-
|
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
|
-
|
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
|
-
|
129
|
-
|
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
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
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
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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 '
|
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
|
-
|
18
|
+
Gem::PackageTask.new(gemspec) do |pkg|
|
19
19
|
pkg.gem_spec = gemspec
|
20
20
|
end
|
21
21
|
|
data/eson-dsl.gemspec
CHANGED
@@ -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
|
data/examples/queries/text.rb
CHANGED
data/lib/compat.rb
ADDED
@@ -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
|
data/lib/eson-dsl.rb
CHANGED
@@ -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}
|