This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
LuceneQueryParser.jj
328 lines (328 loc) · 9.08 KB
/
LuceneQueryParser.jj
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
///*
//options {
// STATIC=false;
// JAVA_UNICODE_ESCAPE=true;
// USER_CHAR_STREAM=true;
//}
//
//PARSER_BEGIN(LuceneQueryParser)
//
//package com.rbmhtechnology.vind.parser.queryparser;
//
//import java.io.StringReader;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Locale;
//import java.util.Set;
//
////import org.apache.lucene.analysis.Analyzer;
////import org.apache.lucene.document.DateTools;
////import org.apache.lucene.search.BooleanClause;
////import org.apache.lucene.search.Query;
////import org.apache.lucene.search.TermRangeQuery;
////import org.apache.lucene.queryparser.charstream.CharStream;
////import org.apache.lucene.queryparser.charstream.FastCharStream;
//
//
//public class LuceneQueryParser {
//
// static public enum Operator { OR, AND }
// static public enum Conjuntion { CONJ_NONE, CONJ_AND, CONJ_OR}
//
// */
///** default split on whitespace behavior *//*
//
// public static final boolean DEFAULT_SPLIT_ON_WHITESPACE = false;
//
//
// */
///**
// * @see #setSplitOnWhitespace(boolean)
// *//*
//
// public boolean getSplitOnWhitespace() {
// return splitOnWhitespace;
// }
//
// */
///***//*
//
// public void setSplitOnWhitespace(boolean splitOnWhitespace) {
// this.splitOnWhitespace = splitOnWhitespace;
// }
//
// private boolean splitOnWhitespace = DEFAULT_SPLIT_ON_WHITESPACE;
//
// public Query run() throws ParseException {
// return TopLevelQuery();
// }
//}
//
//PARSER_END(LuceneQueryParser)
//
//*/
///* ***************** *//*
//
//*/
///* Token Definitions *//*
//
//*/
///* ***************** *//*
//
//
//<*> TOKEN : {
// <#_NUM_CHAR: ["0"-"9"] >
//| <#_ESCAPED_CHAR: "\\" ~[] > // every character that follows a backslash is considered as an escaped character
//| <#_TERM_START_CHAR: ( ~[ " ", "\t", "\n", "\r", "\u3000", "+", "-", "!", "(", ")", ":", "^",
// "[", "]", "\"", "{", "}", "~", "*", "?", "\\", "/" ]
// | <_ESCAPED_CHAR> ) >
//| <#_TERM_CHAR: ( <_TERM_START_CHAR> | "-" | "+" ) >
//| <#_WHITESPACE: ( " " | "\t" | "\n" | "\r" | "\u3000") >
//| <#_QUOTED_CHAR: ( ~[ "\"", "\\" ] | <_ESCAPED_CHAR> ) >
//}
//
//<DEFAULT, Range> SKIP : {
// < <_WHITESPACE>>
//}
//
//<DEFAULT> TOKEN : {
// <AND: ("AND" | "&&") >
//| <OR: ("OR" | "||") >
//| <NOT: ("NOT" | "!") >
//| <PLUS: "+" >
//| <MINUS: "-" >
//| <BAREOPER: ("+"|"-"|"!") <_WHITESPACE> >
//| <LPAREN: "(" >
//| <RPAREN: ")" >
//| <COLON: ":" >
//| <STAR: "*" >
//| <CARAT: "^" > : Boost
//| <QUOTED: "\"" (<_QUOTED_CHAR>)* "\"">
//| <TERM: <_TERM_START_CHAR> (<_TERM_CHAR>)* >
//| <FUZZY_SLOP: "~" ((<_NUM_CHAR>)+ (( "." (<_NUM_CHAR>)+ )? (<_TERM_CHAR>)*) | (<_TERM_CHAR>)*) >
//| <PREFIXTERM: ("*") | ( <_TERM_START_CHAR> (<_TERM_CHAR>)* "*" ) >
//| <WILDTERM: (<_TERM_START_CHAR> | [ "*", "?" ]) (<_TERM_CHAR> | ( [ "*", "?" ] ))* >
//| <REGEXPTERM: "/" (~[ "/" ] | "\\/" )* "/" >
//| <RANGEIN_START: "[" > : Range
//| <RANGEEX_START: "{" > : Range
//}
//
//<Boost> TOKEN : {
// <NUMBER: (<_NUM_CHAR>)+ ( "." (<_NUM_CHAR>)+ )? > : DEFAULT
//}
//
//<Range> TOKEN : {
// <RANGE_TO: "TO">
//| <RANGEIN_END: "]"> : DEFAULT
//| <RANGEEX_END: "}"> : DEFAULT
//| <RANGE_QUOTED: "\"" (~["\""] | "\\\"")+ "\"">
//| <RANGE_GOOP: (~[ " ", "]", "}" ])+ >
//}
//
//// * Query ::= ( Clause )*
//// * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
//
//int Conjunction() : {
// int ret = 0;
//}
//{
// [
// <AND> { ret = 1; }
// | <OR> { ret = 2; }
// ]
// { return ret; }
//}
//
//int Modifiers() : {
// int ret = 0;
//}
//{
// [
// <PLUS> { ret = 1; }
// | <MINUS> { ret = 2; }
// | <NOT> { ret = 3; }
// ]
// { return ret; }
//}
//
//// This makes sure that there is no garbage after the query string
//Query TopLevelQuery() : {
// Query q;
//}
//{
// q=Query() <EOF>
// { return q; }
//}
//
//Query Query() :
//{
// //List<BooleanClause> clauses = new ArrayList<BooleanClause>();
// Query q, firstQuery=null;
// int conj, mods;
//}
//{
// (
// LOOKAHEAD(2)
//
// //firstQuery= MultiTerm(field, clauses) |
// mods=Modifiers() q=Clause(field)
// {
// q.add()
// addClause(clauses, 0, mods, q);
// if (mods == 0) {
// firstQuery = q;
// }
// }
// )
// (
// LOOKAHEAD(2)
// //MultiTerm(field, clauses) |
// conj=Conjunction() mods=Modifiers() q=Clause(field)
// { addClause(clauses, conj, mods, q); }
// )*
// {
// if (clauses.size() == 1 && firstQuery != null) {
// return firstQuery;
// } else {
// return getBooleanQuery(clauses);
// }
// }
//}
//
//Query Clause() : {
// Query q;
// Token fieldToken=null, boost=null;
//}
//{
// [
// LOOKAHEAD(2)
// (
// fieldToken=<TERM> <COLON> {field=discardEscapeChar(fieldToken.image);}
// | <STAR> <COLON> {field="*";}
// )
// ]
// (
// q=Term(field)
// | <LPAREN> q=Query() <RPAREN> [ <CARAT> boost=<NUMBER> ]
// )
// { return handleBoost(q, boost); }
//}
//
//Query Term(String field) : {
// Token term, boost=null, fuzzySlop=null, goop1, goop2;
// boolean prefix = false;
// boolean wildcard = false;
// boolean fuzzy = false;
// boolean regexp = false;
// boolean startInc=false;
// boolean endInc=false;
// Query q;
// SimpleLiteral literal;
//}
//{
// (
// (
// term=<TERM>
// | term=<STAR> { wildcard=true; }
// | term=<PREFIXTERM> { prefix=true; }
// | term=<WILDTERM> { wildcard=true; }
// | term=<REGEXPTERM> { regexp=true; }
// | term=<NUMBER>
// | term=<BAREOPER> { term.image = term.image.substring(0,1); }
// )
// [
// <CARAT> boost=<NUMBER> [ fuzzySlop=<FUZZY_SLOP> { fuzzy=true; } ]
// | fuzzySlop=<FUZZY_SLOP> { fuzzy=true; } [ <CARAT> boost=<NUMBER> ]
// ]
// { //TODO: field / term query
// literal = new TermsLiteral().add(term.image);
// if(field != null) {
// q.add( new SimpleTermClause(false, fieldName, literal));
// } else {
// q.addText(literal);
// }
// //q = handleBareTokenQuery(field, term, fuzzySlop, prefix, wildcard, fuzzy, regexp);
// }
//
// | ( <RANGEIN_START> { startInc = true; } | <RANGEEX_START> )
// ( goop1=<RANGE_GOOP> | goop1=<RANGE_QUOTED> | goop1=<RANGE_TO> )
// ( <RANGE_TO> )
// ( goop2=<RANGE_GOOP> | goop2=<RANGE_QUOTED> | goop2=<RANGE_TO> )
// ( <RANGEIN_END> { endInc = true; } | <RANGEEX_END> )
// [ <CARAT> boost=<NUMBER> ]
// {
// boolean startOpen=false;
// boolean endOpen=false;
// if (goop1.kind == RANGE_QUOTED) {
// goop1.image = goop1.image.substring(1, goop1.image.length()-1);
// } else if ("*".equals(goop1.image)) {
// startOpen=true;
// }
// if (goop2.kind == RANGE_QUOTED) {
// goop2.image = goop2.image.substring(1, goop2.image.length()-1);
// } else if ("*".equals(goop2.image)) {
// endOpen=true;
// }
// //TODO: add range query
// q = getRangeQuery(field, startOpen ? null : discardEscapeChar(goop1.image), endOpen ? null : discardEscapeChar(goop2.image), startInc, endInc);
// }
//
// | term=<QUOTED>
// [
// <CARAT> boost=<NUMBER> [ fuzzySlop=<FUZZY_SLOP> { fuzzy=true; } ]
// | fuzzySlop=<FUZZY_SLOP> { fuzzy=true; } [ <CARAT> boost=<NUMBER> ]
// ]
// {
// //TODO: field / term query
// literal = new TermsLiteral().add(term.image);
// if(field != null) {
// q.add( new SimpleTermClause(false, fieldName, literal));
// } else {
// q.addText(literal);
// }
// //q = handleQuotedTerm(field, term, fuzzySlop);
// }
// )
// { return q;//handleBoost(q, boost);
// }
//}
//
//*/
///** Returns the first query if splitOnWhitespace=true or otherwise the entire produced query *//*
//
////Query MultiTerm(String field, List<BooleanClause> clauses) : {
//// Token text, whitespace, followingText;
//// Query firstQuery = null;
////}
////{
//// text=<TERM>
//// {
//// if (splitOnWhitespace) {
//// firstQuery = getFieldQuery(field, discardEscapeChar(text.image), false);
//// addClause(clauses, 0, 0, firstQuery);
//// }
//// }
//// // Both lookaheads are required; the first lookahead vets the first following term and the second lookahead vets the rest
//// LOOKAHEAD({ getToken(1).kind == TERM && allowedPostMultiTerm(getToken(2).kind) })
//// (
//// LOOKAHEAD({ getToken(1).kind == TERM && allowedPostMultiTerm(getToken(2).kind) })
//// followingText=<TERM>
//// {
//// if (splitOnWhitespace) {
//// Query q = getFieldQuery(field, discardEscapeChar(followingText.image), false);
//// addClause(clauses, 0, 0, q);
//// } else { // build up the text to send to analysis
//// text.image += " " + followingText.image;
//// }
//// }
//// )+
//// {
//// if (splitOnWhitespace == false) {
//// firstQuery = getFieldQuery(field, discardEscapeChar(text.image), false);
//// addMultiTermClauses(clauses, firstQuery);
//// }
//// return firstQuery;
//// }
////}*/