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