waistband 0.2.2 → 0.2.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.
@@ -16,6 +16,7 @@ module Waistband
16
16
  @ranges = []
17
17
  @sorts = []
18
18
  @terms = {}
19
+ @exclude_terms = {}
19
20
  @optional_terms = {}
20
21
  @page = (options[:page] || 1).to_i
21
22
  @page_size = (options[:page_size] || 20).to_i
@@ -73,6 +74,14 @@ module Waistband
73
74
  end
74
75
  alias :add_term :add_terms
75
76
 
77
+ def add_exclude_terms(key, words)
78
+ @exclude_terms[key] ||= {
79
+ keywords: []
80
+ }
81
+ @exclude_terms[key][:keywords] += prep_words_uniquely(words)
82
+ end
83
+ alias :add_exclude_term :add_exclude_terms
84
+
76
85
  def add_optional_terms(key, words)
77
86
  @optional_terms[key] ||= {
78
87
  keywords: []
@@ -103,7 +112,7 @@ module Waistband
103
112
  query: {
104
113
  bool: {
105
114
  must: must_to_hash,
106
- must_not: [],
115
+ must_not: must_not_to_hash,
107
116
  should: should_to_hash
108
117
  }
109
118
  },
@@ -160,35 +169,23 @@ module Waistband
160
169
  }
161
170
  } if @match.present?
162
171
 
163
- terms.each do |term|
172
+ prep_term_hash(@terms).each do |term|
164
173
  must << term
165
174
  end
166
175
 
167
176
  must
168
177
  end
169
178
 
170
- def should_to_hash
171
- should = []
172
-
173
- optional_terms.each do |term|
174
- should << term
175
- end
176
-
177
- should
179
+ def must_not_to_hash
180
+ prep_term_hash(@exclude_terms).map { |term| term }
178
181
  end
179
182
 
180
- def terms
181
- @terms.map do |key, term|
182
- {
183
- terms: {
184
- key.to_sym => term[:keywords]
185
- }
186
- }
187
- end
183
+ def should_to_hash
184
+ prep_term_hash(@optional_terms).map { |term| term }
188
185
  end
189
186
 
190
- def optional_terms
191
- @optional_terms.map do |key, term|
187
+ def prep_term_hash(terms)
188
+ terms.map do |key, term|
192
189
  {
193
190
  terms: {
194
191
  key.to_sym => term[:keywords]
@@ -1,3 +1,3 @@
1
1
  module Waistband
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -50,6 +50,26 @@ describe Waistband::Query do
50
50
  hit['_source']['description'].should eql 'pick me up some eggs'
51
51
  end
52
52
 
53
+ it "excludes results" do
54
+ query = index.query('shopping ikea')
55
+ query.add_field('name')
56
+ query.add_exclude_term('internal', 'true')
57
+
58
+ json = query.send(:execute!)
59
+
60
+ json['hits']['hits'].size.should eql 1
61
+
62
+ hit = json['hits']['hits'].first
63
+
64
+ hit['_id'].should match(/^task_.*/)
65
+ hit['_source'].should be_a Hash
66
+ hit['_source']['id'].should eql 123123
67
+ hit['_source']['name'].should eql 'some shopping in ikea'
68
+ hit['_source']['user_id'].should eql 999
69
+ hit['_source']['description'].should eql 'i need you to pick up some stuff in ikea'
70
+ hit['_source']['internal'].should be_false
71
+ end
72
+
53
73
  end
54
74
 
55
75
  describe '#add_sort' do
@@ -101,6 +121,20 @@ describe Waistband::Query do
101
121
 
102
122
  end
103
123
 
124
+ describe '#add_exclude_term' do
125
+
126
+ it "adds the term on the key" do
127
+ query.add_exclude_term('metro', 'boston')
128
+ query.instance_variable_get('@exclude_terms')['metro'][:keywords].should eql ['boston']
129
+ end
130
+
131
+ it "adds several terms on multiple words" do
132
+ query.add_exclude_term('metro', 'sf bay area')
133
+ query.instance_variable_get('@exclude_terms')['metro'][:keywords].should eql ['sf', 'bay', 'area']
134
+ end
135
+
136
+ end
137
+
104
138
  describe '#add_optional_term' do
105
139
 
106
140
  it "adds the term on the key" do
@@ -115,60 +149,34 @@ describe Waistband::Query do
115
149
 
116
150
  end
117
151
 
118
- describe '#terms' do
152
+ describe '#must_to_hash' do
119
153
 
120
- it "builds an array of all terms" do
154
+ it "creates an array of the must of the query" do
121
155
  query.add_term('metro', 'sf bay area')
122
- query.send(:terms).should eql([
156
+ query.add_field('name')
157
+ query.send(:must_to_hash).should eql([
123
158
  {
124
- terms: {
125
- metro: ['sf', 'bay', 'area']
159
+ multi_match: {
160
+ query: "shopping ikea",
161
+ fields: ['name']
126
162
  }
127
- }
128
- ])
129
- end
130
-
131
- it "builds an array of single terms" do
132
- query.add_term('metro', 'boston')
133
- query.send(:terms).should eql([
163
+ },
134
164
  {
135
165
  terms: {
136
- metro: ['boston']
166
+ metro: ["sf", "bay", "area"]
137
167
  }
138
168
  }
139
169
  ])
140
170
  end
141
171
 
142
- it "constructs correctly with multiple terms" do
143
- query.add_term('metro', 'sf bay area')
144
- query.add_term('geography', 'San Francisco')
145
- query.send(:terms).should eql([
146
- {
147
- terms: {
148
- metro: ["sf", "bay", "area"]}
149
- },
150
- {
151
- terms: {
152
- geography: ["San", "Francisco"]
153
- }
154
- }
155
- ])
156
- end
157
-
158
172
  end
159
173
 
160
- describe '#must_to_hash' do
174
+ describe '#must_not_to_hash' do
161
175
 
162
176
  it "creates an array of the must of the query" do
163
- query.add_term('metro', 'sf bay area')
164
- query.add_field('name')
165
- query.send(:must_to_hash).should eql([
166
- {
167
- multi_match: {
168
- query: "shopping ikea",
169
- fields: ['name']
170
- }
171
- },
177
+ query.add_exclude_term('metro', 'sf bay area')
178
+
179
+ query.send(:must_not_to_hash).should eql([
172
180
  {
173
181
  terms: {
174
182
  metro: ["sf", "bay", "area"]
@@ -248,6 +256,7 @@ describe Waistband::Query do
248
256
  query.add_term('metro', 'sf bay area')
249
257
  query.add_term('geography', 'San Francisco')
250
258
  query.add_optional_term('internal', 'true')
259
+ query.add_exclude_term('user_id', '999')
251
260
  query.add_field('name')
252
261
  query.add_field('description')
253
262
 
@@ -272,7 +281,13 @@ describe Waistband::Query do
272
281
  }
273
282
  }
274
283
  ],
275
- must_not: [],
284
+ must_not: [
285
+ {
286
+ terms: {
287
+ user_id: ['999']
288
+ }
289
+ }
290
+ ],
276
291
  should: [
277
292
  {
278
293
  terms: {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waistband
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: