lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Hatcher <>
Subject Re: Query Analyzer
Date Mon, 07 Feb 2005 16:39:23 GMT

On Feb 7, 2005, at 11:29 AM, Ravi wrote:

> How do I set the analyzer when I build the query in my code instead of
> using a query parser ?

You don't.  All terms you use for any Query subclasses you instantiate 
must match exactly the terms in the index.  If you need an analyzer to 
do this then you're responsible for doing it yourself, just as 
QueryParser does underneath.  I do this myself in my current 
application like this:

     private Query createPhraseQuery(String fieldName, String string, 
boolean lowercase) {
         RossettiAnalyzer analyzer = new RossettiAnalyzer(lowercase);
         TokenStream stream = analyzer.tokenStream(fieldName, new 

         PhraseQuery pq = new PhraseQuery();
         Token token;
         try {
           while ((token = != null) {
               pq.add(new Term(fieldName, token.termText()));
         } catch (IOException ignored) {
           // ignore - shouldn't get an IOException on a StringReader

         if (pq.getTerms().length == 1) {
             // optimize single term phrase to TermQuery
             return new TermQuery(pq.getTerms()[0]);

         return pq;

Hope that helps.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message