lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ravi" <r...@htinc.com>
Subject RE: Query Analyzer
Date Mon, 07 Feb 2005 17:46:46 GMT
That worked. Thanks a lot. 

-----Original Message-----
From: Erik Hatcher [mailto:erik@ehatchersolutions.com] 
Sent: Monday, February 07, 2005 11:39 AM
To: Lucene Users List
Subject: Re: Query Analyzer


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 
StringReader(string));

         PhraseQuery pq = new PhraseQuery();
         Token token;
         try {
           while ((token = stream.next()) != 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.

	Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Mime
View raw message