Mercurial Hosting > luan
annotate src/goodjava/lucene/queryparser/MultiFieldParser.java @ 1579:dd881eb03d87
yourkit link
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 10 Feb 2021 22:49:47 -0700 |
parents | c6b50b7b958c |
children |
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.Map; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 import java.util.HashMap; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import org.apache.lucene.search.Query; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import org.apache.lucene.search.BooleanQuery; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 import org.apache.lucene.search.BooleanClause; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 import org.apache.lucene.search.SortField; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
9 import goodjava.parser.ParseException; |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 public class MultiFieldParser implements FieldParser { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 |
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 * maps field name to FieldParser |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 */ |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 public final Map<String,FieldParser> fields = new HashMap<String,FieldParser>(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 public boolean allowUnspecifiedFields = false; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 private final FieldParser defaultFieldParser; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 private final String[] defaultFields; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 public MultiFieldParser() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 this.defaultFieldParser = null; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 this.defaultFields = null; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 public MultiFieldParser(FieldParser defaultFieldParser,String... defaultFields) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 this.defaultFieldParser = defaultFieldParser; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 this.defaultFields = defaultFields; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 for( String field : defaultFields ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 fields.put(field,defaultFieldParser); |
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 |
1459 | 35 @Override public Query getQuery(GoodQueryParser qp,String field,String query) throws ParseException { |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 if( field == null ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 if( defaultFieldParser == null ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
38 throw qp.exception("no defaults were specified, so a field is required"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 if( defaultFields.length == 1 ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 return defaultFieldParser.getQuery(qp,defaultFields[0],query); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 BooleanQuery bq = new BooleanQuery(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 for( String f : defaultFields ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 bq.add( defaultFieldParser.getQuery(qp,f,query), BooleanClause.Occur.SHOULD ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 return bq; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 } else { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 FieldParser fp = fields.get(field); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 if( fp != null ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 return fp.getQuery(qp,field,query); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 if( allowUnspecifiedFields ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 return defaultFieldParser.getQuery(qp,field,query); |
1573
c6b50b7b958c
lucene/Web_search improvement
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
52 throw qp.exception("undefined query field '"+field+"'"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 |
1459 | 56 @Override public Query getRangeQuery(GoodQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException { |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 if( field == null ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 if( defaultFieldParser == null ) |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
59 throw qp.exception("no defaults were specified, so a field is required"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 if( defaultFields.length == 1 ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 return defaultFieldParser.getRangeQuery(qp,defaultFields[0],minQuery,maxQuery,includeMin,includeMax); |
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 for( String f : defaultFields ) { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
64 bq.add( defaultFieldParser.getRangeQuery(qp,f,minQuery,maxQuery,includeMin,includeMax), BooleanClause.Occur.SHOULD ); |
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 return bq; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 } else { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
68 FieldParser fp = fields.get(field); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
69 if( fp != null ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
70 return fp.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 if( allowUnspecifiedFields ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
72 return defaultFieldParser.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax); |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
73 throw qp.exception("field '"+field+"' not specified"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 } |
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 |
1459 | 77 @Override public SortField getSortField(GoodQueryParser qp,String field,boolean reverse) throws ParseException { |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 FieldParser fp = fields.get(field); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
79 if( fp != null ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
80 return fp.getSortField(qp,field,reverse); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 if( allowUnspecifiedFields ) |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 return defaultFieldParser.getSortField(qp,field,reverse); |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
83 throw qp.exception("field '"+field+"' not specified"); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
85 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
86 } |