2014-08-07 01:36:23 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
|
|
|
// 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.
|
2014-08-29 20:18:36 +02:00
|
|
|
|
2014-08-07 01:36:23 +02:00
|
|
|
package bleve
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQuerySyntaxParserValid(t *testing.T) {
|
2015-05-21 21:43:13 +02:00
|
|
|
fivePointOh := 5.0
|
|
|
|
theTruth := true
|
|
|
|
theFalsehood := false
|
2014-08-07 01:36:23 +02:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
result Query
|
|
|
|
mapping *IndexMapping
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "test",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test"),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `"test phrase 1"`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchPhraseQuery("test phrase 1"),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "field:test",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test").SetField("field"),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil),
|
|
|
|
},
|
2015-05-21 21:43:13 +02:00
|
|
|
// - is allowed inside a term, just not the start
|
|
|
|
{
|
|
|
|
input: "field:t-est",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("t-est").SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
// + is allowed inside a term, just not the start
|
|
|
|
{
|
|
|
|
input: "field:t+est",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("t+est").SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
// > is allowed inside a term, just not the start
|
|
|
|
{
|
|
|
|
input: "field:t>est",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("t>est").SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
// < is allowed inside a term, just not the start
|
|
|
|
{
|
|
|
|
input: "field:t<est",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("t<est").SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
// = is allowed inside a term, just not the start
|
|
|
|
{
|
|
|
|
input: "field:t=est",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("t=est").SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
{
|
|
|
|
input: "+field1:test1",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test1").SetField("field1"),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil,
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "-field2:test2",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test2").SetField("field2"),
|
|
|
|
}),
|
2014-08-07 01:36:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `field3:"test phrase 2"`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchPhraseQuery("test phrase 2").SetField("field3"),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `+field4:"test phrase 1"`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchPhraseQuery("test phrase 1").SetField("field4"),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil,
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `-field5:"test phrase 2"`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchPhraseQuery("test phrase 2").SetField("field5"),
|
|
|
|
}),
|
2014-08-07 01:36:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `+field6:test3 -field7:test4 field8:test5`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test3").SetField("field6"),
|
|
|
|
},
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test5").SetField("field8"),
|
|
|
|
},
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test4").SetField("field7"),
|
|
|
|
}),
|
2014-08-07 01:36:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "test^3",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test").SetBoost(3.0),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "test^3 other^6",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
2014-08-29 22:27:32 +02:00
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("test").SetBoost(3.0),
|
|
|
|
NewMatchQuery("other").SetBoost(6.0),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
nil),
|
|
|
|
},
|
2014-10-11 05:36:31 +02:00
|
|
|
{
|
|
|
|
input: "33",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("33"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "field:33",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("33").SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
2014-10-11 05:45:57 +02:00
|
|
|
{
|
|
|
|
input: "cat-dog",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("cat-dog"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
2014-10-24 19:39:48 +02:00
|
|
|
{
|
|
|
|
input: "watex~",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("watex").SetFuzziness(1),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "watex~2",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("watex").SetFuzziness(2),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "watex~ 2",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("watex").SetFuzziness(1),
|
|
|
|
NewMatchQuery("2"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
2015-01-19 22:20:32 +01:00
|
|
|
{
|
|
|
|
input: "field:watex~",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("watex").SetFuzziness(1).SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "field:watex~2",
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("watex").SetFuzziness(2).SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
2015-05-21 21:43:13 +02:00
|
|
|
{
|
|
|
|
input: `field:555c3bb06f7a127cda000005`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewMatchQuery("555c3bb06f7a127cda000005").SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `field:>5`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewNumericRangeInclusiveQuery(&fivePointOh, nil, &theFalsehood, nil).SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `field:>=5`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewNumericRangeInclusiveQuery(&fivePointOh, nil, &theTruth, nil).SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `field:<5`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewNumericRangeInclusiveQuery(nil, &fivePointOh, nil, &theFalsehood).SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `field:<=5`,
|
|
|
|
mapping: NewIndexMapping(),
|
|
|
|
result: NewBooleanQuery(
|
|
|
|
nil,
|
|
|
|
[]Query{
|
|
|
|
NewNumericRangeInclusiveQuery(nil, &fivePointOh, nil, &theTruth).SetField("field"),
|
|
|
|
},
|
|
|
|
nil),
|
|
|
|
},
|
2014-08-07 01:36:23 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:43:13 +02:00
|
|
|
// turn on lexer debugging
|
|
|
|
// debugLexer = true
|
|
|
|
// logger = log.New(os.Stderr, "bleve", log.LstdFlags)
|
|
|
|
|
2014-08-07 01:36:23 +02:00
|
|
|
for _, test := range tests {
|
|
|
|
|
2014-08-30 01:31:09 +02:00
|
|
|
q, err := parseQuerySyntax(test.input, test.mapping)
|
2014-08-07 01:36:23 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(q, test.result) {
|
|
|
|
t.Errorf("Expected %#v, got %#v: for %s", test.result, q, test.input)
|
2015-05-21 21:43:13 +02:00
|
|
|
t.Errorf("Expected %#v, got %#v: for %s", test.result.(*booleanQuery).Should.(*disjunctionQuery).Disjuncts[0], q.(*booleanQuery).Should.(*disjunctionQuery).Disjuncts[0], test.input)
|
2014-08-07 01:36:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuerySyntaxParserInvalid(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
}{
|
|
|
|
{"^"},
|
|
|
|
{"^5"},
|
2015-05-21 21:43:13 +02:00
|
|
|
{"field:-text"},
|
|
|
|
{"field:+text"},
|
|
|
|
{"field:>text"},
|
|
|
|
{"field:>=text"},
|
|
|
|
{"field:<text"},
|
|
|
|
{"field:<=text"},
|
|
|
|
{"field:~text"},
|
|
|
|
{"field:^text"},
|
|
|
|
{"field::text"},
|
2014-08-07 01:36:23 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:43:13 +02:00
|
|
|
// turn on lexer debugging
|
|
|
|
// debugLexer = true
|
|
|
|
// logger = log.New(os.Stderr, "bleve", log.LstdFlags)
|
|
|
|
|
2014-08-07 01:36:23 +02:00
|
|
|
for _, test := range tests {
|
2014-08-30 01:31:09 +02:00
|
|
|
_, err := parseQuerySyntax(test.input, NewIndexMapping())
|
2014-08-07 01:36:23 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error, got nil for `%s`", test.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|