Mercurial Hosting > luan
comparison src/goodjava/queryparser/MultiFieldParser.java @ 1402:27efb1fcbcb5
move luan.lib to goodjava
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 17 Sep 2019 01:35:01 -0400 |
parents | src/luan/lib/queryparser/MultiFieldParser.java@dc2af9d5463b |
children |
comparison
equal
deleted
inserted
replaced
1401:ef1620aa99cb | 1402:27efb1fcbcb5 |
---|---|
1 package goodjava.queryparser; | |
2 | |
3 import java.util.Map; | |
4 import java.util.HashMap; | |
5 import org.apache.lucene.search.Query; | |
6 import org.apache.lucene.search.BooleanQuery; | |
7 import org.apache.lucene.search.BooleanClause; | |
8 import org.apache.lucene.search.SortField; | |
9 import goodjava.parser.ParseException; | |
10 | |
11 | |
12 public class MultiFieldParser implements FieldParser { | |
13 | |
14 /** | |
15 * maps field name to FieldParser | |
16 */ | |
17 public final Map<String,FieldParser> fields = new HashMap<String,FieldParser>(); | |
18 public boolean allowUnspecifiedFields = false; | |
19 private final FieldParser defaultFieldParser; | |
20 private final String[] defaultFields; | |
21 | |
22 public MultiFieldParser() { | |
23 this.defaultFieldParser = null; | |
24 this.defaultFields = null; | |
25 } | |
26 | |
27 public MultiFieldParser(FieldParser defaultFieldParser,String... defaultFields) { | |
28 this.defaultFieldParser = defaultFieldParser; | |
29 this.defaultFields = defaultFields; | |
30 for( String field : defaultFields ) { | |
31 fields.put(field,defaultFieldParser); | |
32 } | |
33 } | |
34 | |
35 @Override public Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException { | |
36 if( field == null ) { | |
37 if( defaultFieldParser == null ) | |
38 throw qp.exception("no defaults were specified, so a field is required"); | |
39 if( defaultFields.length == 1 ) | |
40 return defaultFieldParser.getQuery(qp,defaultFields[0],query); | |
41 BooleanQuery bq = new BooleanQuery(); | |
42 for( String f : defaultFields ) { | |
43 bq.add( defaultFieldParser.getQuery(qp,f,query), BooleanClause.Occur.SHOULD ); | |
44 } | |
45 return bq; | |
46 } else { | |
47 FieldParser fp = fields.get(field); | |
48 if( fp != null ) | |
49 return fp.getQuery(qp,field,query); | |
50 if( allowUnspecifiedFields ) | |
51 return defaultFieldParser.getQuery(qp,field,query); | |
52 throw qp.exception("unrecognized field '"+field+"'"); | |
53 } | |
54 } | |
55 | |
56 @Override public Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException { | |
57 if( field == null ) { | |
58 if( defaultFieldParser == null ) | |
59 throw qp.exception("no defaults were specified, so a field is required"); | |
60 if( defaultFields.length == 1 ) | |
61 return defaultFieldParser.getRangeQuery(qp,defaultFields[0],minQuery,maxQuery,includeMin,includeMax); | |
62 BooleanQuery bq = new BooleanQuery(); | |
63 for( String f : defaultFields ) { | |
64 bq.add( defaultFieldParser.getRangeQuery(qp,f,minQuery,maxQuery,includeMin,includeMax), BooleanClause.Occur.SHOULD ); | |
65 } | |
66 return bq; | |
67 } else { | |
68 FieldParser fp = fields.get(field); | |
69 if( fp != null ) | |
70 return fp.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax); | |
71 if( allowUnspecifiedFields ) | |
72 return defaultFieldParser.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax); | |
73 throw qp.exception("field '"+field+"' not specified"); | |
74 } | |
75 } | |
76 | |
77 @Override public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) throws ParseException { | |
78 FieldParser fp = fields.get(field); | |
79 if( fp != null ) | |
80 return fp.getSortField(qp,field,reverse); | |
81 if( allowUnspecifiedFields ) | |
82 return defaultFieldParser.getSortField(qp,field,reverse); | |
83 throw qp.exception("field '"+field+"' not specified"); | |
84 } | |
85 | |
86 } |