You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-dev@lucene.apache.org by Yonik Seeley <yo...@apache.org> on 2007/09/05 22:10:31 UTC

boosting a query by a function of other fields

I need to be able to boost a relevancy query by the value of another
field (boost as in multiply, not add in a function query to a boolean
query as dismax currently does).  And instead of a single field, it
will actually be a function of many different fields (so most likely a
function query).

Instead of just hacking up a custom request handler, it seems like
this functionality would be of general use in Solr, so I've started
thinking about it in more general terms.

Issues:
- Solr still uses it's own FunctionQuery instead of what was added
back to Lucene.  May want to migrate first, or may want to hold off if
changes are needed that would be easier to make here.

- I thought about being able to include queries as a ValueSource (so
one could do functions on  relevancy scores).  One issue with this is
that some queries need rewriting to work... so rewrite functionality
would need to be added to ValueSource.

- some field values may be sparse... so if you multiplied the
relevancy query by field "myscore" which only existed on a few
documents, you would not want to multiply the scores for other
documents by 0.  So either
  A) treat 0 as special in the multiply function... it means "not
indexed" and so the multiplier would be 1
  B) remember which values were filled in for a field and provide a
way to get that info (a DocSet?)

- weighting... if a FunctionQuery is at the top level, we need a way
to get back un-weighted scores (actually, looking at the code again,
it looks like as long as the boost is 1, we will get back exact scores
from the FunctionQuery already).

- Interface: seems like SOLR-334 (pluggable query parsers) could help
out here to enable existing handlers to specify a boosted query
without introducing yet more different (hard-coded) HTTP query params.

Thoughts?

-Yonik

Re: boosting a query by a function of other fields

Posted by Will Johnson <wi...@gmail.com>.
> I haven't yet looked at SOLR-192 to see how it is done there, though.
>
> -Mike
>

it no where near perfect but it did at least pass some unit tests.   
my immediate need to have that bit of functionality has lessened but  
i'd be happy to keep working and testing if anyone has comments on  
the patch.  it does (as the ticket states) depend on a lucene patch  
at the moment to get field names etc but that could probably be  
removed if necessary.

- will


Re: boosting a query by a function of other fields

Posted by Mike Klaas <mi...@gmail.com>.
On 6-Sep-07, at 12:55 PM, Yonik Seeley wrote:

> On 9/5/07, Mike Klaas <mi...@gmail.com> wrote:
>>> Issues:
>>> - Solr still uses it's own FunctionQuery instead of what was added
>>> back to Lucene.  May want to migrate first, or may want to hold  
>>> off if
>>> changes are needed that would be easier to make here.
>>
>> I believe that the main thing missing is the various functions (which
>> should be subclasses of CustomScoreQuery).
>
> I could see that for the top-level boost (except for the missing- 
> value problem).
> Were you suggesting that other functions also derive from
> CustomScoreQuery for some reason?

Well, the reason would be that CustomScoreQuery is a mechanism by  
which custom score functions can be introduced, e.g.:

class RecipFloatScoreQuery extends CustomScoreQuery {

     RecipFloatScore(Query source, float m, float a, float b) {
          super(source)
         ...
    }
    public float customScore(int doc, float subQueryScore float  
boostScore) {
         // note: boostScore should never be set
         return a/(m*subQueryScore + b)
    }
}

Of course, it could be a ValueSource too, but the above formulation  
allows the function to apply to any query.  And binary ops are  
trivial to add.

I haven't yet looked at SOLR-192 to see how it is done there, though.

-Mike


Re: boosting a query by a function of other fields

Posted by Yonik Seeley <yo...@apache.org>.
On 9/5/07, Mike Klaas <mi...@gmail.com> wrote:
> > Issues:
> > - Solr still uses it's own FunctionQuery instead of what was added
> > back to Lucene.  May want to migrate first, or may want to hold off if
> > changes are needed that would be easier to make here.
>
> I believe that the main thing missing is the various functions (which
> should be subclasses of CustomScoreQuery).

I could see that for the top-level boost (except for the missing-value problem).
Were you suggesting that other functions also derive from
CustomScoreQuery for some reason?

> > - weighting... if a FunctionQuery is at the top level, we need a way
> > to get back un-weighted scores (actually, looking at the code again,
> > it looks like as long as the boost is 1, we will get back exact scores
> > from the FunctionQuery already).
>
> Why do we need the unweighted score?

More of a customer requirement... the calculated boost also needs to
be obtained separately, so just running the function query alone and
getting the result should work.

> > - Interface: seems like SOLR-334 (pluggable query parsers) could help
> > out here to enable existing handlers to specify a boosted query
> > without introducing yet more different (hard-coded) HTTP query params.
> >
> > Thoughts?
>
> I've implemented exactly this.  see http://issues.apache.org/jira/
> browse/LUCENE-850

Cool, I'll take a look.

-Yonik

> Changed CustomScoreQuery (ValueSource-only) to CustomBoostQuery
> (match/score query + arbitrary multiplicative boost query), and use
> it with dismax (as an arbitrary number of bq.prod parameters) as
> follows:
>
>      // generic product boost queries
>        List<Query> prodBoostQueries = U.parseQueryStrings(req,
> params.getParams(DMP.BQ_PROD));
>        if (null != prodBoostQueries && prodBoostQueries.size() > 0) {
>          Query main = query;
>          Query out = null;
>          for(Query q: prodBoostQueries) {
>            out = new CustomBoostQuery(main, q);
>            main = out;
>          }
>          query = new BooleanQuery(true);
>          query.add(out, Occur.MUST);
>        }
>
> -Mike
>

Re: boosting a query by a function of other fields

Posted by Mike Klaas <mi...@gmail.com>.
On 5-Sep-07, at 1:10 PM, Yonik Seeley wrote:
>

> Issues:
> - Solr still uses it's own FunctionQuery instead of what was added
> back to Lucene.  May want to migrate first, or may want to hold off if
> changes are needed that would be easier to make here.

I believe that the main thing missing is the various functions (which  
should be subclasses of CustomScoreQuery).

> - I thought about being able to include queries as a ValueSource (so
> one could do functions on  relevancy scores).  One issue with this is
> that some queries need rewriting to work... so rewrite functionality
> would need to be added to ValueSource.

ValueSources don't guaranteed that getValue(docid) gets called in  
order, though, and seems to require a value for every doc.  We can  
cheat on this, of course, but when I implemented this I preferred  
skipping it entirely (see below).

> - weighting... if a FunctionQuery is at the top level, we need a way
> to get back un-weighted scores (actually, looking at the code again,
> it looks like as long as the boost is 1, we will get back exact scores
> from the FunctionQuery already).

Why do we need the unweighted score?

> - Interface: seems like SOLR-334 (pluggable query parsers) could help
> out here to enable existing handlers to specify a boosted query
> without introducing yet more different (hard-coded) HTTP query params.
>
> Thoughts?

I've implemented exactly this.  see http://issues.apache.org/jira/ 
browse/LUCENE-850

Changed CustomScoreQuery (ValueSource-only) to CustomBoostQuery  
(match/score query + arbitrary multiplicative boost query), and use  
it with dismax (as an arbitrary number of bq.prod parameters) as  
follows:

     // generic product boost queries
       List<Query> prodBoostQueries = U.parseQueryStrings(req,  
params.getParams(DMP.BQ_PROD));
       if (null != prodBoostQueries && prodBoostQueries.size() > 0) {
         Query main = query;
         Query out = null;
         for(Query q: prodBoostQueries) {
           out = new CustomBoostQuery(main, q);
           main = out;
         }
         query = new BooleanQuery(true);
         query.add(out, Occur.MUST);
       }

-Mike

Re: boosting a query by a function of other fields

Posted by Chris Hostetter <ho...@fucit.org>.
: The main problem being knowing if a doc had a value, right?
: The FieldCache doesn't always store that info (only for string-based types).

yeah ... i remember giving this some thought before ... ideally the 
FieldCache methods (getInts, etc...) would have a way for you to specify 
what value you wanted given to docs without values ... if the 
parsers (IntParser, FloatParser, etc...) were abstract classes instead of 
interfaces it could be added there. but that ship has sailed.

: Or I guess perhaps not even using the FieldCache at all... but the
: memory duplication would be undesirable if a FieldCacheSource or
: FieldCache for the same field was used elsewhere.

or go all out: get LUCENE-831 all polished up, and change the 
FieldValueSource (or it's CustomScoreQuery equivilent) to use it directly 
with a new option that knows when a doc doesn't have a value :)


-Hoss


Re: boosting a query by a function of other fields

Posted by Yonik Seeley <yo...@apache.org>.
On 9/5/07, Chris Hostetter <ho...@fucit.org> wrote:
> : - I thought about being able to include queries as a ValueSource (so
> : one could do functions on  relevancy scores).  One issue with this is
> : that some queries need rewriting to work... so rewrite functionality
> : would need to be added to ValueSource.
>
> not neccessarily ... the getValues(IndexReader) method already sort of
> serves the purpose of rewrite ... a "QueryValueSource" class could
> rewrite the query in it's getValues method and use the resulting
> query/weight/scorer in the DocValues object it returns.

Ahhh, of course.  Thanks!

> : - some field values may be sparse... so if you multiplied the
> : relevancy query by field "myscore" which only existed on a few
> : documents, you would not want to multiply the scores for other
> : documents by 0.  So either
>
> this sems like the kind of thing that could best be dealt with in
> something like FieldValueSource -- where the underlying ValueSource
> getting the field value decides what value to use if the doc doesn't have
> one.

The main problem being knowing if a doc had a value, right?
The FieldCache doesn't always store that info (only for string-based types).
One only has an IndexReader at that point, so it could be hacked
together with a CachingWrapperFilter on a RangeFilter for that field.
Or I guess perhaps not even using the FieldCache at all... but the
memory duplication would be undesirable if a FieldCacheSource or
FieldCache for the same field was used elsewhere.

-Yonik

Re: boosting a query by a function of other fields

Posted by Chris Hostetter <ho...@fucit.org>.
: - Solr still uses it's own FunctionQuery instead of what was added
: back to Lucene.  May want to migrate first, or may want to hold off if
: changes are needed that would be easier to make here.

i haven't had a chacne to look at it yet, but i noticed Jira kick out an 
email recently about a patch being added to SOLR-192 ... probably worth 
reviewing as you consider this.

: - I thought about being able to include queries as a ValueSource (so
: one could do functions on  relevancy scores).  One issue with this is
: that some queries need rewriting to work... so rewrite functionality
: would need to be added to ValueSource.

not neccessarily ... the getValues(IndexReader) method already sort of 
serves the purpose of rewrite ... a "QueryValueSource" class could 
rewrite the query in it's getValues method and use the resulting 
query/weight/scorer in the DocValues object it returns.

: - some field values may be sparse... so if you multiplied the
: relevancy query by field "myscore" which only existed on a few
: documents, you would not want to multiply the scores for other
: documents by 0.  So either

this sems like the kind of thing that could best be dealt with in 
something like FieldValueSource -- where the underlying ValueSource 
getting the field value decides what value to use if the doc doesn't have 
one.





-Hoss