Mercurial Hosting > luan
annotate src/goodjava/lucene/queryparser/NumberFieldParser.java @ 1831:8f9ae295bf6a default tip
add Hosted.authorize
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 26 Sep 2024 15:07:45 -0600 |
parents | c62324841dfb |
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 org.apache.lucene.search.Query; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 import org.apache.lucene.search.NumericRangeQuery; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import org.apache.lucene.search.SortField; |
1433 | 6 import org.apache.lucene.search.PrefixQuery; |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
7 import org.apache.lucene.search.TermQuery; |
1433 | 8 import org.apache.lucene.index.Term; |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
9 import org.apache.lucene.util.BytesRef; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1344
diff
changeset
|
10 import goodjava.parser.ParseException; |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
11 import org.apache.lucene.util.NumericUtils; |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 |
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 public abstract class NumberFieldParser implements FieldParser { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 |
1459 | 16 @Override public final Query getQuery(GoodQueryParser qp,String field,String query) throws ParseException { |
1433 | 17 if( query.equals("*") ) |
18 return new PrefixQuery(new Term(field,"")); | |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
19 try { |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
20 return getQuery(field,query); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
21 } catch(NumberFormatException e) { |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
22 throw qp.exception(e); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
23 } |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
24 } |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
25 |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
26 protected Query getQuery(String field,String query) { |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
27 return getRangeQuery(field,query,query,true,true); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
1459 | 30 @Override public final 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
|
31 try { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 } catch(NumberFormatException e) { |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
34 throw qp.exception(e); |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 abstract protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 |
1459 | 40 @Override public SortField getSortField(GoodQueryParser qp,String field,boolean reverse) { |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 return new SortField( field, sortType(), reverse ); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 abstract protected SortField.Type sortType(); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 public static final FieldParser INT = new NumberFieldParser() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
49 @Override protected Query getQuery(String field,String query) { |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
50 int n = Integer.valueOf(query); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
51 return new TermQuery( term(field,n) ); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
52 } |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
53 |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { |
1445 | 55 Integer min = minQuery.equals("*") ? null : Integer.valueOf(minQuery); |
56 Integer max = maxQuery.equals("*") ? null : Integer.valueOf(maxQuery); | |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 return NumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 @Override protected SortField.Type sortType() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 return SortField.Type.INT; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 }; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
64 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
65 public static final FieldParser LONG = new NumberFieldParser() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
67 @Override protected Query getQuery(String field,String query) { |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
68 long n = Long.valueOf(query); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
69 return new TermQuery( term(field,n) ); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
70 } |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
71 |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
72 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { |
1445 | 73 Long min = minQuery.equals("*") ? null : Long.valueOf(minQuery); |
74 Long max = maxQuery.equals("*") ? null : Long.valueOf(maxQuery); | |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
75 return NumericRangeQuery.newLongRange(field,min,max,includeMin,includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
76 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
77 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 @Override protected SortField.Type sortType() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
79 return SortField.Type.LONG; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
80 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 }; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 public static final FieldParser FLOAT = new NumberFieldParser() { |
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 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { |
1445 | 86 Float min = minQuery.equals("*") ? null : Float.valueOf(minQuery); |
87 Float max = maxQuery.equals("*") ? null : Float.valueOf(maxQuery); | |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
88 return NumericRangeQuery.newFloatRange(field,min,max,includeMin,includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
89 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
90 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
91 @Override protected SortField.Type sortType() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
92 return SortField.Type.FLOAT; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
93 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
94 }; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
95 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
96 public static final FieldParser DOUBLE = new NumberFieldParser() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
98 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { |
1445 | 99 Double min = minQuery.equals("*") ? null : Double.valueOf(minQuery); |
100 Double max = maxQuery.equals("*") ? null : Double.valueOf(maxQuery); | |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
101 return NumericRangeQuery.newDoubleRange(field,min,max,includeMin,includeMax); |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
102 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
103 |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
104 @Override protected SortField.Type sortType() { |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
105 return SortField.Type.DOUBLE; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
106 } |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
107 }; |
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
108 |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
109 public static Term term(String name,int value) { |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
110 BytesRef br = new BytesRef(); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
111 NumericUtils.intToPrefixCoded(value,0,br); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
112 return new Term(name,br); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
113 } |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
114 |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
115 public static Term term(String name,long value) { |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
116 BytesRef br = new BytesRef(); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
117 NumericUtils.longToPrefixCoded(value,0,br); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
118 return new Term(name,br); |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
119 } |
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1459
diff
changeset
|
120 |
730
01e68da6983b
add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
121 } |