When you have a category, add the pair of clauses as a sub-Boolean query.
Something like... try { PhraseQuery textQuery = new PhraseQuery(); PhraseQuery titleQuery = new PhraseQuery(); PhraseQuery catQuery = new PhraseQuery(); textQuery.setSlop( 20 ); titleQuery.setSlop( 4 ); bQueryPair = new BooleanQuery(); bQueryAll = new BooleanQuery(); for( int k = 0; k < phrase.length; k++ ) { textQuery.add( new Term( NAME, phrase[k] ) ); titleQuery.add( new Term( REVIEW, phrase[k] ) ); } bQueryPair.add( textQuery, BooleanClause.Occur.SHOULD ); bQueryPair.add( titleQuery, BooleanClause.Occur.SHOULD ); if(category!=null && !category.equals("")){ catQuery.add( new Term( TYPE, category ) ); bQueryAll.add(catQuery,BooleanClause.Occur.MUST); bQueryAll.add(bQueryPair, BooleanCluase.Occur.MUST) } else { bQueryAll = bQueryPair; } } catch( Exception e ) { throw new RuntimeException( "Unable to make any sense of the query.", e ); } and use bQueryAll in your query. And please be waaaay more elegant than the code I wrote <G>. Erick On 2/28/07, Ismail Siddiqui <[EMAIL PROTECTED]> wrote:
Hey guys, I want to filter a result set on a particular field..I have code like this try { PhraseQuery textQuery = new PhraseQuery(); PhraseQuery titleQuery = new PhraseQuery(); PhraseQuery catQuery = new PhraseQuery(); textQuery.setSlop( 20 ); titleQuery.setSlop( 4 ); for( int k = 0; k < phrase.length; k++ ) { textQuery.add( new Term( NAME, phrase[k] ) ); titleQuery.add( new Term( REVIEW, phrase[k] ) ); } bQuery.add( textQuery, BooleanClause.Occur.SHOULD ); bQuery.add( titleQuery, BooleanClause.Occur.SHOULD ); if(category!=null && !category.equals("")){ catQuery.add( new Term( TYPE, category ) ); bQuery.add(catQuery,BooleanClause.Occur.MUST); } } catch( Exception e ) { throw new RuntimeException( "Unable to make any sense of the query.", e ); } Now the problem is its getting all results for a particular category regardless the "phrase" is in the title or text field which make sense as the other two have SHOULD clause. the problem is I can not set a MUST clause on the other two field as I need to match either one of the field. so what i want to is either title or text MUST have it and if category is not null it MUST have the category string passed. any ideas