Mercurial Hosting > luan
comparison src/goodjava/lucene/queryparser/NumberFieldParser.java @ 1771:c62324841dfb
improve NumberFieldParser and add key map lucene query
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 25 Jun 2023 15:05:09 -0600 |
parents | b04b8fc5f4f4 |
children |
comparison
equal
deleted
inserted
replaced
1770:6c01d54edcac | 1771:c62324841dfb |
---|---|
2 | 2 |
3 import org.apache.lucene.search.Query; | 3 import org.apache.lucene.search.Query; |
4 import org.apache.lucene.search.NumericRangeQuery; | 4 import org.apache.lucene.search.NumericRangeQuery; |
5 import org.apache.lucene.search.SortField; | 5 import org.apache.lucene.search.SortField; |
6 import org.apache.lucene.search.PrefixQuery; | 6 import org.apache.lucene.search.PrefixQuery; |
7 import org.apache.lucene.search.TermQuery; | |
7 import org.apache.lucene.index.Term; | 8 import org.apache.lucene.index.Term; |
9 import org.apache.lucene.util.BytesRef; | |
8 import goodjava.parser.ParseException; | 10 import goodjava.parser.ParseException; |
11 import org.apache.lucene.util.NumericUtils; | |
9 | 12 |
10 | 13 |
11 public abstract class NumberFieldParser implements FieldParser { | 14 public abstract class NumberFieldParser implements FieldParser { |
12 | 15 |
13 @Override public final Query getQuery(GoodQueryParser qp,String field,String query) throws ParseException { | 16 @Override public final Query getQuery(GoodQueryParser qp,String field,String query) throws ParseException { |
14 if( query.equals("*") ) | 17 if( query.equals("*") ) |
15 return new PrefixQuery(new Term(field,"")); | 18 return new PrefixQuery(new Term(field,"")); |
16 return getRangeQuery(qp,field,query,query,true,true); | 19 try { |
20 return getQuery(field,query); | |
21 } catch(NumberFormatException e) { | |
22 throw qp.exception(e); | |
23 } | |
24 } | |
25 | |
26 protected Query getQuery(String field,String query) { | |
27 return getRangeQuery(field,query,query,true,true); | |
17 } | 28 } |
18 | 29 |
19 @Override public final Query getRangeQuery(GoodQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException { | 30 @Override public final Query getRangeQuery(GoodQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException { |
20 try { | 31 try { |
21 return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax); | 32 return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax); |
33 abstract protected SortField.Type sortType(); | 44 abstract protected SortField.Type sortType(); |
34 | 45 |
35 | 46 |
36 public static final FieldParser INT = new NumberFieldParser() { | 47 public static final FieldParser INT = new NumberFieldParser() { |
37 | 48 |
49 @Override protected Query getQuery(String field,String query) { | |
50 int n = Integer.valueOf(query); | |
51 return new TermQuery( term(field,n) ); | |
52 } | |
53 | |
38 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { | 54 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { |
39 Integer min = minQuery.equals("*") ? null : Integer.valueOf(minQuery); | 55 Integer min = minQuery.equals("*") ? null : Integer.valueOf(minQuery); |
40 Integer max = maxQuery.equals("*") ? null : Integer.valueOf(maxQuery); | 56 Integer max = maxQuery.equals("*") ? null : Integer.valueOf(maxQuery); |
41 return NumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax); | 57 return NumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax); |
42 } | 58 } |
45 return SortField.Type.INT; | 61 return SortField.Type.INT; |
46 } | 62 } |
47 }; | 63 }; |
48 | 64 |
49 public static final FieldParser LONG = new NumberFieldParser() { | 65 public static final FieldParser LONG = new NumberFieldParser() { |
66 | |
67 @Override protected Query getQuery(String field,String query) { | |
68 long n = Long.valueOf(query); | |
69 return new TermQuery( term(field,n) ); | |
70 } | |
50 | 71 |
51 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { | 72 @Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) { |
52 Long min = minQuery.equals("*") ? null : Long.valueOf(minQuery); | 73 Long min = minQuery.equals("*") ? null : Long.valueOf(minQuery); |
53 Long max = maxQuery.equals("*") ? null : Long.valueOf(maxQuery); | 74 Long max = maxQuery.equals("*") ? null : Long.valueOf(maxQuery); |
54 return NumericRangeQuery.newLongRange(field,min,max,includeMin,includeMax); | 75 return NumericRangeQuery.newLongRange(field,min,max,includeMin,includeMax); |
83 @Override protected SortField.Type sortType() { | 104 @Override protected SortField.Type sortType() { |
84 return SortField.Type.DOUBLE; | 105 return SortField.Type.DOUBLE; |
85 } | 106 } |
86 }; | 107 }; |
87 | 108 |
109 public static Term term(String name,int value) { | |
110 BytesRef br = new BytesRef(); | |
111 NumericUtils.intToPrefixCoded(value,0,br); | |
112 return new Term(name,br); | |
113 } | |
114 | |
115 public static Term term(String name,long value) { | |
116 BytesRef br = new BytesRef(); | |
117 NumericUtils.longToPrefixCoded(value,0,br); | |
118 return new Term(name,br); | |
119 } | |
120 | |
88 } | 121 } |