es-query-builder 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0edb42d41140c53d656101210a9d63faa5266a9e
4
- data.tar.gz: 09fe5cc414ec2cc73c22b53248fc7c6b91b9d8e4
3
+ metadata.gz: f83ec59a43ec2595976f103e18b8a2135a931b41
4
+ data.tar.gz: bf950eec7d0a7b3a984948b12f31fc6b06074b1e
5
5
  SHA512:
6
- metadata.gz: 450f93cd9daf96706d47c7df13d581f9bb236912d8bbfc1e1e3f19025df1ff3e46d3e734e274b5785c7f8860e8f67b05c26b40acc96ebbe8d754b654557b40a7
7
- data.tar.gz: 4d5d93a631b33bbdd549e5abfa424d15fa1c8b9fbdd149bad8446a956c0cc88a872539f96bfcb561251bcb99ae5e8b486501fbb65c161b143dd012f7065cacbd
6
+ metadata.gz: 929a33a7427b0392f686bae8aca50bf2a23877b6982c30775047d109336a271c02355985f35bfe037787cff618815eead53f2955e69645d2ee43c54cca0a8e77
7
+ data.tar.gz: 68b1aa1017822dfa80bb1c60a6829d68393c348efefd9ed6d5c1f625f0ac3ab0ffe9e3e72f7c3c56d221678d4060b243e75dcc87cc3aa1064e8b86c542f0c84f
@@ -136,14 +136,13 @@ class EsQueryBuilder
136
136
  # Returns a Hash represents a filter hash.
137
137
  def build_filter_hash(filter_tokens)
138
138
  return {} if filter_tokens.empty?
139
- must, should, must_not = create_bool_filters(filter_tokens)
140
- if must.size == 1 && should.empty? && must_not.empty?
139
+ must, must_not = create_bool_filters(filter_tokens)
140
+ if must.size == 1 && must_not.empty?
141
141
  # Term filter is cached by default.
142
142
  must.first
143
143
  else
144
144
  bool = {}
145
145
  bool[:must] = must if must.size > 0
146
- bool[:should] = should if should.size > 0
147
146
  bool[:must_not] = must_not if must_not.size > 0
148
147
  # Bool filter is not cached by default.
149
148
  { bool: bool.merge(_cache: true) }
@@ -232,13 +231,9 @@ class EsQueryBuilder
232
231
  #
233
232
  # Examples
234
233
  #
235
- # # When 'tag:"foo bar"'
236
- # create_bool_filters([...])
237
- # # => [[{ term: { tag: 'foo' }}, { term: { tag: 'bar' }], [], []]
238
- #
239
234
  # # When 'tag:foo'
240
235
  # create_bool_filters([...])
241
- # # => [[], [{ term: { tag: 'foo' } }, { prefix: { tag: 'foo/' } }], []]
236
+ # # => [[ { bool: { should: [{ term: { tag: 'foo' } }, { prefix: { tag: 'foo/' } }] } }], []]
242
237
  #
243
238
  # # When '-tag:foo'
244
239
  # create_bool_filters([...])
@@ -246,21 +241,31 @@ class EsQueryBuilder
246
241
  #
247
242
  # Returns an Array consists of must, should and must_not filters arrays.
248
243
  def create_bool_filters(filter_tokens)
249
- must, should, must_not = [], [], []
244
+ must, must_not = [], []
250
245
  filter_tokens.each do |token|
251
246
  token.term.split.each do |term|
252
247
  if @hierarchy_fields.include?(token.field)
253
- cond = token.minus? ? must_not : should
254
- cond << { prefix: { token.field => term.downcase + '/' } }
255
- # Exactly matches to the tag.
256
- cond << { term: { token.field => term.downcase } }
248
+ if token.minus?
249
+ must_not << { prefix: { token.field => term.downcase + '/' } }
250
+ must_not << { term: { token.field => term.downcase } }
251
+ else
252
+ must << {
253
+ bool: {
254
+ should: [
255
+ { prefix: { token.field => term.downcase + '/' } },
256
+ # Exactly matches to the tag.
257
+ { term: { token.field => term.downcase } },
258
+ ]
259
+ }
260
+ }
261
+ end
257
262
  else
258
263
  cond = token.minus? ? must_not : must
259
264
  cond << { term: { token.field => term.downcase } }
260
265
  end
261
266
  end
262
267
  end
263
- [must, should, must_not]
268
+ [must, must_not]
264
269
  end
265
270
  end
266
271
  end
@@ -1,3 +1,3 @@
1
1
  class EsQueryBuilder
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -536,13 +536,80 @@ describe EsQueryBuilder do
536
536
  should: [
537
537
  { prefix: { hierarchy_field => term + '/' } },
538
538
  { term: { hierarchy_field => term } }
539
- ],
540
- _cache: true
539
+ ]
541
540
  }
542
541
  }
543
542
  }
544
543
  )
545
544
  end
545
+
546
+ context 'and the query starts with a minus char' do
547
+ let(:query_string) do
548
+ "-#{hierarchy_field}:#{term}"
549
+ end
550
+
551
+ it 'returns must_not filter' do
552
+ should eq(
553
+ filtered: {
554
+ query: {
555
+ match_all: {},
556
+ },
557
+ filter: {
558
+ bool: {
559
+ must_not: [
560
+ { prefix: { hierarchy_field => term + '/' } },
561
+ { term: { hierarchy_field => term } }
562
+ ],
563
+ _cache: true
564
+ }
565
+ }
566
+ }
567
+ )
568
+ end
569
+ end
570
+
571
+ context 'and there are multiple filtered query terms' do
572
+ let(:query_string) do
573
+ "#{hierarchy_field}:#{term} #{hierarchy_field}:#{term2}"
574
+ end
575
+
576
+ let(:term2) do
577
+ 'world'
578
+ end
579
+
580
+ it 'returns both slash-ending-prefix and term filters' do
581
+ should eq(
582
+ filtered: {
583
+ query: {
584
+ match_all: {},
585
+ },
586
+ filter: {
587
+ bool: {
588
+ must: [
589
+ {
590
+ bool: {
591
+ should: [
592
+ { prefix: { hierarchy_field => term + '/' } },
593
+ { term: { hierarchy_field => term } }
594
+ ]
595
+ }
596
+ },
597
+ {
598
+ bool: {
599
+ should: [
600
+ { prefix: { hierarchy_field => term2 + '/' } },
601
+ { term: { hierarchy_field => term2 } }
602
+ ]
603
+ }
604
+ },
605
+ ],
606
+ _cache: true
607
+ }
608
+ }
609
+ }
610
+ )
611
+ end
612
+ end
546
613
  end
547
614
  end
548
615
 
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.3
4
+ version: 0.0.4
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-02-19 00:00:00.000000000 Z
11
+ date: 2015-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler