elasticsearch-query-builder 0.1.6 → 0.1.7
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.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile.lock +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +257 -10
- data/elasticsearch-query-builder.gemspec +7 -7
- data/lib/elastic_search/query_builder.rb +1 -1
- data/lib/elastic_search/query_builder/version.rb +1 -1
- metadata +13 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02eaa67325bd4f1ae203bc2fbc5761fe2debf49b1a68684437a3e5a42f2b267d
|
4
|
+
data.tar.gz: 1ea45b1264b8279cf07cc0d82154e3b0588a461b87fa50e25e2d2d2d1a0736a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d8ebd9affc58191305a3156596770a6324f95d2220b8cbbadaecc92ab5a06dbeca2529d7a336020cc2700ce88ad887a25ca104d089768180a489b2d4594e1f9
|
7
|
+
data.tar.gz: '02087d0d55fffbd233e60fe10795a8764bb66b1200c346bea56659eadf065e29e5d83c17b19b3282820107d13d09b5d28ff1e4178f9d1a7150dd7feb131ca0e3'
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at
|
58
|
+
reported by contacting the project team at support@goprebo.com. All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
data/Gemfile.lock
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
# Elasticsearch::QueryBuilder
|
1
|
+
# Ruby Elasticsearch::QueryBuilder
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Ruby gem for building complex ElasticSearch queries using clauses as methods. Supports query and function_score builders, as well as clients to fetch results.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,17 +20,262 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
Instantiate the class
|
26
24
|
|
27
|
-
|
25
|
+
```ruby
|
26
|
+
elastic_query = ElasticSearch::QueryBuilder.new(opts: {}, client: nil, function_score: false)
|
27
|
+
```
|
28
|
+
|
29
|
+
### Initialize parameters
|
30
|
+
|
31
|
+
| Parameter | Type | Default | Description |
|
32
|
+
|----------------|---------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
|
33
|
+
| opts | Hash | {} | Optional. Initial query. Each method will add a clause to the @opts object. |
|
34
|
+
| client | Object | nil | Optional. Client to fetch results from ElasticSearch. elasticsearch-model clients is an useful gem for this. |
|
35
|
+
| function_score | Boolean | false | Optional. Whether to include clauses inside a function_score path and therefore be able to use .functions() methods to calculate custom document scores. |
|
36
|
+
|
37
|
+
### Behaviour
|
38
|
+
|
39
|
+
#### Methods definition
|
40
|
+
|
41
|
+
Available methods and paths are:
|
42
|
+
|
43
|
+
- must: [ query bool must ],
|
44
|
+
- must_not: [ query bool must_not ],
|
45
|
+
- should: [ query bool should ],
|
46
|
+
- functions: [ functions ],
|
47
|
+
- ids: [ query terms _id ],
|
48
|
+
- size: [ size ],
|
49
|
+
- fields: [ _source ],
|
50
|
+
- range: [ bool must range ],
|
51
|
+
- sort: [ sort ],
|
52
|
+
- aggs: [ aggs ]
|
53
|
+
|
54
|
+
Once the class is loaded, each method is defined and path **query function_score** is appended if class was initialized with **function_score: true** and original path starts with **query**.
|
55
|
+
|
56
|
+
#### init_path
|
57
|
+
|
58
|
+
Each path is initialized if not added previously to the query. If already added, it's appended to existing path preserving all previous clauses.
|
59
|
+
|
60
|
+
#### exclude_opposite
|
61
|
+
|
62
|
+
**must** and **must_not** are exclusive paths. The QueryBuilder do its best to recognize if an opposite clauses was previously added and remove it preserving only the last exclusive clause.
|
63
|
+
|
64
|
+
#### add_clause
|
65
|
+
|
66
|
+
Once the path is built and the opposite is excluded, the clause is merged with all the other clauses.
|
67
|
+
|
68
|
+
### Methods
|
69
|
+
|
70
|
+
#### .must([clauses])
|
71
|
+
|
72
|
+
Receives an array of clauses and insert them in **query: { bool: { must: [] } }** path. If clause was previously added with .must_not() it is replaced.
|
73
|
+
|
74
|
+
**Example**
|
75
|
+
```ruby
|
76
|
+
elastic_query.must([ { range: { sign_in_count: { gte: 3 } } } ])
|
77
|
+
|
78
|
+
# elastic_query.send(:opts)
|
79
|
+
{
|
80
|
+
query: {
|
81
|
+
bool: {
|
82
|
+
must: [
|
83
|
+
range: {
|
84
|
+
sign_in_count: { gte: 3 }
|
85
|
+
}
|
86
|
+
]
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
```
|
91
|
+
|
92
|
+
#### .must_not([clauses])
|
93
|
+
|
94
|
+
Receives an array of clauses and insert them in **query: { bool: { must_not: [] } }** path. If clause was previously added with .must() it is replaced.
|
95
|
+
|
96
|
+
**Example**
|
97
|
+
```ruby
|
98
|
+
elastic_query.must_not([ { range: { sign_in_count: { gte: 3 } } } ])
|
99
|
+
|
100
|
+
# elastic_query.send(:opts)
|
101
|
+
{
|
102
|
+
query: {
|
103
|
+
bool: {
|
104
|
+
must_not: [
|
105
|
+
range: {
|
106
|
+
sign_in_count: { gte: 3 }
|
107
|
+
}
|
108
|
+
]
|
109
|
+
}
|
110
|
+
}
|
111
|
+
}
|
112
|
+
```
|
113
|
+
|
114
|
+
#### .should([clauses])
|
115
|
+
|
116
|
+
Receives an array of clauses and insert them in **query: { bool: { should: [] } }** path.
|
117
|
+
|
118
|
+
**Example**
|
119
|
+
```ruby
|
120
|
+
elastic_query.should([ { range: { sign_in_count: { gte: 3 } } } ])
|
121
|
+
|
122
|
+
# elastic_query.send(:opts)
|
123
|
+
{
|
124
|
+
query: {
|
125
|
+
bool: {
|
126
|
+
should: [
|
127
|
+
range: {
|
128
|
+
sign_in_count: { gte: 3 }
|
129
|
+
}
|
130
|
+
]
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
```
|
135
|
+
|
136
|
+
#### .functions([functions])
|
137
|
+
|
138
|
+
Receives an array of functions to calculate document score. .functions() is overridden if .sort() method is called. Functions are inserted in **query: { function_score: { functions: [] } }** path.
|
139
|
+
|
140
|
+
**Example**
|
141
|
+
```ruby
|
142
|
+
elastic_query.functions([ { script_score: { script: "1 - ( 1.0 / ( doc['popularity'].value == 0 ? 1 : doc['popularity'].value ))" }, weight: 1 } ])
|
143
|
+
|
144
|
+
# elastic_query.send(:opts)
|
145
|
+
{
|
146
|
+
query: {
|
147
|
+
function_score: {
|
148
|
+
functions: [
|
149
|
+
{ script_score: { script: "1 - ( 1.0 / ( doc['popularity'].value == 0 ? 1 : doc['popularity'].value ))" }, weight: 1 }
|
150
|
+
]
|
151
|
+
}
|
152
|
+
}
|
153
|
+
}
|
154
|
+
```
|
155
|
+
|
156
|
+
#### .ids([array of ids])
|
157
|
+
|
158
|
+
Receives an array of ids to retrieve. Ids are inserted in **query: { terms: { _id: [] } }** path.
|
28
159
|
|
29
|
-
|
160
|
+
**Example**
|
161
|
+
```ruby
|
162
|
+
elastic_query.ids([1, 4, 6])
|
163
|
+
|
164
|
+
# elastic_query.send(:opts)
|
165
|
+
{
|
166
|
+
query: {
|
167
|
+
terms: {
|
168
|
+
_id: [
|
169
|
+
1,
|
170
|
+
4
|
171
|
+
6
|
172
|
+
]
|
173
|
+
}
|
174
|
+
}
|
175
|
+
}
|
176
|
+
```
|
30
177
|
|
31
|
-
|
178
|
+
#### .size(Integer)
|
179
|
+
|
180
|
+
Receives an integer representing the number of documents to retrieve. Size is a root attribute in **size: size** path. Each time .size() method is called, size attribute is overridden.
|
181
|
+
|
182
|
+
.size(0) returns all metadata but documents.
|
183
|
+
|
184
|
+
**Example**
|
185
|
+
```ruby
|
186
|
+
elastic_query.size(200)
|
187
|
+
|
188
|
+
# elastic_query.send(:opts)
|
189
|
+
{
|
190
|
+
query: {
|
191
|
+
},
|
192
|
+
size: 200
|
193
|
+
}
|
194
|
+
```
|
195
|
+
|
196
|
+
#### .fields([fields])
|
197
|
+
|
198
|
+
Receives an array of fields to retrieve for each document. Each field is appended to **_source** path.
|
199
|
+
|
200
|
+
**Example**
|
201
|
+
```ruby
|
202
|
+
elastic_query.fields([:name, :category, :created_at])
|
203
|
+
|
204
|
+
# elastic_query.send(:opts)
|
205
|
+
{
|
206
|
+
query: {
|
207
|
+
},
|
208
|
+
_source: [:name, :category, :created_at]
|
209
|
+
}
|
210
|
+
```
|
211
|
+
|
212
|
+
#### .range([body])
|
213
|
+
|
214
|
+
Subtype of .must() method. Receives an array of clauses representing ranges of fields. Each clause is appended to **query bool must range** path.
|
215
|
+
|
216
|
+
**Example**
|
217
|
+
```ruby
|
218
|
+
elastic_query.range([{ sign_in_count: { gte: 3 } }])
|
219
|
+
|
220
|
+
# elastic_query.send(:opts)
|
221
|
+
{
|
222
|
+
query: {
|
223
|
+
bool: {
|
224
|
+
must: [
|
225
|
+
range: [
|
226
|
+
{
|
227
|
+
sign_in_count: { gte: 3 }
|
228
|
+
}
|
229
|
+
]
|
230
|
+
]
|
231
|
+
}
|
232
|
+
}
|
233
|
+
}
|
234
|
+
```
|
235
|
+
|
236
|
+
#### .sort(field)
|
237
|
+
|
238
|
+
Receives a field to sort query results by. Each time .sort() method is called, sort attribute is overridden. It also disables .functions() score.
|
239
|
+
|
240
|
+
**Example**
|
241
|
+
```ruby
|
242
|
+
elastic_query.sort([popularity: { order: :desc }])
|
243
|
+
|
244
|
+
# elastic_query.send(:opts)
|
245
|
+
{
|
246
|
+
query: {
|
247
|
+
},
|
248
|
+
sort: [
|
249
|
+
{ popularity: { order: :desc } }
|
250
|
+
]
|
251
|
+
}
|
252
|
+
```
|
253
|
+
|
254
|
+
#### .aggs([aggs fields])
|
255
|
+
|
256
|
+
Receives an array of fields to aggregate results count by. If body is not needed, .size(0) will still return aggregated count.
|
257
|
+
|
258
|
+
**Example**
|
259
|
+
```ruby
|
260
|
+
elastic_query.sort([aggs: { ages: { terms: { field: 'median_age' } } }])
|
261
|
+
|
262
|
+
# elastic_query.send(:opts)
|
263
|
+
{
|
264
|
+
query: {
|
265
|
+
},
|
266
|
+
aggs: [
|
267
|
+
{ ages: { terms: { field: 'median_age' } } }
|
268
|
+
]
|
269
|
+
}
|
270
|
+
```
|
271
|
+
|
272
|
+
## Development
|
273
|
+
|
274
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
32
275
|
|
33
276
|
## Contributing
|
34
277
|
|
35
|
-
Bug reports and pull requests are welcome on
|
278
|
+
Bug reports and pull requests are welcome on Gituhub at https://github.com/goprebo/elasticsearch-query-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
279
|
|
37
280
|
## License
|
38
281
|
|
@@ -40,4 +283,8 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
40
283
|
|
41
284
|
## Code of Conduct
|
42
285
|
|
43
|
-
Everyone interacting in the Elasticsearch::QueryBuilder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
286
|
+
Everyone interacting in the Elasticsearch::QueryBuilder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/goprebo/elasticsearch-query-builder/blob/master/CODE_OF_CONDUCT.md).
|
287
|
+
|
288
|
+
## Contact
|
289
|
+
|
290
|
+
You may contact Prebo at support@goprebo.com or in https://www.goprebo.com/
|
@@ -7,14 +7,14 @@ require 'elastic_search/query_builder/version'
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = 'elasticsearch-query-builder'
|
9
9
|
spec.version = ElasticSearch::QueryBuilder::VERSION
|
10
|
-
spec.authors = ['Kickser']
|
11
|
-
spec.email = ['support@goprebo.com']
|
12
|
-
spec.summary = 'ElasticSearch
|
10
|
+
spec.authors = ['Kickser', 'Federico Farina', 'Alejo Zárate', 'Nicolás Vázquez']
|
11
|
+
spec.email = ['support@goprebo.com', 'federico@goprebo.com', 'ale@goprebo.com', 'nico@goprebo.com']
|
12
|
+
spec.summary = 'Ruby gem to build complex ElasticSearch queries with clauses as methods'
|
13
13
|
spec.license = 'MIT'
|
14
|
-
spec.homepage = 'https://
|
15
|
-
spec.metadata['homepage_uri'] = 'https://
|
16
|
-
spec.metadata['source_code_uri'] = 'https://
|
17
|
-
spec.metadata['changelog_uri'] = 'https://
|
14
|
+
spec.homepage = 'https://github.com/goprebo/elasticsearch-query-builder'
|
15
|
+
spec.metadata['homepage_uri'] = 'https://github.com/goprebo/elasticsearch-query-builder'
|
16
|
+
spec.metadata['source_code_uri'] = 'https://github.com/goprebo/elasticsearch-query-builder'
|
17
|
+
spec.metadata['changelog_uri'] = 'https://github.com/goprebo/elasticsearch-query-builder'
|
18
18
|
|
19
19
|
# Specify which files should be added to the gem when it is released.
|
20
20
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticsearch-query-builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kickser
|
8
|
+
- Federico Farina
|
9
|
+
- Alejo Zárate
|
10
|
+
- Nicolás Vázquez
|
8
11
|
autorequire:
|
9
12
|
bindir: exe
|
10
13
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
14
|
+
date: 2019-11-12 00:00:00.000000000 Z
|
12
15
|
dependencies:
|
13
16
|
- !ruby/object:Gem::Dependency
|
14
17
|
name: bundler
|
@@ -55,6 +58,9 @@ dependencies:
|
|
55
58
|
description:
|
56
59
|
email:
|
57
60
|
- support@goprebo.com
|
61
|
+
- federico@goprebo.com
|
62
|
+
- ale@goprebo.com
|
63
|
+
- nico@goprebo.com
|
58
64
|
executables: []
|
59
65
|
extensions: []
|
60
66
|
extra_rdoc_files: []
|
@@ -75,13 +81,13 @@ files:
|
|
75
81
|
- lib/elastic_search/query_builder.rb
|
76
82
|
- lib/elastic_search/query_builder/version.rb
|
77
83
|
- lib/elasticsearch-query-builder.rb
|
78
|
-
homepage: https://
|
84
|
+
homepage: https://github.com/goprebo/elasticsearch-query-builder
|
79
85
|
licenses:
|
80
86
|
- MIT
|
81
87
|
metadata:
|
82
|
-
homepage_uri: https://
|
83
|
-
source_code_uri: https://
|
84
|
-
changelog_uri: https://
|
88
|
+
homepage_uri: https://github.com/goprebo/elasticsearch-query-builder
|
89
|
+
source_code_uri: https://github.com/goprebo/elasticsearch-query-builder
|
90
|
+
changelog_uri: https://github.com/goprebo/elasticsearch-query-builder
|
85
91
|
post_install_message:
|
86
92
|
rdoc_options: []
|
87
93
|
require_paths:
|
@@ -101,5 +107,5 @@ rubyforge_project:
|
|
101
107
|
rubygems_version: 2.7.3
|
102
108
|
signing_key:
|
103
109
|
specification_version: 4
|
104
|
-
summary: ElasticSearch
|
110
|
+
summary: Ruby gem to build complex ElasticSearch queries with clauses as methods
|
105
111
|
test_files: []
|