6120 |
05 Feb 21 |
nicklas |
1 |
package net.sf.basedb.varsearch.query; |
6120 |
05 Feb 21 |
nicklas |
2 |
|
6120 |
05 Feb 21 |
nicklas |
3 |
import java.util.Map; |
6120 |
05 Feb 21 |
nicklas |
4 |
|
6120 |
05 Feb 21 |
nicklas |
5 |
import org.apache.lucene.analysis.Analyzer; |
6121 |
08 Feb 21 |
nicklas |
6 |
import org.apache.lucene.queryparser.classic.ParseException; |
6120 |
05 Feb 21 |
nicklas |
7 |
import org.apache.lucene.queryparser.classic.QueryParser; |
6120 |
05 Feb 21 |
nicklas |
8 |
import org.apache.lucene.search.BoostQuery; |
6120 |
05 Feb 21 |
nicklas |
9 |
import org.apache.lucene.search.Query; |
6120 |
05 Feb 21 |
nicklas |
10 |
import org.apache.lucene.index.Term; |
6120 |
05 Feb 21 |
nicklas |
11 |
|
6120 |
05 Feb 21 |
nicklas |
12 |
/** |
6120 |
05 Feb 21 |
nicklas |
An extension to the classic query parser that adds support for handling some fields |
6120 |
05 Feb 21 |
nicklas |
with custom code. The main reason is to enable numeric queries that uses |
6120 |
05 Feb 21 |
nicklas |
IntPoint or LongPoint to create queries. Otherwise fields that have been indexed |
6120 |
05 Feb 21 |
nicklas |
as numeric will not match. |
6120 |
05 Feb 21 |
nicklas |
17 |
|
6120 |
05 Feb 21 |
nicklas |
@author nicklas |
6120 |
05 Feb 21 |
nicklas |
19 |
*/ |
6120 |
05 Feb 21 |
nicklas |
20 |
public class FieldAwareQueryParser |
6120 |
05 Feb 21 |
nicklas |
21 |
extends QueryParser |
6120 |
05 Feb 21 |
nicklas |
22 |
{ |
6120 |
05 Feb 21 |
nicklas |
23 |
|
6120 |
05 Feb 21 |
nicklas |
24 |
|
6120 |
05 Feb 21 |
nicklas |
25 |
private final Map<String, QueryField> custom; |
6120 |
05 Feb 21 |
nicklas |
26 |
|
6120 |
05 Feb 21 |
nicklas |
27 |
/** |
6120 |
05 Feb 21 |
nicklas |
Creates a new query parser with a map that specify how to handle |
6120 |
05 Feb 21 |
nicklas |
specific fields. |
6120 |
05 Feb 21 |
nicklas |
30 |
*/ |
6120 |
05 Feb 21 |
nicklas |
31 |
public FieldAwareQueryParser(String defaultField, Analyzer analyzer, Map<String, QueryField> custom) |
6120 |
05 Feb 21 |
nicklas |
32 |
{ |
6120 |
05 Feb 21 |
nicklas |
33 |
super(defaultField, analyzer); |
6120 |
05 Feb 21 |
nicklas |
34 |
this.custom = custom; |
6120 |
05 Feb 21 |
nicklas |
35 |
} |
6120 |
05 Feb 21 |
nicklas |
36 |
|
6120 |
05 Feb 21 |
nicklas |
37 |
/** |
6120 |
05 Feb 21 |
nicklas |
Overrides the default implementation if the field requires a special query. |
6120 |
05 Feb 21 |
nicklas |
39 |
*/ |
6120 |
05 Feb 21 |
nicklas |
40 |
@Override |
6120 |
05 Feb 21 |
nicklas |
41 |
public Query newTermQuery(Term term, float boost) |
6120 |
05 Feb 21 |
nicklas |
42 |
{ |
6121 |
08 Feb 21 |
nicklas |
43 |
Query q = null; |
6128 |
12 Feb 21 |
nicklas |
44 |
QueryField qf = custom.get(term.field()); |
6121 |
08 Feb 21 |
nicklas |
45 |
if (qf != null) |
6121 |
08 Feb 21 |
nicklas |
46 |
{ |
6121 |
08 Feb 21 |
nicklas |
47 |
q = qf.newTermQuery(term); |
6121 |
08 Feb 21 |
nicklas |
48 |
if (q != null && boost != 1.0f) |
6121 |
08 Feb 21 |
nicklas |
49 |
{ |
6121 |
08 Feb 21 |
nicklas |
50 |
q = new BoostQuery(q, boost); |
6121 |
08 Feb 21 |
nicklas |
51 |
} |
6121 |
08 Feb 21 |
nicklas |
52 |
} |
6121 |
08 Feb 21 |
nicklas |
53 |
if (q == null) |
6121 |
08 Feb 21 |
nicklas |
54 |
{ |
6121 |
08 Feb 21 |
nicklas |
55 |
q = super.newTermQuery(term, boost); |
6121 |
08 Feb 21 |
nicklas |
56 |
} |
6121 |
08 Feb 21 |
nicklas |
57 |
return q; |
6120 |
05 Feb 21 |
nicklas |
58 |
} |
6120 |
05 Feb 21 |
nicklas |
59 |
|
6121 |
08 Feb 21 |
nicklas |
60 |
@Override |
6121 |
08 Feb 21 |
nicklas |
61 |
protected Query getWildcardQuery(String field, String term) |
6121 |
08 Feb 21 |
nicklas |
62 |
throws ParseException |
6121 |
08 Feb 21 |
nicklas |
63 |
{ |
6121 |
08 Feb 21 |
nicklas |
64 |
Query q = null; |
6121 |
08 Feb 21 |
nicklas |
65 |
QueryField qf = custom.get(field); |
6121 |
08 Feb 21 |
nicklas |
66 |
if (qf != null) |
6121 |
08 Feb 21 |
nicklas |
67 |
{ |
6121 |
08 Feb 21 |
nicklas |
68 |
q = qf.newWildcardQuery(field, term); |
6121 |
08 Feb 21 |
nicklas |
69 |
} |
6121 |
08 Feb 21 |
nicklas |
70 |
if (q == null) |
6121 |
08 Feb 21 |
nicklas |
71 |
{ |
6121 |
08 Feb 21 |
nicklas |
72 |
q = super.getWildcardQuery(field, term); |
6121 |
08 Feb 21 |
nicklas |
73 |
} |
6121 |
08 Feb 21 |
nicklas |
74 |
return q; |
6121 |
08 Feb 21 |
nicklas |
75 |
} |
6121 |
08 Feb 21 |
nicklas |
76 |
|
6120 |
05 Feb 21 |
nicklas |
77 |
/** |
6120 |
05 Feb 21 |
nicklas |
Overrides the default implementation if the field requires a special query. |
6120 |
05 Feb 21 |
nicklas |
79 |
*/ |
6120 |
05 Feb 21 |
nicklas |
80 |
@Override |
6120 |
05 Feb 21 |
nicklas |
81 |
protected Query newRangeQuery(String field, String part1, String part2, |
6120 |
05 Feb 21 |
nicklas |
82 |
boolean startInclusive, boolean endInclusive) |
6120 |
05 Feb 21 |
nicklas |
83 |
{ |
6121 |
08 Feb 21 |
nicklas |
84 |
Query q = null; |
6120 |
05 Feb 21 |
nicklas |
85 |
QueryField qf = custom.get(field); |
6121 |
08 Feb 21 |
nicklas |
86 |
if (qf != null) |
6121 |
08 Feb 21 |
nicklas |
87 |
{ |
6121 |
08 Feb 21 |
nicklas |
88 |
q = qf.newRangeQuery(field, part1, part2, startInclusive, endInclusive); |
6121 |
08 Feb 21 |
nicklas |
89 |
} |
6121 |
08 Feb 21 |
nicklas |
90 |
if (q == null) |
6121 |
08 Feb 21 |
nicklas |
91 |
{ |
6121 |
08 Feb 21 |
nicklas |
92 |
q = super.newRangeQuery(field, part1, part2, startInclusive, endInclusive); |
6121 |
08 Feb 21 |
nicklas |
93 |
} |
6121 |
08 Feb 21 |
nicklas |
94 |
return q; |
6120 |
05 Feb 21 |
nicklas |
95 |
} |
6120 |
05 Feb 21 |
nicklas |
96 |
|
6120 |
05 Feb 21 |
nicklas |
97 |
} |