forked from blevesearch/bleve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_string.y
217 lines (202 loc) · 4.04 KB
/
query_string.y
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
%{
package bleve
import "strconv"
func logDebugGrammar(format string, v ...interface{}) {
if debugParser {
logger.Printf(format, v...)
}
}
%}
%union {
s string
n int
f float64
q Query}
%token tSTRING tPHRASE tPLUS tMINUS tCOLON tBOOST tLPAREN tRPAREN tNUMBER tSTRING tGREATER tLESS
tEQUAL tTILDE tTILDENUMBER
%type <s> tSTRING
%type <s> tPHRASE
%type <s> tNUMBER
%type <s> tTILDENUMBER
%type <q> searchBase
%type <f> searchSuffix
%type <n> searchPrefix
%type <n> searchMustMustNot
%type <f> searchBoost
%%
input:
searchParts {
logDebugGrammar("INPUT")
};
searchParts:
searchPart searchParts {
logDebugGrammar("SEARCH PARTS")
}
|
searchPart {
logDebugGrammar("SEARCH PART")
};
searchPart:
searchPrefix searchBase searchSuffix {
query := $2
query.SetBoost($3)
switch($1) {
case queryShould:
yylex.(*lexerWrapper).query.AddShould(query)
case queryMust:
yylex.(*lexerWrapper).query.AddMust(query)
case queryMustNot:
yylex.(*lexerWrapper).query.AddMustNot(query)
}
};
searchPrefix:
/* empty */ {
$$ = queryShould
}
|
searchMustMustNot {
$$ = $1
}
;
searchMustMustNot:
tPLUS {
logDebugGrammar("PLUS")
$$ = queryMust
}
|
tMINUS {
logDebugGrammar("MINUS")
$$ = queryMustNot
};
searchBase:
tSTRING {
str := $1
logDebugGrammar("STRING - %s", str)
q := NewMatchQuery(str)
$$ = q
}
|
tSTRING tTILDE {
str := $1
logDebugGrammar("FUZZY STRING - %s", str)
q := NewMatchQuery(str)
q.SetFuzziness(1)
$$ = q
}
|
tSTRING tCOLON tSTRING tTILDE {
field := $1
str := $3
logDebugGrammar("FIELD - %s FUZZY STRING - %s", field, str)
q := NewMatchQuery(str)
q.SetFuzziness(1)
q.SetField(field)
$$ = q
}
|
tSTRING tTILDENUMBER {
str := $1
fuzziness, _ := strconv.ParseFloat($2, 64)
logDebugGrammar("FUZZY STRING - %s", str)
q := NewMatchQuery(str)
q.SetFuzziness(int(fuzziness))
$$ = q
}
|
tSTRING tCOLON tSTRING tTILDENUMBER {
field := $1
str := $3
fuzziness, _ := strconv.ParseFloat($4, 64)
logDebugGrammar("FIELD - %s FUZZY-%f STRING - %s", field, fuzziness, str)
q := NewMatchQuery(str)
q.SetFuzziness(int(fuzziness))
q.SetField(field)
$$ = q
}
|
tNUMBER {
str := $1
logDebugGrammar("STRING - %s", str)
q := NewMatchQuery(str)
$$ = q
}
|
tPHRASE {
phrase := $1
logDebugGrammar("PHRASE - %s", phrase)
q := NewMatchPhraseQuery(phrase)
$$ = q
}
|
tSTRING tCOLON tSTRING {
field := $1
str := $3
logDebugGrammar("FIELD - %s STRING - %s", field, str)
q := NewMatchQuery(str).SetField(field)
$$ = q
}
|
tSTRING tCOLON tNUMBER {
field := $1
str := $3
logDebugGrammar("FIELD - %s STRING - %s", field, str)
q := NewMatchQuery(str).SetField(field)
$$ = q
}
|
tSTRING tCOLON tPHRASE {
field := $1
phrase := $3
logDebugGrammar("FIELD - %s PHRASE - %s", field, phrase)
q := NewMatchPhraseQuery(phrase).SetField(field)
$$ = q
}
|
tSTRING tCOLON tGREATER tNUMBER {
field := $1
min, _ := strconv.ParseFloat($4, 64)
minInclusive := false
logDebugGrammar("FIELD - GREATER THAN %f", min)
q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil).SetField(field)
$$ = q
}
|
tSTRING tCOLON tGREATER tEQUAL tNUMBER {
field := $1
min, _ := strconv.ParseFloat($5, 64)
minInclusive := true
logDebugGrammar("FIELD - GREATER THAN OR EQUAL %f", min)
q := NewNumericRangeInclusiveQuery(&min, nil, &minInclusive, nil).SetField(field)
$$ = q
}
|
tSTRING tCOLON tLESS tNUMBER {
field := $1
max, _ := strconv.ParseFloat($4, 64)
maxInclusive := false
logDebugGrammar("FIELD - LESS THAN %f", max)
q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive).SetField(field)
$$ = q
}
|
tSTRING tCOLON tLESS tEQUAL tNUMBER {
field := $1
max, _ := strconv.ParseFloat($5, 64)
maxInclusive := true
logDebugGrammar("FIELD - LESS THAN OR EQUAL %f", max)
q := NewNumericRangeInclusiveQuery(nil, &max, nil, &maxInclusive).SetField(field)
$$ = q
};
searchBoost:
tBOOST tNUMBER {
boost, _ := strconv.ParseFloat($2, 64)
$$ = boost
logDebugGrammar("BOOST %f", boost)
};
searchSuffix:
/* empty */ {
$$ = 1.0
}
|
searchBoost {
};