lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric Jain <Eric.J...@isb-sib.ch>
Subject Re: search for and condition
Date Tue, 10 Aug 2004 13:01:23 GMT
> String line="text1 text2";
> Query q = MultiFieldQueryParser.parse(line,fields,analyzer);
> Hits hits = searcher.search(q);
> 
> Here search will take "text1 OR text2" Condition
> How  to make searh  "text1 and  text2" Condiation

I recently stumbled over the same issue - the setOperator method is not 
static and will therefore not effect the Query that is created by the 
parse method!

Until someone refactors the code (something along the lines of a 
QueryParserFactory might make sense), here is a workaround:

public Query parse(String query, String[] fields, Analyzer analyzer)
{
   BooleanQuery bQuery = new BooleanQuery();
   for (int i = 0; i < fields.length; i++)
   {
     QueryParser parser = new QueryParser(fields[i], analyzer);
     parser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
     Query q = parser.parse(query);
     bQuery.add(q, false, false);
   }
   return bQuery;
}

---------------------------------------------------------------------
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