comparison src/goodjava/lucene/queryparser/NumberFieldParser.java @ 1458:6b6c11c9164e

goodjava.lucene
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 20 Mar 2020 10:58:53 -0600
parents src/goodjava/queryparser/NumberFieldParser.java@f6075d7a36f2
children b04b8fc5f4f4
comparison
equal deleted inserted replaced
1457:a84ce37f3892 1458:6b6c11c9164e
1 package goodjava.lucene.queryparser;
2
3 import org.apache.lucene.search.Query;
4 import org.apache.lucene.search.NumericRangeQuery;
5 import org.apache.lucene.search.SortField;
6 import org.apache.lucene.search.PrefixQuery;
7 import org.apache.lucene.index.Term;
8 import goodjava.parser.ParseException;
9
10
11 public abstract class NumberFieldParser implements FieldParser {
12
13 @Override public final Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
14 if( query.equals("*") )
15 return new PrefixQuery(new Term(field,""));
16 return getRangeQuery(qp,field,query,query,true,true);
17 }
18
19 @Override public final Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
20 try {
21 return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax);
22 } catch(NumberFormatException e) {
23 throw qp.exception(e);
24 }
25 }
26
27 abstract protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax);
28
29 @Override public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) {
30 return new SortField( field, sortType(), reverse );
31 }
32
33 abstract protected SortField.Type sortType();
34
35
36 public static final FieldParser INT = new NumberFieldParser() {
37
38 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
39 Integer min = minQuery.equals("*") ? null : Integer.valueOf(minQuery);
40 Integer max = maxQuery.equals("*") ? null : Integer.valueOf(maxQuery);
41 return NumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax);
42 }
43
44 @Override protected SortField.Type sortType() {
45 return SortField.Type.INT;
46 }
47 };
48
49 public static final FieldParser LONG = new NumberFieldParser() {
50
51 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
52 Long min = minQuery.equals("*") ? null : Long.valueOf(minQuery);
53 Long max = maxQuery.equals("*") ? null : Long.valueOf(maxQuery);
54 return NumericRangeQuery.newLongRange(field,min,max,includeMin,includeMax);
55 }
56
57 @Override protected SortField.Type sortType() {
58 return SortField.Type.LONG;
59 }
60 };
61
62 public static final FieldParser FLOAT = new NumberFieldParser() {
63
64 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
65 Float min = minQuery.equals("*") ? null : Float.valueOf(minQuery);
66 Float max = maxQuery.equals("*") ? null : Float.valueOf(maxQuery);
67 return NumericRangeQuery.newFloatRange(field,min,max,includeMin,includeMax);
68 }
69
70 @Override protected SortField.Type sortType() {
71 return SortField.Type.FLOAT;
72 }
73 };
74
75 public static final FieldParser DOUBLE = new NumberFieldParser() {
76
77 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
78 Double min = minQuery.equals("*") ? null : Double.valueOf(minQuery);
79 Double max = maxQuery.equals("*") ? null : Double.valueOf(maxQuery);
80 return NumericRangeQuery.newDoubleRange(field,min,max,includeMin,includeMax);
81 }
82
83 @Override protected SortField.Type sortType() {
84 return SortField.Type.DOUBLE;
85 }
86 };
87
88 }