0
0

Merge pull request #544 from mschoch/work_on_query_string_parser

add ability to perform exact numeric matches
This commit is contained in:
Marty Schoch 2017-02-24 16:36:31 -05:00 committed by GitHub
commit a7ebb84805
4 changed files with 131 additions and 34 deletions

View File

@ -127,7 +127,15 @@ tSTRING tCOLON tSTRING tTILDE {
tNUMBER { tNUMBER {
str := $1 str := $1
logDebugGrammar("STRING - %s", str) logDebugGrammar("STRING - %s", str)
q := NewMatchQuery(str) q1 := NewMatchQuery(str)
val, err := strconv.ParseFloat($1, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
inclusive := true
q2 := NewNumericRangeInclusiveQuery(&val, &val, &inclusive, &inclusive)
q := NewDisjunctionQuery([]Query{q1,q2})
q.queryStringMode = true
$$ = q $$ = q
} }
| |
@ -158,8 +166,17 @@ tSTRING tCOLON tNUMBER {
field := $1 field := $1
str := $3 str := $3
logDebugGrammar("FIELD - %s STRING - %s", field, str) logDebugGrammar("FIELD - %s STRING - %s", field, str)
q := NewMatchQuery(str) q1 := NewMatchQuery(str)
q.SetField(field) q1.SetField(field)
val, err := strconv.ParseFloat($3, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
inclusive := true
q2 := NewNumericRangeInclusiveQuery(&val, &val, &inclusive, &inclusive)
q2.SetField(field)
q := NewDisjunctionQuery([]Query{q1,q2})
q.queryStringMode = true
$$ = q $$ = q
} }
| |
@ -174,7 +191,10 @@ tSTRING tCOLON tPHRASE {
| |
tSTRING tCOLON tGREATER tNUMBER { tSTRING tCOLON tGREATER tNUMBER {
field := $1 field := $1
min, _ := strconv.ParseFloat($4, 64) min, err := strconv.ParseFloat($4, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
minInclusive := false minInclusive := false
logDebugGrammar("FIELD - GREATER THAN %f", min) logDebugGrammar("FIELD - GREATER THAN %f", min)
q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil) q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil)
@ -184,7 +204,10 @@ tSTRING tCOLON tGREATER tNUMBER {
| |
tSTRING tCOLON tGREATER tEQUAL tNUMBER { tSTRING tCOLON tGREATER tEQUAL tNUMBER {
field := $1 field := $1
min, _ := strconv.ParseFloat($5, 64) min, err := strconv.ParseFloat($5, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
minInclusive := true minInclusive := true
logDebugGrammar("FIELD - GREATER THAN OR EQUAL %f", min) logDebugGrammar("FIELD - GREATER THAN OR EQUAL %f", min)
q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil) q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil)
@ -194,7 +217,10 @@ tSTRING tCOLON tGREATER tEQUAL tNUMBER {
| |
tSTRING tCOLON tLESS tNUMBER { tSTRING tCOLON tLESS tNUMBER {
field := $1 field := $1
max, _ := strconv.ParseFloat($4, 64) max, err := strconv.ParseFloat($4, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
maxInclusive := false maxInclusive := false
logDebugGrammar("FIELD - LESS THAN %f", max) logDebugGrammar("FIELD - LESS THAN %f", max)
q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive) q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive)
@ -204,7 +230,10 @@ tSTRING tCOLON tLESS tNUMBER {
| |
tSTRING tCOLON tLESS tEQUAL tNUMBER { tSTRING tCOLON tLESS tEQUAL tNUMBER {
field := $1 field := $1
max, _ := strconv.ParseFloat($5, 64) max, err := strconv.ParseFloat($5, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
maxInclusive := true maxInclusive := true
logDebugGrammar("FIELD - LESS THAN OR EQUAL %f", max) logDebugGrammar("FIELD - LESS THAN OR EQUAL %f", max)
q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive) q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive)

View File

@ -581,12 +581,20 @@ yydefault:
{ {
str := yyDollar[1].s str := yyDollar[1].s
logDebugGrammar("STRING - %s", str) logDebugGrammar("STRING - %s", str)
q := NewMatchQuery(str) q1 := NewMatchQuery(str)
val, err := strconv.ParseFloat(yyDollar[1].s, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
inclusive := true
q2 := NewNumericRangeInclusiveQuery(&val, &val, &inclusive, &inclusive)
q := NewDisjunctionQuery([]Query{q1, q2})
q.queryStringMode = true
yyVAL.q = q yyVAL.q = q
} }
case 12: case 12:
yyDollar = yyS[yypt-1 : yypt+1] yyDollar = yyS[yypt-1 : yypt+1]
//line query_string.y:134 //line query_string.y:142
{ {
phrase := yyDollar[1].s phrase := yyDollar[1].s
logDebugGrammar("PHRASE - %s", phrase) logDebugGrammar("PHRASE - %s", phrase)
@ -595,7 +603,7 @@ yydefault:
} }
case 13: case 13:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line query_string.y:141 //line query_string.y:149
{ {
field := yyDollar[1].s field := yyDollar[1].s
str := yyDollar[3].s str := yyDollar[3].s
@ -613,18 +621,27 @@ yydefault:
} }
case 14: case 14:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line query_string.y:157 //line query_string.y:165
{ {
field := yyDollar[1].s field := yyDollar[1].s
str := yyDollar[3].s str := yyDollar[3].s
logDebugGrammar("FIELD - %s STRING - %s", field, str) logDebugGrammar("FIELD - %s STRING - %s", field, str)
q := NewMatchQuery(str) q1 := NewMatchQuery(str)
q.SetField(field) q1.SetField(field)
val, err := strconv.ParseFloat(yyDollar[3].s, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
inclusive := true
q2 := NewNumericRangeInclusiveQuery(&val, &val, &inclusive, &inclusive)
q2.SetField(field)
q := NewDisjunctionQuery([]Query{q1, q2})
q.queryStringMode = true
yyVAL.q = q yyVAL.q = q
} }
case 15: case 15:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line query_string.y:166 //line query_string.y:183
{ {
field := yyDollar[1].s field := yyDollar[1].s
phrase := yyDollar[3].s phrase := yyDollar[3].s
@ -635,10 +652,13 @@ yydefault:
} }
case 16: case 16:
yyDollar = yyS[yypt-4 : yypt+1] yyDollar = yyS[yypt-4 : yypt+1]
//line query_string.y:175 //line query_string.y:192
{ {
field := yyDollar[1].s field := yyDollar[1].s
min, _ := strconv.ParseFloat(yyDollar[4].s, 64) min, err := strconv.ParseFloat(yyDollar[4].s, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
minInclusive := false minInclusive := false
logDebugGrammar("FIELD - GREATER THAN %f", min) logDebugGrammar("FIELD - GREATER THAN %f", min)
q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil) q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil)
@ -647,10 +667,13 @@ yydefault:
} }
case 17: case 17:
yyDollar = yyS[yypt-5 : yypt+1] yyDollar = yyS[yypt-5 : yypt+1]
//line query_string.y:185 //line query_string.y:205
{ {
field := yyDollar[1].s field := yyDollar[1].s
min, _ := strconv.ParseFloat(yyDollar[5].s, 64) min, err := strconv.ParseFloat(yyDollar[5].s, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
minInclusive := true minInclusive := true
logDebugGrammar("FIELD - GREATER THAN OR EQUAL %f", min) logDebugGrammar("FIELD - GREATER THAN OR EQUAL %f", min)
q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil) q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil)
@ -659,10 +682,13 @@ yydefault:
} }
case 18: case 18:
yyDollar = yyS[yypt-4 : yypt+1] yyDollar = yyS[yypt-4 : yypt+1]
//line query_string.y:195 //line query_string.y:218
{ {
field := yyDollar[1].s field := yyDollar[1].s
max, _ := strconv.ParseFloat(yyDollar[4].s, 64) max, err := strconv.ParseFloat(yyDollar[4].s, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
maxInclusive := false maxInclusive := false
logDebugGrammar("FIELD - LESS THAN %f", max) logDebugGrammar("FIELD - LESS THAN %f", max)
q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive) q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive)
@ -671,10 +697,13 @@ yydefault:
} }
case 19: case 19:
yyDollar = yyS[yypt-5 : yypt+1] yyDollar = yyS[yypt-5 : yypt+1]
//line query_string.y:205 //line query_string.y:231
{ {
field := yyDollar[1].s field := yyDollar[1].s
max, _ := strconv.ParseFloat(yyDollar[5].s, 64) max, err := strconv.ParseFloat(yyDollar[5].s, 64)
if err != nil {
yylex.(*lexerWrapper).lex.Error(fmt.Sprintf("error parsing number: %v", err))
}
maxInclusive := true maxInclusive := true
logDebugGrammar("FIELD - LESS THAN OR EQUAL %f", max) logDebugGrammar("FIELD - LESS THAN OR EQUAL %f", max)
q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive) q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive)
@ -683,7 +712,7 @@ yydefault:
} }
case 20: case 20:
yyDollar = yyS[yypt-4 : yypt+1] yyDollar = yyS[yypt-4 : yypt+1]
//line query_string.y:215 //line query_string.y:244
{ {
field := yyDollar[1].s field := yyDollar[1].s
minInclusive := false minInclusive := false
@ -700,7 +729,7 @@ yydefault:
} }
case 21: case 21:
yyDollar = yyS[yypt-5 : yypt+1] yyDollar = yyS[yypt-5 : yypt+1]
//line query_string.y:230 //line query_string.y:259
{ {
field := yyDollar[1].s field := yyDollar[1].s
minInclusive := true minInclusive := true
@ -717,7 +746,7 @@ yydefault:
} }
case 22: case 22:
yyDollar = yyS[yypt-4 : yypt+1] yyDollar = yyS[yypt-4 : yypt+1]
//line query_string.y:245 //line query_string.y:274
{ {
field := yyDollar[1].s field := yyDollar[1].s
maxInclusive := false maxInclusive := false
@ -734,7 +763,7 @@ yydefault:
} }
case 23: case 23:
yyDollar = yyS[yypt-5 : yypt+1] yyDollar = yyS[yypt-5 : yypt+1]
//line query_string.y:260 //line query_string.y:289
{ {
field := yyDollar[1].s field := yyDollar[1].s
maxInclusive := true maxInclusive := true
@ -751,13 +780,13 @@ yydefault:
} }
case 24: case 24:
yyDollar = yyS[yypt-0 : yypt+1] yyDollar = yyS[yypt-0 : yypt+1]
//line query_string.y:276 //line query_string.y:305
{ {
yyVAL.pf = nil yyVAL.pf = nil
} }
case 25: case 25:
yyDollar = yyS[yypt-1 : yypt+1] yyDollar = yyS[yypt-1 : yypt+1]
//line query_string.y:280 //line query_string.y:309
{ {
yyVAL.pf = nil yyVAL.pf = nil
boost, err := strconv.ParseFloat(yyDollar[1].s, 64) boost, err := strconv.ParseFloat(yyDollar[1].s, 64)

View File

@ -12,7 +12,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:generate go tool yacc -o query_string.y.go query_string.y // as of Go 1.8 this requires the goyacc external tool
// available from golang.org/x/tools/cmd/goyacc
//go:generate goyacc -o query_string.y.go query_string.y
//go:generate sed -i.tmp -e 1d query_string.y.go //go:generate sed -i.tmp -e 1d query_string.y.go
//go:generate rm query_string.y.go.tmp //go:generate rm query_string.y.go.tmp

View File

@ -24,6 +24,8 @@ import (
) )
func TestQuerySyntaxParserValid(t *testing.T) { func TestQuerySyntaxParserValid(t *testing.T) {
thirtyThreePointOh := 33.0
twoPointOh := 2.0
fivePointOh := 5.0 fivePointOh := 5.0
theTruth := true theTruth := true
theFalsehood := false theFalsehood := false
@ -280,7 +282,15 @@ func TestQuerySyntaxParserValid(t *testing.T) {
result: NewBooleanQueryForQueryString( result: NewBooleanQueryForQueryString(
nil, nil,
[]Query{ []Query{
NewMatchQuery("33"), func() Query {
qo := NewDisjunctionQuery(
[]Query{
NewMatchQuery("33"),
NewNumericRangeInclusiveQuery(&thirtyThreePointOh, &thirtyThreePointOh, &theTruth, &theTruth),
})
qo.queryStringMode = true
return qo
}(),
}, },
nil), nil),
}, },
@ -291,9 +301,21 @@ func TestQuerySyntaxParserValid(t *testing.T) {
nil, nil,
[]Query{ []Query{
func() Query { func() Query {
q := NewMatchQuery("33") qo := NewDisjunctionQuery(
q.SetField("field") []Query{
return q func() Query {
q := NewMatchQuery("33")
q.SetField("field")
return q
}(),
func() Query {
q := NewNumericRangeInclusiveQuery(&thirtyThreePointOh, &thirtyThreePointOh, &theTruth, &theTruth)
q.SetField("field")
return q
}(),
})
qo.queryStringMode = true
return qo
}(), }(),
}, },
nil), nil),
@ -347,7 +369,15 @@ func TestQuerySyntaxParserValid(t *testing.T) {
q.SetFuzziness(1) q.SetFuzziness(1)
return q return q
}(), }(),
NewMatchQuery("2"), func() Query {
qo := NewDisjunctionQuery(
[]Query{
NewMatchQuery("2"),
NewNumericRangeInclusiveQuery(&twoPointOh, &twoPointOh, &theTruth, &theTruth),
})
qo.queryStringMode = true
return qo
}(),
}, },
nil), nil),
}, },
@ -726,6 +756,12 @@ func TestQuerySyntaxParserInvalid(t *testing.T) {
{`cat^3\0`}, {`cat^3\0`},
{`cat~3\:`}, {`cat~3\:`},
{`cat~3\0`}, {`cat~3\0`},
{`99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999`},
{`field:99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999`},
{`field:>99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999`},
{`field:>=99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999`},
{`field:<99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999`},
{`field:<=99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999`},
} }
// turn on lexer debugging // turn on lexer debugging