6122 |
08 Feb 21 |
nicklas |
1 |
package net.sf.basedb.varsearch.query; |
6122 |
08 Feb 21 |
nicklas |
2 |
|
6122 |
08 Feb 21 |
nicklas |
3 |
import org.apache.lucene.index.Term; |
6122 |
08 Feb 21 |
nicklas |
4 |
import org.apache.lucene.search.Query; |
6122 |
08 Feb 21 |
nicklas |
5 |
import org.apache.lucene.search.TermQuery; |
6122 |
08 Feb 21 |
nicklas |
6 |
import org.apache.lucene.search.WildcardQuery; |
6122 |
08 Feb 21 |
nicklas |
7 |
|
6122 |
08 Feb 21 |
nicklas |
8 |
/** |
6122 |
08 Feb 21 |
nicklas |
Query field implementation that strips wildcard if |
6122 |
08 Feb 21 |
nicklas |
this is the first character. |
6122 |
08 Feb 21 |
nicklas |
11 |
|
6122 |
08 Feb 21 |
nicklas |
@author nicklas |
6122 |
08 Feb 21 |
nicklas |
13 |
*/ |
6122 |
08 Feb 21 |
nicklas |
14 |
public class StripWildcardQueryField |
6122 |
08 Feb 21 |
nicklas |
15 |
extends QueryField |
6122 |
08 Feb 21 |
nicklas |
16 |
{ |
6122 |
08 Feb 21 |
nicklas |
17 |
|
6122 |
08 Feb 21 |
nicklas |
18 |
public static final StripWildcardQueryField INSTANCE = new StripWildcardQueryField(); |
6122 |
08 Feb 21 |
nicklas |
19 |
|
6122 |
08 Feb 21 |
nicklas |
20 |
private StripWildcardQueryField() |
6122 |
08 Feb 21 |
nicklas |
21 |
{} |
6122 |
08 Feb 21 |
nicklas |
22 |
|
6122 |
08 Feb 21 |
nicklas |
23 |
@Override |
6122 |
08 Feb 21 |
nicklas |
24 |
public Query newWildcardQuery(String field, String term) |
6122 |
08 Feb 21 |
nicklas |
25 |
{ |
6122 |
08 Feb 21 |
nicklas |
26 |
Query q = null; |
6123 |
09 Feb 21 |
nicklas |
27 |
if (term.startsWith("*") || term.startsWith("?")) |
6122 |
08 Feb 21 |
nicklas |
28 |
{ |
6123 |
09 Feb 21 |
nicklas |
29 |
String term2 = term.substring(1); |
6123 |
09 Feb 21 |
nicklas |
30 |
if (term2.indexOf('*') == -1 && term2.indexOf('?') == -1) |
6122 |
08 Feb 21 |
nicklas |
31 |
{ |
6122 |
08 Feb 21 |
nicklas |
32 |
q = new TermQuery(new Term(field, term)); |
6122 |
08 Feb 21 |
nicklas |
33 |
} |
6122 |
08 Feb 21 |
nicklas |
34 |
else |
6122 |
08 Feb 21 |
nicklas |
35 |
{ |
6123 |
09 Feb 21 |
nicklas |
36 |
q = new WildcardQuery(new Term(field, term2)); |
6122 |
08 Feb 21 |
nicklas |
37 |
} |
6122 |
08 Feb 21 |
nicklas |
38 |
} |
6122 |
08 Feb 21 |
nicklas |
39 |
return q; |
6122 |
08 Feb 21 |
nicklas |
40 |
} |
6122 |
08 Feb 21 |
nicklas |
41 |
|
6122 |
08 Feb 21 |
nicklas |
42 |
|
6122 |
08 Feb 21 |
nicklas |
43 |
|
6122 |
08 Feb 21 |
nicklas |
44 |
} |