You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Ismail Siddiqui <is...@gmail.com> on 2007/02/28 17:53:11 UTC
Filtering results on a Field
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
Re: Filtering results on a Field
Posted by Ismail Siddiqui <is...@gmail.com>.
thanks a lot
On 2/28/07, Erick Erickson <er...@gmail.com> wrote:
>
> 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 <is...@gmail.com> 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
> >
>
Re: Filtering results on a Field
Posted by Erick Erickson <er...@gmail.com>.
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 <is...@gmail.com> 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
>