Mercurial Hosting > luan
comparison src/luan/modules/lucene/queryparser/SaneQueryParser.java @ 1110:38a42f437fd2
queryparser now uses parsers.Parser
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 02 Aug 2017 13:45:06 -0600 |
parents | 1a68fc55a80c |
children | 88b5b81cad4a |
comparison
equal
deleted
inserted
replaced
1109:8c999ab85e33 | 1110:38a42f437fd2 |
---|---|
7 import org.apache.lucene.search.MatchAllDocsQuery; | 7 import org.apache.lucene.search.MatchAllDocsQuery; |
8 import org.apache.lucene.search.BooleanClause; | 8 import org.apache.lucene.search.BooleanClause; |
9 import org.apache.lucene.search.BooleanQuery; | 9 import org.apache.lucene.search.BooleanQuery; |
10 import org.apache.lucene.search.Sort; | 10 import org.apache.lucene.search.Sort; |
11 import org.apache.lucene.search.SortField; | 11 import org.apache.lucene.search.SortField; |
12 import luan.modules.parsers.Parser; | |
13 import luan.modules.parsers.ParseException; | |
12 | 14 |
13 | 15 |
14 public class SaneQueryParser { | 16 public class SaneQueryParser { |
15 | 17 |
16 public static Query parseQuery(FieldParser fieldParser,String query) throws ParseException { | 18 public static Query parseQuery(FieldParser fieldParser,String query) throws ParseException { |
29 | 31 |
30 | 32 |
31 private static final String NOT_IN_TERM = " \t\r\n\":[]{}^+-()"; | 33 private static final String NOT_IN_TERM = " \t\r\n\":[]{}^+-()"; |
32 private static final String NOT_IN_FIELD = NOT_IN_TERM + ","; | 34 private static final String NOT_IN_FIELD = NOT_IN_TERM + ","; |
33 private final FieldParser fieldParser; | 35 private final FieldParser fieldParser; |
34 final Parser parser; | 36 private final Parser parser; |
35 | 37 |
36 private SaneQueryParser(FieldParser fieldParser,String query) { | 38 private SaneQueryParser(FieldParser fieldParser,String query) { |
37 this.fieldParser = fieldParser; | 39 this.fieldParser = fieldParser; |
38 this.parser = new Parser(query); | 40 this.parser = new Parser(query); |
41 parser.begin(); | |
42 } | |
43 | |
44 ParseException exception(String msg) { | |
45 parser.failure(); | |
46 return new ParseException(parser,msg); | |
47 } | |
48 | |
49 ParseException exception(Exception cause) { | |
50 parser.failure(); | |
51 return new ParseException(parser,cause); | |
39 } | 52 } |
40 | 53 |
41 private Query parseQuery() throws ParseException { | 54 private Query parseQuery() throws ParseException { |
42 Spaces(); | 55 Spaces(); |
43 BooleanQuery bq = new BooleanQuery(); | 56 BooleanQuery bq = new BooleanQuery(); |
89 while( parser.anyOf("0123456789.") ); | 102 while( parser.anyOf("0123456789.") ); |
90 String match = parser.textFrom(start); | 103 String match = parser.textFrom(start); |
91 float boost = Float.parseFloat(match); | 104 float boost = Float.parseFloat(match); |
92 query.setBoost(boost); | 105 query.setBoost(boost); |
93 } catch(NumberFormatException e) { | 106 } catch(NumberFormatException e) { |
94 throw new ParseException(this,e); | 107 throw exception(e); |
95 } | 108 } |
96 parser.success(); | 109 parser.success(); |
97 Spaces(); | 110 Spaces(); |
98 } | 111 } |
99 BooleanClause bc = new BooleanClause(query,occur); | 112 BooleanClause bc = new BooleanClause(query,occur); |
105 if( !parser.match('(') ) | 118 if( !parser.match('(') ) |
106 return parser.failure(null); | 119 return parser.failure(null); |
107 BooleanQuery bq = new BooleanQuery(); | 120 BooleanQuery bq = new BooleanQuery(); |
108 while( !parser.match(')') ) { | 121 while( !parser.match(')') ) { |
109 if( parser.endOfInput() ) | 122 if( parser.endOfInput() ) |
110 throw new ParseException(this,"unclosed parentheses"); | 123 throw exception("unclosed parentheses"); |
111 bq.add( Term(field) ); | 124 bq.add( Term(field) ); |
112 } | 125 } |
113 Spaces(); | 126 Spaces(); |
114 BooleanClause[] clauses = bq.getClauses(); | 127 BooleanClause[] clauses = bq.getClauses(); |
115 switch( clauses.length ) { | 128 switch( clauses.length ) { |
116 case 0: | 129 case 0: |
117 throw new ParseException(this,"empty parentheses"); | 130 throw exception("empty parentheses"); |
118 case 1: | 131 case 1: |
119 { | 132 { |
120 BooleanClause bc = clauses[0]; | 133 BooleanClause bc = clauses[0]; |
121 if( bc.getOccur() != BooleanClause.Occur.MUST_NOT ) | 134 if( bc.getOccur() != BooleanClause.Occur.MUST_NOT ) |
122 return parser.success(bc.getQuery()); | 135 return parser.success(bc.getQuery()); |
134 Spaces(); | 147 Spaces(); |
135 String minQuery = SimpleTerm(); | 148 String minQuery = SimpleTerm(); |
136 TO(); | 149 TO(); |
137 String maxQuery = SimpleTerm(); | 150 String maxQuery = SimpleTerm(); |
138 if( !parser.anyOf("]}") ) | 151 if( !parser.anyOf("]}") ) |
139 throw new ParseException(this,"unclosed range"); | 152 throw exception("unclosed range"); |
140 boolean includeMax = parser.lastChar() == ']'; | 153 boolean includeMax = parser.lastChar() == ']'; |
141 Spaces(); | 154 Spaces(); |
142 Query query = fieldParser.getRangeQuery(this,field,minQuery,maxQuery,includeMin,includeMax); | 155 Query query = fieldParser.getRangeQuery(this,field,minQuery,maxQuery,includeMin,includeMax); |
143 return parser.success(query); | 156 return parser.success(query); |
144 } | 157 } |
145 | 158 |
146 private void TO() throws ParseException { | 159 private void TO() throws ParseException { |
147 parser.begin(); | 160 parser.begin(); |
148 if( !(parser.match("TO") && Space()) ) | 161 if( !(parser.match("TO") && Space()) ) |
149 throw new ParseException(this,"'TO' expected"); | 162 throw exception("'TO' expected"); |
150 Spaces(); | 163 Spaces(); |
151 parser.success(); | 164 parser.success(); |
152 } | 165 } |
153 | 166 |
154 private String SimpleTerm() throws ParseException { | 167 private String SimpleTerm() throws ParseException { |
156 String match; | 169 String match; |
157 if( parser.match('"') ) { | 170 if( parser.match('"') ) { |
158 int start = parser.currentIndex() - 1; | 171 int start = parser.currentIndex() - 1; |
159 while( !parser.match('"') ) { | 172 while( !parser.match('"') ) { |
160 if( parser.endOfInput() ) | 173 if( parser.endOfInput() ) |
161 throw new ParseException(this,"unclosed quotes"); | 174 throw exception("unclosed quotes"); |
162 parser.anyChar(); | 175 parser.anyChar(); |
163 checkEscape(); | 176 checkEscape(); |
164 } | 177 } |
165 match = parser.textFrom(start); | 178 match = parser.textFrom(start); |
166 Spaces(); | 179 Spaces(); |
167 } else { | 180 } else { |
168 match = Unquoted(NOT_IN_TERM); | 181 match = Unquoted(NOT_IN_TERM); |
169 } | 182 } |
170 if( match.length() == 0 ) | 183 if( match.length() == 0 ) |
171 throw new ParseException(this); | 184 throw exception("invalid input"); |
172 return parser.success(match); | 185 return parser.success(match); |
173 } | 186 } |
174 | 187 |
175 private String QueryField() throws ParseException { | 188 private String QueryField() throws ParseException { |
176 parser.begin(); | 189 parser.begin(); |
223 List<SortField> list = new ArrayList<SortField>(); | 236 List<SortField> list = new ArrayList<SortField>(); |
224 list.add( SortField() ); | 237 list.add( SortField() ); |
225 while( !parser.endOfInput() ) { | 238 while( !parser.endOfInput() ) { |
226 parser.begin(); | 239 parser.begin(); |
227 if( !parser.match(',') ) | 240 if( !parser.match(',') ) |
228 throw new ParseException(this,"',' expected"); | 241 throw exception("',' expected"); |
229 Spaces(); | 242 Spaces(); |
230 parser.success(); | 243 parser.success(); |
231 list.add( SortField() ); | 244 list.add( SortField() ); |
232 } | 245 } |
233 return new Sort(list.toArray(new SortField[0])); | 246 return new Sort(list.toArray(new SortField[0])); |
235 | 248 |
236 private SortField SortField() throws ParseException { | 249 private SortField SortField() throws ParseException { |
237 parser.begin(); | 250 parser.begin(); |
238 String field = Field(); | 251 String field = Field(); |
239 if( field==null ) | 252 if( field==null ) |
240 throw new ParseException(this); | 253 throw exception("invalid input"); |
241 boolean reverse = !parser.matchIgnoreCase("asc") && parser.matchIgnoreCase("desc"); | 254 boolean reverse = !parser.matchIgnoreCase("asc") && parser.matchIgnoreCase("desc"); |
242 Spaces(); | 255 Spaces(); |
243 SortField sf = fieldParser.getSortField(this,field,reverse); | 256 SortField sf = fieldParser.getSortField(this,field,reverse); |
244 return parser.success(sf); | 257 return parser.success(sf); |
245 } | 258 } |