comparison src/goodjava/queryparser/NumberFieldParser.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/queryparser/NumberFieldParser.java@dc2af9d5463b
children 5f038be65271
comparison
equal deleted inserted replaced
1401:ef1620aa99cb 1402:27efb1fcbcb5
1 package goodjava.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 goodjava.parser.ParseException;
7
8
9 public abstract class NumberFieldParser implements FieldParser {
10
11 @Override public final Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
12 return getRangeQuery(qp,field,query,query,true,true);
13 }
14
15 @Override public final Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
16 try {
17 return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax);
18 } catch(NumberFormatException e) {
19 throw qp.exception(e);
20 }
21 }
22
23 abstract protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax);
24
25 @Override public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) {
26 return new SortField( field, sortType(), reverse );
27 }
28
29 abstract protected SortField.Type sortType();
30
31
32 public static final FieldParser INT = new NumberFieldParser() {
33
34 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
35 int min = Integer.parseInt(minQuery);
36 int max = Integer.parseInt(maxQuery);
37 return NumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax);
38 }
39
40 @Override protected SortField.Type sortType() {
41 return SortField.Type.INT;
42 }
43 };
44
45 public static final FieldParser LONG = new NumberFieldParser() {
46
47 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
48 long min = Long.parseLong(minQuery);
49 long max = Long.parseLong(maxQuery);
50 return NumericRangeQuery.newLongRange(field,min,max,includeMin,includeMax);
51 }
52
53 @Override protected SortField.Type sortType() {
54 return SortField.Type.LONG;
55 }
56 };
57
58 public static final FieldParser FLOAT = new NumberFieldParser() {
59
60 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
61 float min = Float.parseFloat(minQuery);
62 float max = Float.parseFloat(maxQuery);
63 return NumericRangeQuery.newFloatRange(field,min,max,includeMin,includeMax);
64 }
65
66 @Override protected SortField.Type sortType() {
67 return SortField.Type.FLOAT;
68 }
69 };
70
71 public static final FieldParser DOUBLE = new NumberFieldParser() {
72
73 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
74 double min = Double.parseDouble(minQuery);
75 double max = Double.parseDouble(maxQuery);
76 return NumericRangeQuery.newDoubleRange(field,min,max,includeMin,includeMax);
77 }
78
79 @Override protected SortField.Type sortType() {
80 return SortField.Type.DOUBLE;
81 }
82 };
83
84 }