That worked. Thanks a lot. -----Original Message----- From: Erik Hatcher [mailto:[EMAIL PROTECTED] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]