es-query-builder 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b7061462ef887c110a7362d082ecab6170d8800
4
- data.tar.gz: 273dc7a2f14404b05e8bf8b573fb1f4af62650be
3
+ metadata.gz: 0edb42d41140c53d656101210a9d63faa5266a9e
4
+ data.tar.gz: 09fe5cc414ec2cc73c22b53248fc7c6b91b9d8e4
5
5
  SHA512:
6
- metadata.gz: 19b39d4e940467281be485a915fa4333f808d903c0dac03d69206570a603f0b592b02e0950357b6cefb90f4229a62bc63ab884adae1394961e615aad981350a5
7
- data.tar.gz: 36d828651c879de1cdfb76b59bbdc4de8772372c72845acd7fcfb8929a44ecbb3d1c6f4b824553d038cc75f26c432f96bda09bb2e589f2d3594a65eaf115f994
6
+ metadata.gz: 450f93cd9daf96706d47c7df13d581f9bb236912d8bbfc1e1e3f19025df1ff3e46d3e734e274b5785c7f8860e8f67b05c26b40acc96ebbe8d754b654557b40a7
7
+ data.tar.gz: 4d5d93a631b33bbdd549e5abfa424d15fa1c8b9fbdd149bad8446a956c0cc88a872539f96bfcb561251bcb99ae5e8b486501fbb65c161b143dd012f7065cacbd
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in es-query-builder.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ end
data/README.md CHANGED
@@ -1,12 +1,14 @@
1
- [![Build Status](https://travis-ci.org/increments/es-query-builder.svg?branch=master)](https://travis-ci.org/increments/es-query-builder)
1
+ # EsQueryBuilder
2
2
 
3
- NAME
4
- ====
3
+ [![Build Status](https://travis-ci.org/increments/es-query-builder.svg?branch=master)](https://travis-ci.org/increments/es-query-builder) [![Code Climate](https://codeclimate.com/github/increments/es-query-builder/badges/gpa.svg)](https://codeclimate.com/github/increments/es-query-builder) [![Coverage Status](https://coveralls.io/repos/increments/es-query-builder/badge.svg)](https://coveralls.io/r/increments/es-query-builder) [![Dependency Status](https://gemnasium.com/increments/es-query-builder.svg)](https://gemnasium.com/increments/es-query-builder)
5
4
 
6
- `EsQueryBuilder` - A query builder for Elasticsearch in Ruby.
5
+ A query builder for Elasticsearch in Ruby.
7
6
 
8
- SYNOPSIS
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
- DESCRIPTION
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
- # query_fields - An Array of Strings for specifing allowed quering
16
- # types (default: []).
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 '-tag:foo'
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
- # # => [[], [], [{ prefix: { tag: 'foo/' } }, { term: { tag: 'foo' } }]]
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) && term.end_with?('/')
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[0...-1].downcase } }
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 } }
@@ -1,3 +1,3 @@
1
1
  class EsQueryBuilder
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -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
@@ -1,3 +1,6 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
1
4
  require 'es-query-builder'
2
5
 
3
6
  RSpec.configure do |config|
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.2
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-01-03 00:00:00.000000000 Z
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