1 package org.jahia.services.search.analyzer; 2 3 import java.io.IOException ; 4 import java.io.StringReader ; 5 import java.util.ArrayList ; 6 import java.util.List ; 7 8 import org.apache.lucene.analysis.Analyzer; 9 import org.apache.lucene.analysis.Token; 10 import org.apache.lucene.analysis.TokenStream; 11 import org.apache.lucene.index.Term; 12 import org.apache.lucene.queryParser.ParseException; 13 import org.apache.lucene.queryParser.QueryParser; 14 import org.apache.lucene.search.PhraseQuery; 15 import org.apache.lucene.search.PrefixQuery; 16 import org.apache.lucene.search.Query; 17 import org.apache.lucene.search.TermQuery; 18 19 25 public class SearchQueryParser extends QueryParser 26 { 27 28 private boolean doAutoAppendWildcards; 29 30 37 public SearchQueryParser(String f, Analyzer a, boolean autoAppendWildcards) 38 { 39 super(f, a); 40 doAutoAppendWildcards = autoAppendWildcards; 41 } 42 43 49 protected Query getFieldQuery(String field, Analyzer analyzer, 50 String queryText) throws ParseException 51 { 52 55 TokenStream source = analyzer.tokenStream(field, 56 new StringReader (queryText)); 57 List v = new ArrayList (); 58 Token t; 59 60 while (true) 61 { 62 try 63 { 64 t = source.next(); 65 } 66 catch (IOException e) 67 { 68 t = null; 69 } 70 if (t == null) 71 break; 72 73 v.add(t.termText()); 74 } 75 try 76 { 77 source.close(); 78 } 79 catch (IOException e) 80 { 81 } 83 84 if (v.size() == 0) 85 return null; 86 else if (v.size() == 1) 87 return doAutoAppendWildcards ? (Query)new PrefixQuery(new Term(field, 88 (String )v.get(0))) : (Query)new TermQuery(new Term(field, (String )v 89 .get(0))); 90 else 91 { 92 PhraseQuery q = new PhraseQuery(); 93 q.setSlop(getPhraseSlop()); 94 for (int i = 0; i < v.size(); i++) 95 { 96 q.add(new Term(field, (String )v.get(i))); 97 } 98 return q; 99 } 100 } 101 102 } 103 | Popular Tags |