0
0
Fork 0

fix query string query syntax when term starts with a number

fixes #207
This commit is contained in:
Marty Schoch 2015-05-21 15:43:13 -04:00
parent 165e63ac09
commit 2af47cea75
4 changed files with 565 additions and 408 deletions

View File

@ -932,6 +932,31 @@ func TestKeywordSearchBug207(t *testing.T) {
t.Errorf("expecated id 'b', got '%s'", sres.Hits[0].ID)
}
// now do the same searches using query strings
sreq = NewSearchRequest(NewQueryStringQuery("Body:a555c3bb06f7a127cda000005"))
sres, err = index.Search(sreq)
if err != nil {
t.Fatal(err)
}
if sres.Total != 1 {
t.Errorf("expected 1 result, got %d", sres.Total)
}
if sres.Hits[0].ID != "a" {
t.Errorf("expecated id 'a', got '%s'", sres.Hits[0].ID)
}
sreq = NewSearchRequest(NewQueryStringQuery(`Body:555c3bb06f7a127cda000005`))
sres, err = index.Search(sreq)
if err != nil {
t.Fatal(err)
}
if sres.Total != 1 {
t.Errorf("expected 1 result, got %d", sres.Total)
}
if sres.Hits[0].ID != "b" {
t.Errorf("expecated id 'b', got '%s'", sres.Hits[0].ID)
}
err = index.Close()
if err != nil {
t.Fatal(err)

View File

@ -26,7 +26,7 @@
return tNUMBER
}
/[ \t\n]+/ { logDebugTokens("WHITESPACE (count=%d)", len(yylex.Text())) /* eat up whitespace */ }
/[^\t\n\f\r :^\+\-><=~][^\t\n\f\r :^~]*/ {
/[^\t\n\f\r :^\+><=~-][^\t\n\f\r :^~]*/ {
lval.s = yylex.Text()
logDebugTokens("STRING - %s", lval.s);
return tSTRING

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,9 @@ import (
)
func TestQuerySyntaxParserValid(t *testing.T) {
fivePointOh := 5.0
theTruth := true
theFalsehood := false
tests := []struct {
input string
result Query
@ -50,6 +53,61 @@ func TestQuerySyntaxParserValid(t *testing.T) {
},
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),
},
// = 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),
},
{
input: "+field1:test1",
mapping: NewIndexMapping(),
@ -216,8 +274,62 @@ func TestQuerySyntaxParserValid(t *testing.T) {
},
nil),
},
{
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),
},
}
// turn on lexer debugging
// debugLexer = true
// logger = log.New(os.Stderr, "bleve", log.LstdFlags)
for _, test := range tests {
q, err := parseQuerySyntax(test.input, test.mapping)
@ -226,6 +338,7 @@ func TestQuerySyntaxParserValid(t *testing.T) {
}
if !reflect.DeepEqual(q, test.result) {
t.Errorf("Expected %#v, got %#v: for %s", test.result, q, test.input)
t.Errorf("Expected %#v, got %#v: for %s", test.result.(*booleanQuery).Should.(*disjunctionQuery).Disjuncts[0], q.(*booleanQuery).Should.(*disjunctionQuery).Disjuncts[0], test.input)
}
}
}
@ -236,8 +349,21 @@ func TestQuerySyntaxParserInvalid(t *testing.T) {
}{
{"^"},
{"^5"},
{"field:-text"},
{"field:+text"},
{"field:>text"},
{"field:>=text"},
{"field:<text"},
{"field:<=text"},
{"field:~text"},
{"field:^text"},
{"field::text"},
}
// turn on lexer debugging
// debugLexer = true
// logger = log.New(os.Stderr, "bleve", log.LstdFlags)
for _, test := range tests {
_, err := parseQuerySyntax(test.input, NewIndexMapping())
if err == nil {