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 Philippe <ma...@gmail.com> on 2010/07/31 10:04:27 UTC

Rank results only on some fields

Hi,

I want to rank my results only on parts of my query.  E.g my query is 
"TITLE:Lucene AND AUTHOR:Manning". After this query standard lucene 
ranking for both fields take place.

However, is it possible to query the index using the full query and rank 
results only according to the "TITLE"-Field?

Regards,
     Philippe

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


Re: Rank results only on some fields

Posted by Naama Kraus <na...@gmail.com>.
Wouldn't boosting the term AUTHOR:Manning with a boost factor of 0 do the
trick ?
Naama


On Sat, Jul 31, 2010 at 11:04 AM, Philippe <ma...@gmail.com> wrote:

> Hi,
>
> I want to rank my results only on parts of my query.  E.g my query is
> "TITLE:Lucene AND AUTHOR:Manning". After this query standard lucene ranking
> for both fields take place.
>
> However, is it possible to query the index using the full query and rank
> results only according to the "TITLE"-Field?
>
> Regards,
>    Philippe
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

Re: Rank results only on some fields

Posted by Lance Norskog <go...@gmail.com>.
Oops, didn't notice that this was java-user. I had a Solr 'why write
more code' reaction ?)

On Sat, Jul 31, 2010 at 1:56 PM, Uwe Schindler <uw...@thetaphi.de> wrote:
> We don't want to modify the ranking using functions, we want to switch some queries to constant score mode. The QueryParser subclassing is just to make it convenient.
>
> In general to strip off scores from queries, you use "new ConstantScoreQuery(new QueryWrapperFilter(query))", this is used inside Lucene, too (MultiTermQuery,...). The trick is to normalize the Scorer to return a constant value (boost of CSQ). This can be done by first wrapping the scorer of the original query in a filter and then add a scorer to the filter again, that returns a constant.
>
> With function queries you can do something similar by returning a constant in the CustomScoreProvider. The QWF/CSQ trick is more convenient and used quite often inside Lucene, too.
>
> -----
> Uwe Schindler
> H.-H.-Meier-Allee 63, D-28213 Bremen
> http://www.thetaphi.de
> eMail: uwe@thetaphi.de
>
>
>> -----Original Message-----
>> From: Lance Norskog [mailto:goksron@gmail.com]
>> Sent: Saturday, July 31, 2010 10:50 PM
>> To: java-user@lucene.apache.org
>> Subject: Re: Rank results only on some fields
>>
>> Can't this use case be done with a function query?
>>
>> On Sat, Jul 31, 2010 at 1:59 AM, Uwe Schindler <uw...@thetaphi.de> wrote:
>> > Here some example code, the method is getFieldQuery() (Lucene 2.9 or
>> > 3.0 or following, don't use that approach before, because
>> > QueryWrapperFilter is not effective before 2.9 for that):
>> >
>> > @Override
>> > protected Query getFieldQuery(String field, String queryText)  throws
>> > ParseException {
>> >        Query q = super.getFieldQuery(field,queryText);
>> >        if (!"TITLE".equals(field))
>> >                q = new ConstantScoreQuery(new QueryWrapperFilter(q));
>> >        return q;
>> > }
>> >
>> > I hope that explains itself. You may look at other Query type
>> > factories in QP that produce scoring queries and wrap them similar.
>> > But e.g. WildCard and RangeQueries are constant score. Phrases are also
>> handled by this method.
>> > Only the slop setting may not work correctly after this (look at the
>> > instanceof checks in getFieldQuery(..., slop)).
>> >
>> > Uwe
>> >
>> > -----
>> > Uwe Schindler
>> > H.-H.-Meier-Allee 63, D-28213 Bremen
>> > http://www.thetaphi.de
>> > eMail: uwe@thetaphi.de
>> >
>> >
>> >> -----Original Message-----
>> >> From: Uwe Schindler [mailto:uwe@thetaphi.de]
>> >> Sent: Saturday, July 31, 2010 10:19 AM
>> >> To: java-user@lucene.apache.org
>> >> Subject: RE: Rank results only on some fields
>> >>
>> >> You can construct the query using a customized query parser that
>> >> wraps all queries not with the suggested field name using a "new
>> >> ConstantScoreQuery(new QueryWrapperFilter(originalCreatedQuery))".
>> >> Override
>> >> newFieldQuery() to do that and pass the super call to this ctor chain.
>> >>
>> >> Uwe
>> >>
>> >> -----
>> >> Uwe Schindler
>> >> H.-H.-Meier-Allee 63, D-28213 Bremen
>> >> http://www.thetaphi.de
>> >> eMail: uwe@thetaphi.de
>> >>
>> >>
>> >> > -----Original Message-----
>> >> > From: Philippe [mailto:mailer.thomas@gmail.com]
>> >> > Sent: Saturday, July 31, 2010 10:04 AM
>> >> > To: java-user@lucene.apache.org
>> >> > Subject: Rank results only on some fields
>> >> >
>> >> > Hi,
>> >> >
>> >> > I want to rank my results only on parts of my query.  E.g my query
>> >> > is "TITLE:Lucene AND AUTHOR:Manning". After this query standard
>> >> > lucene ranking for both fields take place.
>> >> >
>> >> > However, is it possible to query the index using the full query and
>> >> > rank results only according to the "TITLE"-Field?
>> >> >
>> >> > Regards,
>> >> >      Philippe
>> >> >
>> >> > -------------------------------------------------------------------
>> >> > -- To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> >> > For additional commands, e-mail: java-user-help@lucene.apache.org
>> >>
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> >> For additional commands, e-mail: java-user-help@lucene.apache.org
>> >
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> > For additional commands, e-mail: java-user-help@lucene.apache.org
>> >
>> >
>>
>>
>>
>> --
>> Lance Norskog
>> goksron@gmail.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>



-- 
Lance Norskog
goksron@gmail.com

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


RE: Rank results only on some fields

Posted by Uwe Schindler <uw...@thetaphi.de>.
We don't want to modify the ranking using functions, we want to switch some queries to constant score mode. The QueryParser subclassing is just to make it convenient.

In general to strip off scores from queries, you use "new ConstantScoreQuery(new QueryWrapperFilter(query))", this is used inside Lucene, too (MultiTermQuery,...). The trick is to normalize the Scorer to return a constant value (boost of CSQ). This can be done by first wrapping the scorer of the original query in a filter and then add a scorer to the filter again, that returns a constant.

With function queries you can do something similar by returning a constant in the CustomScoreProvider. The QWF/CSQ trick is more convenient and used quite often inside Lucene, too.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Lance Norskog [mailto:goksron@gmail.com]
> Sent: Saturday, July 31, 2010 10:50 PM
> To: java-user@lucene.apache.org
> Subject: Re: Rank results only on some fields
> 
> Can't this use case be done with a function query?
> 
> On Sat, Jul 31, 2010 at 1:59 AM, Uwe Schindler <uw...@thetaphi.de> wrote:
> > Here some example code, the method is getFieldQuery() (Lucene 2.9 or
> > 3.0 or following, don't use that approach before, because
> > QueryWrapperFilter is not effective before 2.9 for that):
> >
> > @Override
> > protected Query getFieldQuery(String field, String queryText)  throws
> > ParseException {
> >        Query q = super.getFieldQuery(field,queryText);
> >        if (!"TITLE".equals(field))
> >                q = new ConstantScoreQuery(new QueryWrapperFilter(q));
> >        return q;
> > }
> >
> > I hope that explains itself. You may look at other Query type
> > factories in QP that produce scoring queries and wrap them similar.
> > But e.g. WildCard and RangeQueries are constant score. Phrases are also
> handled by this method.
> > Only the slop setting may not work correctly after this (look at the
> > instanceof checks in getFieldQuery(..., slop)).
> >
> > Uwe
> >
> > -----
> > Uwe Schindler
> > H.-H.-Meier-Allee 63, D-28213 Bremen
> > http://www.thetaphi.de
> > eMail: uwe@thetaphi.de
> >
> >
> >> -----Original Message-----
> >> From: Uwe Schindler [mailto:uwe@thetaphi.de]
> >> Sent: Saturday, July 31, 2010 10:19 AM
> >> To: java-user@lucene.apache.org
> >> Subject: RE: Rank results only on some fields
> >>
> >> You can construct the query using a customized query parser that
> >> wraps all queries not with the suggested field name using a "new
> >> ConstantScoreQuery(new QueryWrapperFilter(originalCreatedQuery))".
> >> Override
> >> newFieldQuery() to do that and pass the super call to this ctor chain.
> >>
> >> Uwe
> >>
> >> -----
> >> Uwe Schindler
> >> H.-H.-Meier-Allee 63, D-28213 Bremen
> >> http://www.thetaphi.de
> >> eMail: uwe@thetaphi.de
> >>
> >>
> >> > -----Original Message-----
> >> > From: Philippe [mailto:mailer.thomas@gmail.com]
> >> > Sent: Saturday, July 31, 2010 10:04 AM
> >> > To: java-user@lucene.apache.org
> >> > Subject: Rank results only on some fields
> >> >
> >> > Hi,
> >> >
> >> > I want to rank my results only on parts of my query.  E.g my query
> >> > is "TITLE:Lucene AND AUTHOR:Manning". After this query standard
> >> > lucene ranking for both fields take place.
> >> >
> >> > However, is it possible to query the index using the full query and
> >> > rank results only according to the "TITLE"-Field?
> >> >
> >> > Regards,
> >> >      Philippe
> >> >
> >> > -------------------------------------------------------------------
> >> > -- To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> >> > For additional commands, e-mail: java-user-help@lucene.apache.org
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> >> For additional commands, e-mail: java-user-help@lucene.apache.org
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> > For additional commands, e-mail: java-user-help@lucene.apache.org
> >
> >
> 
> 
> 
> --
> Lance Norskog
> goksron@gmail.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org



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


Re: Rank results only on some fields

Posted by Lance Norskog <go...@gmail.com>.
Can't this use case be done with a function query?

On Sat, Jul 31, 2010 at 1:59 AM, Uwe Schindler <uw...@thetaphi.de> wrote:
> Here some example code, the method is getFieldQuery() (Lucene 2.9 or 3.0 or
> following, don't use that approach before, because QueryWrapperFilter is not
> effective before 2.9 for that):
>
> @Override
> protected Query getFieldQuery(String field, String queryText)  throws
> ParseException {
>        Query q = super.getFieldQuery(field,queryText);
>        if (!"TITLE".equals(field))
>                q = new ConstantScoreQuery(new QueryWrapperFilter(q));
>        return q;
> }
>
> I hope that explains itself. You may look at other Query type factories in
> QP that produce scoring queries and wrap them similar. But e.g. WildCard and
> RangeQueries are constant score. Phrases are also handled by this method.
> Only the slop setting may not work correctly after this (look at the
> instanceof checks in getFieldQuery(..., slop)).
>
> Uwe
>
> -----
> Uwe Schindler
> H.-H.-Meier-Allee 63, D-28213 Bremen
> http://www.thetaphi.de
> eMail: uwe@thetaphi.de
>
>
>> -----Original Message-----
>> From: Uwe Schindler [mailto:uwe@thetaphi.de]
>> Sent: Saturday, July 31, 2010 10:19 AM
>> To: java-user@lucene.apache.org
>> Subject: RE: Rank results only on some fields
>>
>> You can construct the query using a customized query parser that wraps all
>> queries not with the suggested field name using a "new
>> ConstantScoreQuery(new QueryWrapperFilter(originalCreatedQuery))".
>> Override
>> newFieldQuery() to do that and pass the super call to this ctor chain.
>>
>> Uwe
>>
>> -----
>> Uwe Schindler
>> H.-H.-Meier-Allee 63, D-28213 Bremen
>> http://www.thetaphi.de
>> eMail: uwe@thetaphi.de
>>
>>
>> > -----Original Message-----
>> > From: Philippe [mailto:mailer.thomas@gmail.com]
>> > Sent: Saturday, July 31, 2010 10:04 AM
>> > To: java-user@lucene.apache.org
>> > Subject: Rank results only on some fields
>> >
>> > Hi,
>> >
>> > I want to rank my results only on parts of my query.  E.g my query is
>> > "TITLE:Lucene AND AUTHOR:Manning". After this query standard lucene
>> > ranking for both fields take place.
>> >
>> > However, is it possible to query the index using the full query and
>> > rank results only according to the "TITLE"-Field?
>> >
>> > Regards,
>> >      Philippe
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> > For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>



-- 
Lance Norskog
goksron@gmail.com

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


RE: Rank results only on some fields

Posted by Uwe Schindler <uw...@thetaphi.de>.
Here some example code, the method is getFieldQuery() (Lucene 2.9 or 3.0 or
following, don't use that approach before, because QueryWrapperFilter is not
effective before 2.9 for that):

@Override
protected Query getFieldQuery(String field, String queryText)  throws
ParseException {
	Query q = super.getFieldQuery(field,queryText);
	if (!"TITLE".equals(field))
		q = new ConstantScoreQuery(new QueryWrapperFilter(q));
	return q;
}

I hope that explains itself. You may look at other Query type factories in
QP that produce scoring queries and wrap them similar. But e.g. WildCard and
RangeQueries are constant score. Phrases are also handled by this method.
Only the slop setting may not work correctly after this (look at the
instanceof checks in getFieldQuery(..., slop)).

Uwe

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Uwe Schindler [mailto:uwe@thetaphi.de]
> Sent: Saturday, July 31, 2010 10:19 AM
> To: java-user@lucene.apache.org
> Subject: RE: Rank results only on some fields
> 
> You can construct the query using a customized query parser that wraps all
> queries not with the suggested field name using a "new
> ConstantScoreQuery(new QueryWrapperFilter(originalCreatedQuery))".
> Override
> newFieldQuery() to do that and pass the super call to this ctor chain.
> 
> Uwe
> 
> -----
> Uwe Schindler
> H.-H.-Meier-Allee 63, D-28213 Bremen
> http://www.thetaphi.de
> eMail: uwe@thetaphi.de
> 
> 
> > -----Original Message-----
> > From: Philippe [mailto:mailer.thomas@gmail.com]
> > Sent: Saturday, July 31, 2010 10:04 AM
> > To: java-user@lucene.apache.org
> > Subject: Rank results only on some fields
> >
> > Hi,
> >
> > I want to rank my results only on parts of my query.  E.g my query is
> > "TITLE:Lucene AND AUTHOR:Manning". After this query standard lucene
> > ranking for both fields take place.
> >
> > However, is it possible to query the index using the full query and
> > rank results only according to the "TITLE"-Field?
> >
> > Regards,
> >      Philippe
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> > For additional commands, e-mail: java-user-help@lucene.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org



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


RE: Rank results only on some fields

Posted by Uwe Schindler <uw...@thetaphi.de>.
You can construct the query using a customized query parser that wraps all
queries not with the suggested field name using a "new
ConstantScoreQuery(new QueryWrapperFilter(originalCreatedQuery))". Override
newFieldQuery() to do that and pass the super call to this ctor chain.

Uwe

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Philippe [mailto:mailer.thomas@gmail.com]
> Sent: Saturday, July 31, 2010 10:04 AM
> To: java-user@lucene.apache.org
> Subject: Rank results only on some fields
> 
> Hi,
> 
> I want to rank my results only on parts of my query.  E.g my query is
> "TITLE:Lucene AND AUTHOR:Manning". After this query standard lucene
> ranking for both fields take place.
> 
> However, is it possible to query the index using the full query and rank
> results only according to the "TITLE"-Field?
> 
> Regards,
>      Philippe
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org



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