-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquery_string_parser.go
More file actions
247 lines (212 loc) · 7.05 KB
/
query_string_parser.go
File metadata and controls
247 lines (212 loc) · 7.05 KB
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
// Copyright (c) 2020 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.
// 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 rm query_string.y.go.tmp
//go:generate gofmt -s -w query_string.y.go
// note: OSX sed and gnu sed handle the -i (in-place) option differently.
// using -i.tmp works on both, at the expense of having to remove
// the unsightly .tmp files
package querystr
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/blugelabs/bluge"
)
type QueryStringOptions struct {
debugParser bool
debugLexer bool
dateFormat string
logger *log.Logger
}
var defaultLogger = log.New(os.Stderr, "", log.LstdFlags)
// DefaultOptions creates default options with debug features disabled
func DefaultOptions() QueryStringOptions {
return QueryStringOptions{
dateFormat: time.RFC3339,
logger: defaultLogger,
}
}
// WithDebugParser will make parser print debug messages to logger in options or stderr by default
func (o QueryStringOptions) WithDebugParser(debug bool) QueryStringOptions {
o.debugParser = debug
return o
}
// WithDebugLexer will make lexer print debug messages to logger in options or stderr by default
func (o QueryStringOptions) WithDebugLexer(debug bool) QueryStringOptions {
o.debugLexer = debug
return o
}
// WithDateFormat changes the default date format (RFC 3339) to the specified one
func (o QueryStringOptions) WithDateFormat(dateFormat string) QueryStringOptions {
o.dateFormat = dateFormat
return o
}
// WithLogger changes the log destination to the specified one, default is os.Stderr
func (o QueryStringOptions) WithLogger(logger *log.Logger) QueryStringOptions {
o.logger = logger
return o
}
func ParseQueryString(query string, options QueryStringOptions) (rq bluge.Query, err error) {
if query == "" {
return bluge.NewMatchNoneQuery(), nil
}
lex := newLexerWrapper(newQueryStringLex(strings.NewReader(query), options), options)
doParse(lex)
if len(lex.errs) > 0 {
return nil, fmt.Errorf(strings.Join(lex.errs, "\n"))
}
return lex.query, nil
}
func doParse(lex *lexerWrapper) {
defer func() {
r := recover()
if r != nil {
lex.errs = append(lex.errs, fmt.Sprintf("parse error: %v", r))
}
}()
yyParse(lex)
}
const (
queryShould = iota
queryMust
queryMustNot
)
type lexerWrapper struct {
lex yyLexer
errs []string
query *bluge.BooleanQuery
debugParser bool
dateFormat string
logger *log.Logger
}
func newLexerWrapper(lex yyLexer, options QueryStringOptions) *lexerWrapper {
return &lexerWrapper{
lex: lex,
query: bluge.NewBooleanQuery(),
debugParser: options.debugParser,
dateFormat: options.dateFormat,
logger: options.logger,
}
}
func (l *lexerWrapper) Lex(lval *yySymType) int {
return l.lex.Lex(lval)
}
func (l *lexerWrapper) Error(s string) {
l.errs = append(l.errs, s)
}
func (l *lexerWrapper) logDebugGrammarf(format string, v ...interface{}) {
if l.debugParser {
l.logger.Printf(format, v...)
}
}
func queryTimeFromString(yylex yyLexer, t string) (time.Time, error) {
rv, err := time.Parse(yylex.(*lexerWrapper).dateFormat, t)
if err != nil {
return time.Time{}, err
}
return rv, nil
}
func queryStringStringToken(field, str string) bluge.Query {
if strings.HasPrefix(str, "/") && strings.HasSuffix(str, "/") {
return bluge.NewRegexpQuery(str[1 : len(str)-1]).SetField(field)
} else if strings.ContainsAny(str, "*?") {
return bluge.NewWildcardQuery(str).SetField(field)
}
return bluge.NewMatchQuery(str).SetField(field)
}
func queryStringStringTokenFuzzy(field, str, fuzziness string) (*bluge.MatchQuery, error) {
fuzzy, err := strconv.ParseFloat(fuzziness, 64)
if err != nil {
return nil, fmt.Errorf("invalid fuzziness value: %v", err)
}
return bluge.NewMatchQuery(str).SetFuzziness(int(fuzzy)).SetField(field), nil
}
func queryStringNumberToken(field, str string) (bluge.Query, error) {
q1 := bluge.NewMatchQuery(str).SetField(field)
val, err := strconv.ParseFloat(str, 64)
if err != nil {
return nil, fmt.Errorf("error parsing number: %v", err)
}
q2 := bluge.NewNumericRangeInclusiveQuery(val, val, true, true).SetField(field)
return bluge.NewBooleanQuery().AddShould([]bluge.Query{q1, q2}...), nil
}
func queryStringPhraseToken(field, str string) *bluge.MatchPhraseQuery {
return bluge.NewMatchPhraseQuery(str).SetField(field)
}
func queryStringNumericRangeGreaterThanOrEqual(field, str string, orEqual bool) (*bluge.NumericRangeQuery, error) {
min, err := strconv.ParseFloat(str, 64)
if err != nil {
return nil, fmt.Errorf("error parsing number: %v", err)
}
return bluge.NewNumericRangeInclusiveQuery(min, bluge.MaxNumeric, orEqual, true).
SetField(field), nil
}
func queryStringNumericRangeLessThanOrEqual(field, str string, orEqual bool) (*bluge.NumericRangeQuery, error) {
max, err := strconv.ParseFloat(str, 64)
if err != nil {
return nil, fmt.Errorf("error parsing number: %v", err)
}
return bluge.NewNumericRangeInclusiveQuery(bluge.MinNumeric, max, true, orEqual).
SetField(field), nil
}
func queryStringDateRangeGreaterThanOrEqual(yylex yyLexer, field, phrase string, orEqual bool) (*bluge.DateRangeQuery, error) {
minTime, err := queryTimeFromString(yylex, phrase)
if err != nil {
return nil, fmt.Errorf("invalid time: %v", err)
}
return bluge.NewDateRangeInclusiveQuery(minTime, time.Time{}, orEqual, true).
SetField(field), nil
}
func queryStringDateRangeLessThanOrEqual(yylex yyLexer, field, phrase string, orEqual bool) (*bluge.DateRangeQuery, error) {
maxTime, err := queryTimeFromString(yylex, phrase)
if err != nil {
return nil, fmt.Errorf("invalid time: %v", err)
}
return bluge.NewDateRangeInclusiveQuery(time.Time{}, maxTime, true, orEqual).
SetField(field), nil
}
const noBoost = 1.0
func queryStringParseBoost(str string) (float64, error) {
boost, err := strconv.ParseFloat(str, 64)
if err != nil {
return noBoost, fmt.Errorf("invalid boost value: %v", err)
}
return boost, nil
}
func queryStringSetBoost(q bluge.Query, b float64) (bluge.Query, error) {
switch v := q.(type) {
case *bluge.MatchQuery:
return v.SetBoost(b), nil
case *bluge.RegexpQuery:
return v.SetBoost(b), nil
case *bluge.WildcardQuery:
return v.SetBoost(b), nil
case *bluge.BooleanQuery:
return v.SetBoost(b), nil
case *bluge.NumericRangeQuery:
return v.SetBoost(b), nil
case *bluge.MatchPhraseQuery:
return v.SetBoost(b), nil
case *bluge.DateRangeQuery:
return v.SetBoost(b), nil
}
return nil, fmt.Errorf("cannot boost %T", q)
}