summaryrefslogtreecommitdiff
path: root/chef/spec/unit/solr_query/query_transform_spec.rb
blob: f3fc746746cb1453c2f127b5a805b12852df47d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#
# Author:: Seth Falcon (<seth@opscode.com>)
# Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'
require 'chef/solr_query/query_transform'

describe "Chef::SolrQuery::QueryTransform" do
  before(:each) do
    @parser = Chef::SolrQuery::QueryTransform
    @parseError = Chef::Exceptions::QueryParseError
  end

  describe "single term queries" do
    basic_terms = %w(a ab 123 a1 2b foo_bar baz-baz)
    basic_terms << "  leading"
    basic_terms << "trailing "
    basic_terms += %w(XAND ANDX XOR ORX XNOT NOTX)
    basic_terms.each do |term|
      expect = "T:#{term.strip}"
      it "'#{term}' => #{expect}" do
        @parser.parse(term).should == expect
      end
    end
    describe "invalid" do
      %w(AND OR NOT :).each do |t|
        it "'#{t}' => ParseError" do
          lambda { @parser.parse(t) }.should raise_error(@parseError)
        end
      end
    end

    describe "wildcards in terms" do
      it "allows * as a wildcard" do
        @parser.parse("foo*bar").should == "T:foo*bar"
      end

      it "allows a single ? as a wildcard" do
        @parser.parse("foo?bar").should == "T:foo?bar"
      end

      it "allows multiple ? as fixed length wildcards" do
        @parser.parse("foo???bar").should == "T:foo???bar"
      end

      it "allows a leading wildcard with *" do
        # NOTE: This is not valid lucene query syntax.  However, our
        # index format and query transformation can allow it because
        # the transformed query ends up with the '*' not in leading
        # position.  We decided that allowing it makes sense because
        # queries like ec2:* are useful and many users expect this
        # behavior to work.
        @parser.parse("*foobar").should == "T:*foobar"
      end

      it "does not allow a leading wildcard with ?" do
        lambda {  @parser.parse("?foobar") }.should raise_error(@parseError)
      end

      it "does not allow a leading wildcard with ?" do
        lambda {  @parser.parse("afield:?foobar") }.should raise_error(@parseError)
      end

    end

    describe "escaped special characters in terms" do
      special_chars = ["!", "(", ")", "{", "}", "[", "]", "^", "\"",
                       "~", "*", "?", ":", "\\", "&", "|", "+", "-"]
      example_fmts = ['foo%sbar', '%sb', 'a%s']
      special_chars.each do |char|
        example_fmts.each do |fmt|
          input = fmt % ("\\" + char)
          expect = "T:#{input}"
          it "'#{input}' => #{expect}" do
            @parser.parse(input).should == expect
          end
        end
      end
    end

    describe "special characters in terms are not allowed" do
      # NOTE: '*' is not a valid start letter for a lucene search
      # term, however, we can support it because of our index
      # structure and query transformation.  We decided to keep this
      # flexibility because queries like ec2:* are common and useful.
      prefix_ok = ["!", "+", "-", "*"]
      suffix_ok = ["*", "?", "~", "-"]
      # FIXME: ideally, '!' would not be allowed in the middle of a
      # term.  Currently we parse foo!bar the same as foo !bar.
      # Also '+' might be nice to disallow
      embed_ok = ["*", "?", ":", "-", "!", "+"]
      special_chars = ["!", "(", ")", "{", "}", "[", "]", "^", "\"",
                       "~", "*", "?", ":", "\\", "&", "|", "+", "-"]
      example_fmts = {
        :prefix => '%sb',
        :middle => 'foo%sbar',
        :suffix => 'a%s'
      }
      special_chars.each do |char|
        example_fmts.keys.each do |key|
          fmt = example_fmts[key]
          if key == :prefix && prefix_ok.include?(char)
            :pass
          elsif key == :middle && embed_ok.include?(char)
            :pass
          elsif key == :suffix && suffix_ok.include?(char)
            :pass
          else
            input = fmt % char
            it "disallows: '#{input}'" do
              lambda { @parser.parse(input) }.should raise_error(@parseError)
            end
          end
        end
      end
    end

  end

  describe "multiple terms" do
    it "should allow multiple terms" do
      @parser.parse("a b cdefg").should == "T:a T:b T:cdefg"
    end
  end

  describe "boolean queries" do
    describe "two term basic and/or" do
      binary_operators = [['AND', 'AND'], ['&&', 'AND'], ['OR', 'OR'], ['||', 'OR']]
      binary_operators.each do |op, op_name|
        expect = "(OP:#{op_name} T:t1 T:t2)"
        it "should parse 't1 #{op} t2' => #{expect}" do
          @parser.parse("t1 #{op} t2").should == expect
        end
      end
    end

    it "should allow a string of terms with ands and ors" do
      expect = "(OP:AND T:t1 (OP:OR T:t2 (OP:AND T:t3 T:t4)))"
      @parser.parse("t1 AND t2 OR t3 AND t4").should == expect
    end
  end

  describe "grouping with parens" do
    it "should create a single group for (aterm)" do
      @parser.parse("(aterm)").should == "(T:aterm)"
    end

    describe "and booleans" do

      %w(AND &&).each do |op|
        expect = "((OP:AND T:a T:b))"
        input = "(a #{op} b)"
        it "parses #{input} => #{expect}" do
          @parser.parse(input).should == expect
        end
      end

      %w(OR ||).each do |op|
        expect = "((OP:OR T:a T:b))"
        input = "(a #{op} b)"
        it "parses #{input} => #{expect}" do
          @parser.parse(input).should == expect
        end
      end

      it "should handle a LHS group" do
        expect = "(OP:OR ((OP:AND T:a T:b)) T:c)"
        @parser.parse("(a && b) OR c").should == expect
        @parser.parse("(a && b) || c").should == expect
      end

      it "should handle a RHS group" do
        expect = "(OP:OR T:c ((OP:AND T:a T:b)))"
        @parser.parse("c OR (a && b)").should == expect
        @parser.parse("c OR (a AND b)").should == expect
      end

      it "should handle both sides as groups" do
        expect = "(OP:OR ((OP:AND T:c T:d)) ((OP:AND T:a T:b)))"
        @parser.parse("(c AND d) OR (a && b)").should == expect
      end
    end
  end

  describe "NOT queries" do
    # input, output
    [
     ["a NOT b", "T:a (OP:NOT T:b)"],
     ["a ! b", "T:a (OP:NOT T:b)"],
     ["a !b", "T:a (OP:NOT T:b)"],
     ["a NOT (b || c)", "T:a (OP:NOT ((OP:OR T:b T:c)))"],
     ["a ! (b || c)", "T:a (OP:NOT ((OP:OR T:b T:c)))"],
     ["a !(b || c)", "T:a (OP:NOT ((OP:OR T:b T:c)))"]
    ].each do |input, expected|
      it "should parse '#{input}' => #{expected.inspect}" do
        @parser.parse(input).should == expected
      end
    end

    ["NOT", "a NOT", "(NOT)"].each do |d|
      it "should raise a ParseError on '#{d}'" do
        lambda { @parser.parse(d) }.should raise_error(@parseError)
      end
    end
  end

  describe 'required and prohibited prefixes (+/-)' do
    ["+", "-"].each do |kind|
      [
       ["#{kind}foo", "(OP:#{kind} T:foo)"],
       ["bar #{kind}foo", "T:bar (OP:#{kind} T:foo)"],
       ["(#{kind}oneA twoA) b", "((OP:#{kind} T:oneA) T:twoA) T:b"]
      ].each do |input, expect|
        it "should parse '#{input} => #{expect.inspect}" do
          @parser.parse(input).should == expect
        end
      end
    end

    # it 'ignores + embedded in a term' do
    #   @parser.parse("one+two").should == "T:one+two"
    # end

    it 'ignores - embedded in a term' do
      @parser.parse("one-two").should == "T:one-two"
    end

    it "allows a trailing dash" do
      @parser.parse("one-").should == "T:one-"
    end

  end

  describe "phrases (strings)" do
    phrases = [['"single"', 'STR:"single"'],
               ['"two term"', 'STR:"two term"'],
               ['"has \"escaped\" quote\"s"', 'STR:"has \"escaped\" quote\"s"']
              ]
    phrases.each do |phrase, expect|
      it "'#{phrase}' => #{expect}" do
        @parser.parse(phrase).should == expect
      end
    end

    describe "invalid" do
      bad = ['""', '":not:a:term"', '"a :bad:']
      bad.each do |t|
        it "'#{t}' => ParseError" do
          lambda { @parser.parse(t) }.should raise_error(@parseError)
        end
      end
    end

    it "allows phrases to be required with '+'" do
      @parser.parse('+"a b c"').should == '(OP:+ STR:"a b c")'
    end

    it "allows phrases to be prohibited with '-'" do
      @parser.parse('-"a b c"').should == '(OP:- STR:"a b c")'
    end

    it "allows phrases to be excluded with NOT" do
      @parser.parse('a NOT "b c"').should == 'T:a (OP:NOT STR:"b c")'
    end

  end

  describe "fields" do
    it "parses a term annotated with a field" do
      @parser.parse("afield:aterm").should == "(F:afield T:aterm)"
    end

    it "allows underscore in a field name" do
      @parser.parse("a_field:aterm").should == "(F:a_field T:aterm)"
    end

    it "parses a group annotated with a field" do
      @parser.parse("afield:(a b c)").should == "(F:afield (T:a T:b T:c))"
    end

    it "parses a phrase annotated with a field" do
      @parser.parse('afield:"a b c"').should == '(F:afield STR:"a b c")'
    end

    it "allows @ in a term" do
      @parser.parse('afield:foo@acme.org').should == '(F:afield T:foo@acme.org)'
    end

    describe "and binary operators" do
      examples = [
                  ['term1 AND afield:term2', "(OP:AND T:term1 (F:afield T:term2))"],
                  ['afield:term1 AND term2', "(OP:AND (F:afield T:term1) T:term2)"],
                  ['afield:term1 AND bfield:term2',
                   "(OP:AND (F:afield T:term1) (F:bfield T:term2))"]]
      examples.each do |input, want|
        it "'#{input}' => '#{want}'" do
          @parser.parse(input).should == want
        end
      end
    end

    describe "and unary operators" do
      examples = [
                  ['term1 AND NOT afield:term2',
                   "(OP:AND T:term1 (OP:NOT (F:afield T:term2)))"],
                  ['term1 AND ! afield:term2',
                   "(OP:AND T:term1 (OP:NOT (F:afield T:term2)))"],
                  ['term1 AND !afield:term2',
                   "(OP:AND T:term1 (OP:NOT (F:afield T:term2)))"],
                  ['term1 AND -afield:term2',
                   "(OP:AND T:term1 (OP:- (F:afield T:term2)))"],
                  ['-afield:[* TO *]',
                   "(OP:- (FR:afield [*] [*]))"]
                 ]
      examples.each do |input, want|
        it "#{input} => #{want}" do
          @parser.parse(input).should == want
        end
      end
    end
  end

  describe "range queries" do
    before(:each) do
      @kinds = {
        "inclusive" => {:left => "[", :right => "]"},
        "exclusive" => {:left => "{", :right => "}"}
      }
    end

    def make_expect(kind, field, s, e)
      expect_fmt = "(FR:%s %s%s%s %s%s%s)"
      left = @kinds[kind][:left]
      right = @kinds[kind][:right]
      expect_fmt % [field, left, s, right, left, e, right]
    end

    def make_query(kind, field, s, e)
      query_fmt = "%s:%s%s TO %s%s"
      left = @kinds[kind][:left]
      right = @kinds[kind][:right]
      query_fmt % [field, left, s, e, right]
    end

    ["inclusive", "exclusive"].each do |kind|
      tests = [["afield", "start", "end"],
               ["afield", "start", "*"],
               ["afield", "*", "end"],
               ["afield", "*", "*"]
              ]
      tests.each do |field, s, e|
        it "parses an #{kind} range query #{s} TO #{e}" do
          expect = make_expect(kind, field, s, e)
          query = make_query(kind, field, s, e)
          @parser.parse(query).should == expect
        end
      end
    end

    describe "and binary operators" do
      [["afield:[start TO end] AND term",
        "(OP:AND (FR:afield [start] [end]) T:term)"],
       ["term OR afield:[start TO end]",
        "(OP:OR T:term (FR:afield [start] [end]))"],
       ["f1:[s1 TO e1] OR f2:[s2 TO e2]",
        "(OP:OR (FR:f1 [s1] [e1]) (FR:f2 [s2] [e2]))"]
      ].each do |q, want|
        it "parses '#{q}'" do
          @parser.parse(q).should == want
        end
      end
    end

    describe "and unary operators" do
      [["t1 NOT afield:[start TO end]",
        "T:t1 (OP:NOT (FR:afield [start] [end]))"]
      ].each do |input, want|
        it "#{input} => #{want}" do
          @parser.parse(input).should == want
        end
      end
    end
  end

  describe "proximity query" do
    [
     ['"one two"~10', '(OP:~ STR:"one two" 10)'],
     ['word~', '(OP:~ T:word)'],
     ['word~0.5', '(OP:~ T:word 0.5)']
    ].each do |input, expect|
      it "'#{input}' => #{expect}" do
        @parser.parse(input).should == expect
      end
    end
  end

  describe "term boosting" do
    [
     ['"one two"^10', '(OP:^ STR:"one two" 10)'],
     ['word^0.5', '(OP:^ T:word 0.5)']
    ].each do |input, expect|
      it "'#{input}' => #{expect}" do
        @parser.parse(input).should == expect
      end
    end

    it "should fail to parse if no boosting argument is given" do
      lambda { @parser.parse("foo^")}.should raise_error(@parseError)
    end
  end

  describe "examples" do
    examples = [['tags:apples*.for.eating.com', "(F:tags T:apples*.for.eating.com)"],
                ['ohai_time:[1234.567 TO *]', "(FR:ohai_time [1234.567] [*])"],
                ['ohai_time:[* TO 1234.567]', "(FR:ohai_time [*] [1234.567])"],
                ['ohai_time:[* TO *]', "(FR:ohai_time [*] [*])"]]
                # ['aterm AND afield:aterm', "((OP:AND T:aterm ((F:afield T:aterm))))"],
                # ['role:prod AND aterm', "blah"],
                # ['role:prod AND xy:true', "blah"]]
    examples.each do |input, want|
      it "'#{input}' => '#{want}'" do
        @parser.parse(input).should == want
      end
    end
  end

  describe "transform queries for solr schema" do
    testcase_file = "#{CHEF_SPEC_DATA}/search_queries_to_transform.txt"
    lines = File.readlines(testcase_file).map { |line| line.strip }
    lines = lines.select { |line| !line.empty? }
    testcases = Hash[*(lines)]
    testcases.keys.sort.each do |input|
      expected = testcases[input]
      it "from> #{input}\n    to> #{expected}\n" do
        @parser.transform(input).should == expected
      end
    end
  end

end