es-query-builder 0.0.2 → 0.0.3
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/.coverrails.yml +1 -0
- data/Gemfile +4 -0
- data/README.md +9 -20
- data/lib/es-query-builder/parser.rb +8 -14
- data/lib/es-query-builder/version.rb +1 -1
- data/spec/es_query_builder_spec.rb +34 -0
- data/spec/spec_helper.rb +3 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0edb42d41140c53d656101210a9d63faa5266a9e
|
4
|
+
data.tar.gz: 09fe5cc414ec2cc73c22b53248fc7c6b91b9d8e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 450f93cd9daf96706d47c7df13d581f9bb236912d8bbfc1e1e3f19025df1ff3e46d3e734e274b5785c7f8860e8f67b05c26b40acc96ebbe8d754b654557b40a7
|
7
|
+
data.tar.gz: 4d5d93a631b33bbdd549e5abfa424d15fa1c8b9fbdd149bad8446a956c0cc88a872539f96bfcb561251bcb99ae5e8b486501fbb65c161b143dd012f7065cacbd
|
data/.coverrails.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
# EsQueryBuilder
|
2
2
|
|
3
|
-
|
4
|
-
====
|
3
|
+
[](https://travis-ci.org/increments/es-query-builder) [](https://codeclimate.com/github/increments/es-query-builder) [](https://coveralls.io/r/increments/es-query-builder) [](https://gemnasium.com/increments/es-query-builder)
|
5
4
|
|
6
|
-
|
5
|
+
A query builder for Elasticsearch in Ruby.
|
7
6
|
|
8
|
-
|
9
|
-
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```rb
|
10
|
+
gem 'es-query-builder'
|
11
|
+
```
|
10
12
|
|
11
13
|
```ruby
|
12
14
|
builder = EsQueryBuilder.new(
|
@@ -39,23 +41,10 @@ client.search({
|
|
39
41
|
# => #<Hash>
|
40
42
|
```
|
41
43
|
|
42
|
-
|
43
|
-
===========
|
44
|
+
## Description
|
44
45
|
|
45
46
|
`EsQueryBuilder` converts a query string into a corresponding hash object for [elasticsearch-ruby](https://github.com/elasticsearch/elasticsearch-ruby).
|
46
47
|
|
47
48
|
Elasticsearch supports [query_string query](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html) dsl which is very useful to use internally, but too powerful to use as public interface. Allowing anonymous users to use the dsl may cause not only performance problems but also security risks if your index includes secret types.
|
48
49
|
|
49
50
|
This gem accepts the query_string-query-dsl-like string and converts the string into a query object using other query dsls. At the same time it sanitizes fields in the query.
|
50
|
-
|
51
|
-
INSTALLATION
|
52
|
-
============
|
53
|
-
|
54
|
-
```bash
|
55
|
-
gem install es-query-builder
|
56
|
-
```
|
57
|
-
|
58
|
-
LICENSE
|
59
|
-
=======
|
60
|
-
|
61
|
-
This software is licensed under [MIT license](https://github.com/increments/es-query-builder/tree/master/LICENSE.txt).
|
@@ -12,8 +12,8 @@ class EsQueryBuilder
|
|
12
12
|
class Parser
|
13
13
|
# Public: Construct the parser object.
|
14
14
|
#
|
15
|
-
#
|
16
|
-
#
|
15
|
+
# all_query_fields - A String or an Array of Strings for searching usual
|
16
|
+
# query terms (default: '_all').
|
17
17
|
# hierarchy_fields - An Array of Strings which treats the trailing slash
|
18
18
|
# character as a hierarchy (default: []).
|
19
19
|
#
|
@@ -236,30 +236,24 @@ class EsQueryBuilder
|
|
236
236
|
# create_bool_filters([...])
|
237
237
|
# # => [[{ term: { tag: 'foo' }}, { term: { tag: 'bar' }], [], []]
|
238
238
|
#
|
239
|
-
# # When '
|
240
|
-
# create_bool_filters([...])
|
241
|
-
# # => [[], [], [{ term: { tag: 'foo' } }]]
|
242
|
-
#
|
243
|
-
# # Suppose @hierarchy_fields contains 'tag'
|
244
|
-
#
|
245
|
-
# # When 'tag:foo/'
|
239
|
+
# # When 'tag:foo'
|
246
240
|
# create_bool_filters([...])
|
247
241
|
# # => [[], [{ term: { tag: 'foo' } }, { prefix: { tag: 'foo/' } }], []]
|
248
242
|
#
|
249
|
-
# # When '-tag:foo
|
243
|
+
# # When '-tag:foo'
|
250
244
|
# create_bool_filters([...])
|
251
|
-
# # => [[], [], [{
|
245
|
+
# # => [[], [], [{ term: { tag: 'foo' } }, { prefix: { tag: 'foo/' } }]]
|
252
246
|
#
|
253
247
|
# Returns an Array consists of must, should and must_not filters arrays.
|
254
248
|
def create_bool_filters(filter_tokens)
|
255
249
|
must, should, must_not = [], [], []
|
256
250
|
filter_tokens.each do |token|
|
257
251
|
token.term.split.each do |term|
|
258
|
-
if @hierarchy_fields.include?(token.field)
|
252
|
+
if @hierarchy_fields.include?(token.field)
|
259
253
|
cond = token.minus? ? must_not : should
|
260
|
-
cond << { prefix: { token.field => term.downcase } }
|
254
|
+
cond << { prefix: { token.field => term.downcase + '/' } }
|
261
255
|
# Exactly matches to the tag.
|
262
|
-
cond << { term: { token.field => term
|
256
|
+
cond << { term: { token.field => term.downcase } }
|
263
257
|
else
|
264
258
|
cond = token.minus? ? must_not : must
|
265
259
|
cond << { term: { token.field => term.downcase } }
|
@@ -510,6 +510,40 @@ describe EsQueryBuilder do
|
|
510
510
|
end
|
511
511
|
end
|
512
512
|
end
|
513
|
+
|
514
|
+
context 'and it is constructed with filter_fields and hierarchy_fields' do
|
515
|
+
let(:param) do
|
516
|
+
{ filter_fields: [hierarchy_field],
|
517
|
+
hierarchy_fields: [hierarchy_field] }
|
518
|
+
end
|
519
|
+
|
520
|
+
let(:hierarchy_field) do
|
521
|
+
'hierarchy_field'
|
522
|
+
end
|
523
|
+
|
524
|
+
let(:query_string) do
|
525
|
+
"#{hierarchy_field}:#{term}"
|
526
|
+
end
|
527
|
+
|
528
|
+
it 'returns both slash-ending-prefix and term filters' do
|
529
|
+
should eq(
|
530
|
+
filtered: {
|
531
|
+
query: {
|
532
|
+
match_all: {},
|
533
|
+
},
|
534
|
+
filter: {
|
535
|
+
bool: {
|
536
|
+
should: [
|
537
|
+
{ prefix: { hierarchy_field => term + '/' } },
|
538
|
+
{ term: { hierarchy_field => term } }
|
539
|
+
],
|
540
|
+
_cache: true
|
541
|
+
}
|
542
|
+
}
|
543
|
+
}
|
544
|
+
)
|
545
|
+
end
|
546
|
+
end
|
513
547
|
end
|
514
548
|
|
515
549
|
context 'when term queries are given' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: es-query-builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuku Takahashi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -59,6 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- ".coverrails.yml"
|
62
63
|
- ".gitignore"
|
63
64
|
- ".travis.yml"
|
64
65
|
- CHANGELOG.md
|