1 package org.apache.lucene.queryParser; 2 3 18 19 import java.util.Vector ; 20 21 import org.apache.lucene.analysis.Analyzer; 22 import org.apache.lucene.search.BooleanClause; 23 import org.apache.lucene.search.BooleanQuery; 24 import org.apache.lucene.search.MultiPhraseQuery; 25 import org.apache.lucene.search.PhraseQuery; 26 import org.apache.lucene.search.Query; 27 28 34 public class MultiFieldQueryParser extends QueryParser 35 { 36 37 private String [] fields; 38 39 59 public MultiFieldQueryParser(String [] fields, Analyzer analyzer) { 60 super(null, analyzer); 61 this.fields = fields; 62 } 63 64 protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException { 65 if (field == null) { 66 Vector clauses = new Vector (); 67 for (int i = 0; i < fields.length; i++) { 68 Query q = super.getFieldQuery(fields[i], queryText); 69 if (q != null) { 70 if (q instanceof PhraseQuery) { 71 ((PhraseQuery) q).setSlop(slop); 72 } 73 if (q instanceof MultiPhraseQuery) { 74 ((MultiPhraseQuery) q).setSlop(slop); 75 } 76 clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD)); 77 } 78 } 79 if (clauses.size() == 0) return null; 81 return getBooleanQuery(clauses, true); 82 } 83 return super.getFieldQuery(field, queryText); 84 } 85 86 87 protected Query getFieldQuery(String field, String queryText) throws ParseException { 88 return getFieldQuery(field, queryText, 0); 89 } 90 91 94 protected Query getFieldQuery(String field, Analyzer analyzer, String queryText) 95 throws ParseException { 96 return getFieldQuery(field, queryText); 97 } 98 99 102 protected Query getFuzzyQuery(String field, String termStr) throws ParseException { 103 return getFuzzyQuery(field, termStr, fuzzyMinSim); 104 } 105 106 protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException 107 { 108 if (field == null) { 109 Vector clauses = new Vector (); 110 for (int i = 0; i < fields.length; i++) { 111 clauses.add(new BooleanClause(super.getFuzzyQuery(fields[i], termStr, minSimilarity), 112 BooleanClause.Occur.SHOULD)); 113 } 114 return getBooleanQuery(clauses, true); 115 } 116 return super.getFuzzyQuery(field, termStr, minSimilarity); 117 } 118 119 protected Query getPrefixQuery(String field, String termStr) throws ParseException 120 { 121 if (field == null) { 122 Vector clauses = new Vector (); 123 for (int i = 0; i < fields.length; i++) { 124 clauses.add(new BooleanClause(super.getPrefixQuery(fields[i], termStr), 125 BooleanClause.Occur.SHOULD)); 126 } 127 return getBooleanQuery(clauses, true); 128 } 129 return super.getPrefixQuery(field, termStr); 130 } 131 132 protected Query getWildcardQuery(String field, String termStr) throws ParseException { 133 if (field == null) { 134 Vector clauses = new Vector (); 135 for (int i = 0; i < fields.length; i++) { 136 clauses.add(new BooleanClause(super.getWildcardQuery(fields[i], termStr), 137 BooleanClause.Occur.SHOULD)); 138 } 139 return getBooleanQuery(clauses, true); 140 } 141 return super.getWildcardQuery(field, termStr); 142 } 143 144 147 protected Query getRangeQuery(String field, Analyzer analyzer, 148 String part1, String part2, boolean inclusive) throws ParseException { 149 return getRangeQuery(field, part1, part2, inclusive); 150 } 151 152 protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException { 153 if (field == null) { 154 Vector clauses = new Vector (); 155 for (int i = 0; i < fields.length; i++) { 156 clauses.add(new BooleanClause(super.getRangeQuery(fields[i], part1, part2, inclusive), 157 BooleanClause.Occur.SHOULD)); 158 } 159 return getBooleanQuery(clauses, true); 160 } 161 return super.getRangeQuery(field, part1, part2, inclusive); 162 } 163 164 165 166 public static final int NORMAL_FIELD = 0; 167 168 public static final int REQUIRED_FIELD = 1; 169 170 public static final int PROHIBITED_FIELD = 2; 171 172 175 public MultiFieldQueryParser(QueryParserTokenManager tm) 176 { 177 super(tm); 178 } 179 180 183 public MultiFieldQueryParser(CharStream stream) 184 { 185 super(stream); 186 } 187 188 191 public MultiFieldQueryParser(String f, Analyzer a) 192 { 193 super(f, a); 194 } 195 196 215 public static Query parse(String query, String [] fields, Analyzer analyzer) 216 throws ParseException 217 { 218 BooleanQuery bQuery = new BooleanQuery(); 219 for (int i = 0; i < fields.length; i++) 220 { 221 Query q = parse(query, fields[i], analyzer); 222 bQuery.add(q, BooleanClause.Occur.SHOULD); 223 } 224 return bQuery; 225 } 226 227 244 public static Query parse(String [] queries, String [] fields, 245 Analyzer analyzer) throws ParseException 246 { 247 if (queries.length != fields.length) 248 throw new IllegalArgumentException ("queries.length != fields.length"); 249 BooleanQuery bQuery = new BooleanQuery(); 250 for (int i = 0; i < fields.length; i++) 251 { 252 QueryParser qp = new QueryParser(fields[i], analyzer); 253 Query q = qp.parse(queries[i]); 254 bQuery.add(q, BooleanClause.Occur.SHOULD); 255 } 256 return bQuery; 257 } 258 259 291 public static Query parse(String query, String [] fields, int[] flags, 292 Analyzer analyzer) throws ParseException 293 { 294 if (fields.length != flags.length) 295 throw new IllegalArgumentException ("fields.length != flags.length"); 296 BooleanQuery bQuery = new BooleanQuery(); 297 for (int i = 0; i < fields.length; i++) 298 { 299 QueryParser qp = new QueryParser(fields[i], analyzer); 300 Query q = qp.parse(query); 301 int flag = flags[i]; 302 switch (flag) 303 { 304 case REQUIRED_FIELD: 305 bQuery.add(q, BooleanClause.Occur.MUST); 306 break; 307 case PROHIBITED_FIELD: 308 bQuery.add(q, BooleanClause.Occur.MUST_NOT); 309 break; 310 default: 311 bQuery.add(q, BooleanClause.Occur.SHOULD); 312 break; 313 } 314 } 315 return bQuery; 316 } 317 318 349 public static Query parse(String query, String [] fields, 350 BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException { 351 if (fields.length != flags.length) 352 throw new IllegalArgumentException ("fields.length != flags.length"); 353 BooleanQuery bQuery = new BooleanQuery(); 354 for (int i = 0; i < fields.length; i++) { 355 QueryParser qp = new QueryParser(fields[i], analyzer); 356 Query q = qp.parse(query); 357 bQuery.add(q, flags[i]); 358 } 359 return bQuery; 360 } 361 362 394 public static Query parse(String [] queries, String [] fields, int[] flags, 395 Analyzer analyzer) throws ParseException 396 { 397 if (!(queries.length == fields.length && queries.length == flags.length)) 398 throw new IllegalArgumentException ("queries, fields, and flags array have have different length"); 399 BooleanQuery bQuery = new BooleanQuery(); 400 for (int i = 0; i < fields.length; i++) 401 { 402 QueryParser qp = new QueryParser(fields[i], analyzer); 403 Query q = qp.parse(queries[i]); 404 int flag = flags[i]; 405 switch (flag) 406 { 407 case REQUIRED_FIELD: 408 bQuery.add(q, BooleanClause.Occur.MUST); 409 break; 410 case PROHIBITED_FIELD: 411 bQuery.add(q, BooleanClause.Occur.MUST_NOT); 412 break; 413 default: 414 bQuery.add(q, BooleanClause.Occur.SHOULD); 415 break; 416 } 417 } 418 return bQuery; 419 } 420 421 453 public static Query parse(String [] queries, String [] fields, BooleanClause.Occur[] flags, 454 Analyzer analyzer) throws ParseException 455 { 456 if (!(queries.length == fields.length && queries.length == flags.length)) 457 throw new IllegalArgumentException ("queries, fields, and flags array have have different length"); 458 BooleanQuery bQuery = new BooleanQuery(); 459 for (int i = 0; i < fields.length; i++) 460 { 461 QueryParser qp = new QueryParser(fields[i], analyzer); 462 Query q = qp.parse(queries[i]); 463 bQuery.add(q, flags[i]); 464 } 465 return bQuery; 466 } 467 468 } 469 | Popular Tags |