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