annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1458
6b6c11c9164e goodjava.lucene
Franklin Schmidt <fschmidt@gmail.com>
parents: 1445
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
6 import org.apache.lucene.search.PrefixQuery;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
7 import org.apache.lucene.index.Term;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
8 import goodjava.parser.ParseException;
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 public abstract class NumberFieldParser implements FieldParser {
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 @Override public final Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
1433
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
14 if( query.equals("*") )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
15 return new PrefixQuery(new Term(field,""));
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 return getRangeQuery(qp,field,query,query,true,true);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 @Override public final Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 try {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 } catch(NumberFormatException e) {
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
23 throw qp.exception(e);
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 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
28
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 @Override public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 return new SortField( field, sortType(), reverse );
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 abstract protected SortField.Type sortType();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
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 public static final FieldParser INT = new NumberFieldParser() {
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 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
1445
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
39 Integer min = minQuery.equals("*") ? null : Integer.valueOf(minQuery);
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
40 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
41 return NumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax);
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 @Override protected SortField.Type sortType() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 return SortField.Type.INT;
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 };
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 public static final FieldParser LONG = new NumberFieldParser() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
1445
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
52 Long min = minQuery.equals("*") ? null : Long.valueOf(minQuery);
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
53 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
54 return NumericRangeQuery.newLongRange(field,min,max,includeMin,includeMax);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 @Override protected SortField.Type sortType() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 return SortField.Type.LONG;
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 };
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 public static final FieldParser FLOAT = new NumberFieldParser() {
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 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
1445
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
65 Float min = minQuery.equals("*") ? null : Float.valueOf(minQuery);
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
66 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
67 return NumericRangeQuery.newFloatRange(field,min,max,includeMin,includeMax);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 @Override protected SortField.Type sortType() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 return SortField.Type.FLOAT;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 };
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 public static final FieldParser DOUBLE = new NumberFieldParser() {
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 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
1445
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
78 Double min = minQuery.equals("*") ? null : Double.valueOf(minQuery);
f6075d7a36f2 better range query
Franklin Schmidt <fschmidt@gmail.com>
parents: 1433
diff changeset
79 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
80 return NumericRangeQuery.newDoubleRange(field,min,max,includeMin,includeMax);
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 @Override protected SortField.Type sortType() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 return SortField.Type.DOUBLE;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 };
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }