comparison src/goodjava/lucene/queryparser/StringFieldParser.java @ 1459:b04b8fc5f4f4

GoodQueryParser
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 20 Mar 2020 11:06:53 -0600
parents 6b6c11c9164e
children 83caee2ed4ad
comparison
equal deleted inserted replaced
1458:6b6c11c9164e 1459:b04b8fc5f4f4
23 23
24 public StringFieldParser(Analyzer analyzer) { 24 public StringFieldParser(Analyzer analyzer) {
25 this.analyzer = analyzer; 25 this.analyzer = analyzer;
26 } 26 }
27 27
28 @Override public Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException { 28 @Override public Query getQuery(GoodQueryParser qp,String field,String query) throws ParseException {
29 String wildcard = wildcard(qp,query); 29 String wildcard = wildcard(qp,query);
30 if( wildcard != null ) 30 if( wildcard != null )
31 return new WildcardQuery(new Term(field,wildcard)); 31 return new WildcardQuery(new Term(field,wildcard));
32 if( query.endsWith("*") && !query.endsWith("\\*") ) 32 if( query.endsWith("*") && !query.endsWith("\\*") )
33 return new PrefixQuery(new Term(field,query.substring(0,query.length()-1))); 33 return new PrefixQuery(new Term(field,query.substring(0,query.length()-1)));
52 if( terms.length==1 && pq.getPositions()[0]==0 ) 52 if( terms.length==1 && pq.getPositions()[0]==0 )
53 return new TermQuery(terms[0]); 53 return new TermQuery(terms[0]);
54 return pq; 54 return pq;
55 } 55 }
56 56
57 @Override public Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException { 57 @Override public Query getRangeQuery(GoodQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
58 minQuery = minQuery.equals("*") ? null : escape(qp,minQuery); 58 minQuery = minQuery.equals("*") ? null : escape(qp,minQuery);
59 maxQuery = maxQuery.equals("*") ? null : escape(qp,maxQuery); 59 maxQuery = maxQuery.equals("*") ? null : escape(qp,maxQuery);
60 return TermRangeQuery.newStringRange(field,minQuery,maxQuery,includeMin,includeMax); 60 return TermRangeQuery.newStringRange(field,minQuery,maxQuery,includeMin,includeMax);
61 } 61 }
62 62
63 static String escape(SaneQueryParser qp,String s) throws ParseException { 63 static String escape(GoodQueryParser qp,String s) throws ParseException {
64 final char[] a = s.toCharArray(); 64 final char[] a = s.toCharArray();
65 int i, n; 65 int i, n;
66 if( a[0] == '"' ) { 66 if( a[0] == '"' ) {
67 if( a[a.length-1] != '"' ) throw new RuntimeException(); 67 if( a[a.length-1] != '"' ) throw new RuntimeException();
68 i = 1; 68 i = 1;
82 sb.append(c); 82 sb.append(c);
83 } 83 }
84 return sb.toString(); 84 return sb.toString();
85 } 85 }
86 86
87 private static String wildcard(SaneQueryParser qp,String s) throws ParseException { 87 private static String wildcard(GoodQueryParser qp,String s) throws ParseException {
88 final char[] a = s.toCharArray(); 88 final char[] a = s.toCharArray();
89 if( a[0] == '"' ) 89 if( a[0] == '"' )
90 return null; 90 return null;
91 boolean hasWildcard = false; 91 boolean hasWildcard = false;
92 StringBuilder sb = new StringBuilder(); 92 StringBuilder sb = new StringBuilder();
104 sb.append(c); 104 sb.append(c);
105 } 105 }
106 return hasWildcard ? sb.toString() : null; 106 return hasWildcard ? sb.toString() : null;
107 } 107 }
108 108
109 @Override public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) { 109 @Override public SortField getSortField(GoodQueryParser qp,String field,boolean reverse) {
110 return new SortField( field, SortField.Type.STRING, reverse ); 110 return new SortField( field, SortField.Type.STRING, reverse );
111 } 111 }
112 112
113 } 113 }