Mercurial Hosting > luan
annotate src/goodjava/lucene/queryparser/GoodQueryParser.java @ 1565:44e444a8da64
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 11 Nov 2020 13:17:58 -0700 |
parents | b1195cfe8712 |
children | 45e42f403fb3 |
rev | line source |
---|---|
1458 | 1 package goodjava.lucene.queryparser; |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 import java.util.List; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 import java.util.ArrayList; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import java.util.regex.Pattern; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import org.apache.lucene.search.Query; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 import org.apache.lucene.search.MatchAllDocsQuery; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 import org.apache.lucene.search.BooleanClause; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 import org.apache.lucene.search.BooleanQuery; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 import org.apache.lucene.search.Sort; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 import org.apache.lucene.search.SortField; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1362
diff
changeset
|
12 import goodjava.parser.Parser; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1362
diff
changeset
|
13 import goodjava.parser.ParseException; |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 |
1459 | 16 public class GoodQueryParser { |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 public static Query parseQuery(FieldParser fieldParser,String query) throws ParseException { |
1459 | 19 return new GoodQueryParser(fieldParser,query).parseQuery(); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 |
1422 | 22 public static String quote(String s) { |
1458 | 23 s = s.replace("\\","\\\\"); |
24 s = s.replace("\b","\\b"); | |
25 s = s.replace("\f","\\f"); | |
26 s = s.replace("\n","\\n"); | |
27 s = s.replace("\r","\\r"); | |
28 s = s.replace("\t","\\t"); | |
29 s = s.replace("\"","\\\""); | |
1422 | 30 return "\""+s+"\""; |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 public static Sort parseSort(FieldParser fieldParser,String sort) throws ParseException { |
1459 | 34 return new GoodQueryParser(fieldParser,sort).parseSort(); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 |
1362
77f2d091f17f
minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
38 private static final String NOT_IN_RANGE = " \t\r\n\":[]{}^+()"; |
77f2d091f17f
minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
39 private static final String NOT_IN_TERM = NOT_IN_RANGE + "-"; |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 private static final String NOT_IN_FIELD = NOT_IN_TERM + ","; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 private final FieldParser fieldParser; |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
42 private final Parser parser; |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 |
1459 | 44 private GoodQueryParser(FieldParser fieldParser,String query) { |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 this.fieldParser = fieldParser; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 this.parser = new Parser(query); |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
47 parser.begin(); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
48 } |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
49 |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
50 ParseException exception(String msg) { |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
51 parser.failure(); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
52 return new ParseException(parser,msg); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
53 } |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
54 |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
55 ParseException exception(Exception cause) { |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
56 parser.failure(); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
57 return new ParseException(parser,cause); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 private Query parseQuery() throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 BooleanQuery bq = new BooleanQuery(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 while( !parser.endOfInput() ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
64 bq.add( Term(null) ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
65 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 BooleanClause[] clauses = bq.getClauses(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 switch( clauses.length ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
68 case 0: |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
69 return new MatchAllDocsQuery(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
70 case 1: |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
72 BooleanClause bc = clauses[0]; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 if( bc.getOccur() != BooleanClause.Occur.MUST_NOT ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 return bc.getQuery(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
75 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
76 default: |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
77 return bq; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
79 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
80 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 private BooleanClause Term(String defaultField) throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 BooleanClause.Occur occur; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 if( parser.match('+') ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 occur = BooleanClause.Occur.MUST; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
85 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
86 } else if( parser.match('-') ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
87 occur = BooleanClause.Occur.MUST_NOT; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
88 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
89 } else { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
90 occur = BooleanClause.Occur.SHOULD; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
91 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
92 String field = QueryField(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
93 if( field == null ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
94 field = defaultField; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
95 Query query = NestedTerm(field); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
96 if( query == null ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 query = RangeTerm(field); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
98 if( query == null ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
99 parser.begin(); |
1362
77f2d091f17f
minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
100 String match = SimpleTerm(NOT_IN_TERM); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
101 query = fieldParser.getQuery(this,field,match); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
102 parser.success(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
103 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
104 if( parser.match('^') ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
105 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
106 int start = parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
107 try { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
108 while( parser.anyOf("0123456789.") ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
109 String match = parser.textFrom(start); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
110 float boost = Float.parseFloat(match); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
111 query.setBoost(boost); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 } catch(NumberFormatException e) { |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
113 throw exception(e); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
114 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
115 parser.success(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
116 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
117 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
118 BooleanClause bc = new BooleanClause(query,occur); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
119 return bc; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
120 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
121 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
122 private Query NestedTerm(String field) throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
123 parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
124 if( !parser.match('(') ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
125 return parser.failure(null); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
126 BooleanQuery bq = new BooleanQuery(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
127 while( !parser.match(')') ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
128 if( parser.endOfInput() ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
129 throw exception("unclosed parentheses"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
130 bq.add( Term(field) ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
131 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
132 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 BooleanClause[] clauses = bq.getClauses(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
134 switch( clauses.length ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
135 case 0: |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
136 throw exception("empty parentheses"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
137 case 1: |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
138 { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
139 BooleanClause bc = clauses[0]; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
140 if( bc.getOccur() != BooleanClause.Occur.MUST_NOT ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
141 return parser.success(bc.getQuery()); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
142 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
143 default: |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
144 return parser.success(bq); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
145 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
146 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
147 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
148 private Query RangeTerm(String field) throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
149 parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
150 if( !parser.anyOf("[{") ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
151 return parser.failure(null); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
152 boolean includeMin = parser.lastChar() == '['; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
153 Spaces(); |
1362
77f2d091f17f
minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
154 String minQuery = SimpleTerm(NOT_IN_RANGE); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
155 TO(); |
1362
77f2d091f17f
minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
156 String maxQuery = SimpleTerm(NOT_IN_RANGE); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
157 if( !parser.anyOf("]}") ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
158 throw exception("unclosed range"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
159 boolean includeMax = parser.lastChar() == ']'; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
160 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
161 Query query = fieldParser.getRangeQuery(this,field,minQuery,maxQuery,includeMin,includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
162 return parser.success(query); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
163 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
164 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
165 private void TO() throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
166 parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
167 if( !(parser.match("TO") && Space()) ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
168 throw exception("'TO' expected"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
169 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
170 parser.success(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
171 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
172 |
1362
77f2d091f17f
minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
173 private String SimpleTerm(String exclude) throws ParseException { |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
174 parser.begin(); |
1422 | 175 String match = Quoted(); |
176 if( match==null ) | |
1362
77f2d091f17f
minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
177 match = Unquoted(exclude); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
178 if( match.length() == 0 ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
179 throw exception("invalid input"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
180 return parser.success(match); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
181 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
182 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
183 private String QueryField() throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
184 parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
185 String match = Field(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
186 if( match==null || !parser.match(':') ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
187 return parser.failure((String)null); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
188 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
189 return parser.success(match); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
190 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
191 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
192 private String Field() throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
193 parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
194 String match = Unquoted(NOT_IN_FIELD); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
195 if( match.length()==0 ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
196 return parser.failure((String)null); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
197 match = StringFieldParser.escape(this,match); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
198 return parser.success(match); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
199 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
200 |
1422 | 201 private String Quoted() throws ParseException { |
202 parser.begin(); | |
203 if( !parser.match('"') ) | |
204 return parser.failure(null); | |
205 StringBuilder sb = new StringBuilder(); | |
206 while( parser.anyChar() ) { | |
207 char c = parser.lastChar(); | |
208 switch(c) { | |
209 case '"': | |
1564
b1195cfe8712
lucene/queryparser fix
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
210 Spaces(); |
1422 | 211 return parser.success(sb.toString()); |
212 case '\\': | |
213 if( parser.anyChar() ) { | |
214 c = parser.lastChar(); | |
215 switch(c) { | |
216 case '"': | |
217 case '\\': | |
218 sb.append(c); | |
219 continue; | |
220 case 'b': | |
221 sb.append('\b'); | |
222 continue; | |
223 case 'f': | |
224 sb.append('\f'); | |
225 continue; | |
226 case 'n': | |
227 sb.append('\n'); | |
228 continue; | |
229 case 'r': | |
230 sb.append('\r'); | |
231 continue; | |
232 case 't': | |
233 sb.append('\t'); | |
234 continue; | |
235 case 'u': | |
236 int n = 0; | |
237 for( int i=0; i<4; i++ ) { | |
238 int d; | |
239 if( parser.inCharRange('0','9') ) { | |
240 d = parser.lastChar() - '0'; | |
241 } else if( parser.inCharRange('a','f') ) { | |
242 d = parser.lastChar() - 'a' + 10; | |
243 } else if( parser.inCharRange('A','F') ) { | |
244 d = parser.lastChar() - 'A' + 10; | |
245 } else { | |
246 throw exception("invalid hex digit"); | |
247 } | |
248 n = 16*n + d; | |
249 } | |
250 sb.append((char)n); | |
251 continue; | |
252 } | |
253 } | |
254 throw exception("invalid escape char"); | |
255 default: | |
256 sb.append(c); | |
257 } | |
258 } | |
259 parser.failure(); | |
260 throw exception("unclosed string"); | |
261 } | |
262 | |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
263 private String Unquoted(String exclude) throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
264 int start = parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
265 while( parser.noneOf(exclude) ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
266 checkEscape(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
267 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
268 String match = parser.textFrom(start); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
269 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
270 return parser.success(match); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
271 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
272 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
273 private void checkEscape() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
274 if( parser.lastChar() == '\\' ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
275 parser.anyChar(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
276 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
277 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
278 private void Spaces() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
279 while( Space() ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
280 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
281 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
282 private boolean Space() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
283 return parser.anyOf(" \t\r\n"); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
284 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
285 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
286 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
287 // sort |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
288 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
289 private Sort parseSort() throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
290 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
291 if( parser.endOfInput() ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
292 return null; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
293 List<SortField> list = new ArrayList<SortField>(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
294 list.add( SortField() ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
295 while( !parser.endOfInput() ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
296 parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
297 if( !parser.match(',') ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
298 throw exception("',' expected"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
299 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
300 parser.success(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
301 list.add( SortField() ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
302 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
303 return new Sort(list.toArray(new SortField[0])); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
304 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
305 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
306 private SortField SortField() throws ParseException { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
307 parser.begin(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
308 String field = Field(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
309 if( field==null ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
310 throw exception("invalid input"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
311 boolean reverse = !parser.matchIgnoreCase("asc") && parser.matchIgnoreCase("desc"); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
312 Spaces(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
313 SortField sf = fieldParser.getSortField(this,field,reverse); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
314 return parser.success(sf); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
315 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
316 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
317 } |