comparison src/goodjava/lucene/queryparser/StringFieldParser.java @ 1619:83caee2ed4ad

Lucene.escape
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 25 Jul 2021 18:50:14 -0600
parents b04b8fc5f4f4
children
comparison
equal deleted inserted replaced
1618:a37ffe2d1b14 1619:83caee2ed4ad
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)));
34 query = escape(qp,query); 34 query = unescape(qp,query);
35 PhraseQuery pq = new PhraseQuery(); 35 PhraseQuery pq = new PhraseQuery();
36 try { 36 try {
37 TokenStream ts = analyzer.tokenStream(field,new StringReader(query)); 37 TokenStream ts = analyzer.tokenStream(field,new StringReader(query));
38 CharTermAttribute termAttr = ts.addAttribute(CharTermAttribute.class); 38 CharTermAttribute termAttr = ts.addAttribute(CharTermAttribute.class);
39 PositionIncrementAttribute posAttr = ts.addAttribute(PositionIncrementAttribute.class); 39 PositionIncrementAttribute posAttr = ts.addAttribute(PositionIncrementAttribute.class);
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(GoodQueryParser 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 : unescape(qp,minQuery);
59 maxQuery = maxQuery.equals("*") ? null : escape(qp,maxQuery); 59 maxQuery = maxQuery.equals("*") ? null : unescape(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(GoodQueryParser qp,String s) throws ParseException { 63 static String unescape(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;