KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > search > analyzer > SearchQueryParser


1 package org.jahia.services.search.analyzer;
2
3 import java.io.IOException JavaDoc;
4 import java.io.StringReader JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
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 /**
20  * Search query string parser that supports appending wildcards to each search
21  * term.
22  *
23  * @author Sergiy Shyrkov
24  */

25 public class SearchQueryParser extends QueryParser
26 {
27
28   private boolean doAutoAppendWildcards;
29
30   /**
31    * Initializes an instance of this class.
32    *
33    * @param f the search query
34    * @param a the analyzer to be used
35    * @param autoAppendWildcards append wildcards to each search term?
36    */

37   public SearchQueryParser(String JavaDoc f, Analyzer a, boolean autoAppendWildcards)
38   {
39     super(f, a);
40     doAutoAppendWildcards = autoAppendWildcards;
41   }
42
43   /*
44    * (non-Javadoc)
45    *
46    * @see org.apache.lucene.queryParser.QueryParser#getFieldQuery(java.lang.String,
47    * org.apache.lucene.analysis.Analyzer, java.lang.String)
48    */

49   protected Query getFieldQuery(String JavaDoc field, Analyzer analyzer,
50     String JavaDoc queryText) throws ParseException
51   {
52     // Use the analyzer to get all the tokens, and then build a TermQuery,
53
// PhraseQuery, or nothing based on the term count
54

55     TokenStream source = analyzer.tokenStream(field,
56       new StringReader JavaDoc(queryText));
57     List JavaDoc v = new ArrayList JavaDoc();
58     Token t;
59
60     while (true)
61     {
62       try
63       {
64         t = source.next();
65       }
66       catch (IOException JavaDoc 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 JavaDoc e)
80     {
81       // ignore
82
}
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 JavaDoc)v.get(0))) : (Query)new TermQuery(new Term(field, (String JavaDoc)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 JavaDoc)v.get(i)));
97       }
98       return q;
99     }
100   }
101
102 }
103
Popular Tags