You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Lucas Cotta <lu...@gmail.com> on 2016/10/03 22:49:07 UTC

How to implement a custom boost function

Hello,

I'm new in Solr (4.7.2) and I was given the following requirement:

Given a query such as:

studentId:(875141 OR 873071 OR 875198 OR 108142 OR 918841 OR 870688 OR
107920 OR 870637 OR 870636 OR 870635 OR 918792 OR 107721 OR 875078 OR
875166 OR 875151 OR 918829 OR 918808)

I want the results to be ordered by the same order the elements were
informed in the query. This would be similar to MySQL's ORDER BY FIELD(id,
3,2,5,7,8,1).

I have tried to use term boosting
<https://wiki.apache.org/solr/SolrRelevancyCookbook#Boosting_Ranking_Terms>
in the query but that only works when I use big factors like this:
875078^100000
OR 875166^10000 OR 875151^1000 OR 918829^100OR 918808^10

But that would cause the query to be too big in case I have 200 ids for
instance.

So it seems I need to implement a custom FunctionQuery.
I'm a little lost on how to do that. Could someone please give me an idea?
Which classes should my custom class extend from? Where should I place this
class? Should I add to Solr project it self and regenerate the JAR?

Thanks

Re: How to implement a custom boost function

Posted by Lucas Cotta <lu...@gmail.com>.
It seems I could also use constant score... but this was introduced on
version 5.1... anyway I could do something similar in 4.7.1?

Thanks

2016-10-04 11:24 GMT-03:00 Lucas Cotta <lu...@gmail.com>:

> Sorry Bill, I didn't understand your suggestion. Could I implement a sort
> function in this case?
> Thanks
>
> 2016-10-04 11:12 GMT-03:00 <bi...@gmail.com>:
>
>> You can pahinaye and sort as long as it is the same each time. Sort can
>> be a function value too. I.e. Sort=geodist() asc...
>>
>> bq can also boost based on a field name
>>
>>
>>
>> Bill Bell
>> Sent from mobile
>>
>>
>> > On Oct 3, 2016, at 11:28 PM, Lucas Cotta <lu...@gmail.com> wrote:
>> >
>> > Hi Walter, unfortunately I use pagination so that would not be
>> possible..
>> >
>> > Thanks
>> >
>> > 2016-10-04 0:51 GMT-03:00 Walter Underwood <wu...@wunderwood.org>:
>> >
>> >> How about sorting them after you get them back from Solr?
>> >>
>> >> wunder
>> >> Walter Underwood
>> >> wunder@wunderwood.org
>> >> http://observer.wunderwood.org/  (my blog)
>> >>
>> >>
>> >>> On Oct 3, 2016, at 6:45 PM, Lucas Cotta <lu...@gmail.com> wrote:
>> >>>
>> >>> I actually could also use a custom similarity class that always
>> returns
>> >> 1.0
>> >>> then I could use small boost factors such as ^1, ^2, ^3, etc.
>> >>>
>> >>> But I want to do this only in some specific queries (that may contain
>> >> other
>> >>> fields besides studentId)
>> >>>
>> >>> How could I do this, use the custom similarity class only for some
>> >> queries?
>> >>> Is it possible?
>> >>>
>> >>> Thanks!
>> >>>
>> >>> 2016-10-03 19:49 GMT-03:00 Lucas Cotta <lu...@gmail.com>:
>> >>>
>> >>>> Hello,
>> >>>>
>> >>>> I'm new in Solr (4.7.2) and I was given the following requirement:
>> >>>>
>> >>>> Given a query such as:
>> >>>>
>> >>>> studentId:(875141 OR 873071 OR 875198 OR 108142 OR 918841 OR 870688
>> OR
>> >>>> 107920 OR 870637 OR 870636 OR 870635 OR 918792 OR 107721 OR 875078 OR
>> >>>> 875166 OR 875151 OR 918829 OR 918808)
>> >>>>
>> >>>> I want the results to be ordered by the same order the elements were
>> >>>> informed in the query. This would be similar to MySQL's ORDER BY
>> >>>> FIELD(id, 3,2,5,7,8,1).
>> >>>>
>> >>>> I have tried to use term boosting
>> >>>> <https://wiki.apache.org/solr/SolrRelevancyCookbook#
>> >> Boosting_Ranking_Terms>
>> >>>> in the query but that only works when I use big factors like this:
>> >> 875078^100000
>> >>>> OR 875166^10000 OR 875151^1000 OR 918829^100OR 918808^10
>> >>>>
>> >>>> But that would cause the query to be too big in case I have 200 ids
>> for
>> >>>> instance.
>> >>>>
>> >>>> So it seems I need to implement a custom FunctionQuery.
>> >>>> I'm a little lost on how to do that. Could someone please give me an
>> >> idea?
>> >>>> Which classes should my custom class extend from? Where should I
>> place
>> >> this
>> >>>> class? Should I add to Solr project it self and regenerate the JAR?
>> >>>>
>> >>>> Thanks
>> >>>>
>> >>
>> >>
>>
>
>

Re: How to implement a custom boost function

Posted by Lucas Cotta <lu...@gmail.com>.
Sorry Bill, I didn't understand your suggestion. Could I implement a sort
function in this case?
Thanks

2016-10-04 11:12 GMT-03:00 <bi...@gmail.com>:

> You can pahinaye and sort as long as it is the same each time. Sort can be
> a function value too. I.e. Sort=geodist() asc...
>
> bq can also boost based on a field name
>
>
>
> Bill Bell
> Sent from mobile
>
>
> > On Oct 3, 2016, at 11:28 PM, Lucas Cotta <lu...@gmail.com> wrote:
> >
> > Hi Walter, unfortunately I use pagination so that would not be possible..
> >
> > Thanks
> >
> > 2016-10-04 0:51 GMT-03:00 Walter Underwood <wu...@wunderwood.org>:
> >
> >> How about sorting them after you get them back from Solr?
> >>
> >> wunder
> >> Walter Underwood
> >> wunder@wunderwood.org
> >> http://observer.wunderwood.org/  (my blog)
> >>
> >>
> >>> On Oct 3, 2016, at 6:45 PM, Lucas Cotta <lu...@gmail.com> wrote:
> >>>
> >>> I actually could also use a custom similarity class that always returns
> >> 1.0
> >>> then I could use small boost factors such as ^1, ^2, ^3, etc.
> >>>
> >>> But I want to do this only in some specific queries (that may contain
> >> other
> >>> fields besides studentId)
> >>>
> >>> How could I do this, use the custom similarity class only for some
> >> queries?
> >>> Is it possible?
> >>>
> >>> Thanks!
> >>>
> >>> 2016-10-03 19:49 GMT-03:00 Lucas Cotta <lu...@gmail.com>:
> >>>
> >>>> Hello,
> >>>>
> >>>> I'm new in Solr (4.7.2) and I was given the following requirement:
> >>>>
> >>>> Given a query such as:
> >>>>
> >>>> studentId:(875141 OR 873071 OR 875198 OR 108142 OR 918841 OR 870688 OR
> >>>> 107920 OR 870637 OR 870636 OR 870635 OR 918792 OR 107721 OR 875078 OR
> >>>> 875166 OR 875151 OR 918829 OR 918808)
> >>>>
> >>>> I want the results to be ordered by the same order the elements were
> >>>> informed in the query. This would be similar to MySQL's ORDER BY
> >>>> FIELD(id, 3,2,5,7,8,1).
> >>>>
> >>>> I have tried to use term boosting
> >>>> <https://wiki.apache.org/solr/SolrRelevancyCookbook#
> >> Boosting_Ranking_Terms>
> >>>> in the query but that only works when I use big factors like this:
> >> 875078^100000
> >>>> OR 875166^10000 OR 875151^1000 OR 918829^100OR 918808^10
> >>>>
> >>>> But that would cause the query to be too big in case I have 200 ids
> for
> >>>> instance.
> >>>>
> >>>> So it seems I need to implement a custom FunctionQuery.
> >>>> I'm a little lost on how to do that. Could someone please give me an
> >> idea?
> >>>> Which classes should my custom class extend from? Where should I place
> >> this
> >>>> class? Should I add to Solr project it self and regenerate the JAR?
> >>>>
> >>>> Thanks
> >>>>
> >>
> >>
>

Re: How to implement a custom boost function

Posted by bi...@gmail.com.
You can pahinaye and sort as long as it is the same each time. Sort can be a function value too. I.e. Sort=geodist() asc...

bq can also boost based on a field name 



Bill Bell
Sent from mobile


> On Oct 3, 2016, at 11:28 PM, Lucas Cotta <lu...@gmail.com> wrote:
> 
> Hi Walter, unfortunately I use pagination so that would not be possible..
> 
> Thanks
> 
> 2016-10-04 0:51 GMT-03:00 Walter Underwood <wu...@wunderwood.org>:
> 
>> How about sorting them after you get them back from Solr?
>> 
>> wunder
>> Walter Underwood
>> wunder@wunderwood.org
>> http://observer.wunderwood.org/  (my blog)
>> 
>> 
>>> On Oct 3, 2016, at 6:45 PM, Lucas Cotta <lu...@gmail.com> wrote:
>>> 
>>> I actually could also use a custom similarity class that always returns
>> 1.0
>>> then I could use small boost factors such as ^1, ^2, ^3, etc.
>>> 
>>> But I want to do this only in some specific queries (that may contain
>> other
>>> fields besides studentId)
>>> 
>>> How could I do this, use the custom similarity class only for some
>> queries?
>>> Is it possible?
>>> 
>>> Thanks!
>>> 
>>> 2016-10-03 19:49 GMT-03:00 Lucas Cotta <lu...@gmail.com>:
>>> 
>>>> Hello,
>>>> 
>>>> I'm new in Solr (4.7.2) and I was given the following requirement:
>>>> 
>>>> Given a query such as:
>>>> 
>>>> studentId:(875141 OR 873071 OR 875198 OR 108142 OR 918841 OR 870688 OR
>>>> 107920 OR 870637 OR 870636 OR 870635 OR 918792 OR 107721 OR 875078 OR
>>>> 875166 OR 875151 OR 918829 OR 918808)
>>>> 
>>>> I want the results to be ordered by the same order the elements were
>>>> informed in the query. This would be similar to MySQL's ORDER BY
>>>> FIELD(id, 3,2,5,7,8,1).
>>>> 
>>>> I have tried to use term boosting
>>>> <https://wiki.apache.org/solr/SolrRelevancyCookbook#
>> Boosting_Ranking_Terms>
>>>> in the query but that only works when I use big factors like this:
>> 875078^100000
>>>> OR 875166^10000 OR 875151^1000 OR 918829^100OR 918808^10
>>>> 
>>>> But that would cause the query to be too big in case I have 200 ids for
>>>> instance.
>>>> 
>>>> So it seems I need to implement a custom FunctionQuery.
>>>> I'm a little lost on how to do that. Could someone please give me an
>> idea?
>>>> Which classes should my custom class extend from? Where should I place
>> this
>>>> class? Should I add to Solr project it self and regenerate the JAR?
>>>> 
>>>> Thanks
>>>> 
>> 
>> 

Re: How to implement a custom boost function

Posted by Lucas Cotta <lu...@gmail.com>.
Hi Walter, unfortunately I use pagination so that would not be possible..

Thanks

2016-10-04 0:51 GMT-03:00 Walter Underwood <wu...@wunderwood.org>:

> How about sorting them after you get them back from Solr?
>
> wunder
> Walter Underwood
> wunder@wunderwood.org
> http://observer.wunderwood.org/  (my blog)
>
>
> > On Oct 3, 2016, at 6:45 PM, Lucas Cotta <lu...@gmail.com> wrote:
> >
> > I actually could also use a custom similarity class that always returns
> 1.0
> > then I could use small boost factors such as ^1, ^2, ^3, etc.
> >
> > But I want to do this only in some specific queries (that may contain
> other
> > fields besides studentId)
> >
> > How could I do this, use the custom similarity class only for some
> queries?
> > Is it possible?
> >
> > Thanks!
> >
> > 2016-10-03 19:49 GMT-03:00 Lucas Cotta <lu...@gmail.com>:
> >
> >> Hello,
> >>
> >> I'm new in Solr (4.7.2) and I was given the following requirement:
> >>
> >> Given a query such as:
> >>
> >> studentId:(875141 OR 873071 OR 875198 OR 108142 OR 918841 OR 870688 OR
> >> 107920 OR 870637 OR 870636 OR 870635 OR 918792 OR 107721 OR 875078 OR
> >> 875166 OR 875151 OR 918829 OR 918808)
> >>
> >> I want the results to be ordered by the same order the elements were
> >> informed in the query. This would be similar to MySQL's ORDER BY
> >> FIELD(id, 3,2,5,7,8,1).
> >>
> >> I have tried to use term boosting
> >> <https://wiki.apache.org/solr/SolrRelevancyCookbook#
> Boosting_Ranking_Terms>
> >> in the query but that only works when I use big factors like this:
> 875078^100000
> >> OR 875166^10000 OR 875151^1000 OR 918829^100OR 918808^10
> >>
> >> But that would cause the query to be too big in case I have 200 ids for
> >> instance.
> >>
> >> So it seems I need to implement a custom FunctionQuery.
> >> I'm a little lost on how to do that. Could someone please give me an
> idea?
> >> Which classes should my custom class extend from? Where should I place
> this
> >> class? Should I add to Solr project it self and regenerate the JAR?
> >>
> >> Thanks
> >>
>
>

Re: How to implement a custom boost function

Posted by Walter Underwood <wu...@wunderwood.org>.
How about sorting them after you get them back from Solr?

wunder
Walter Underwood
wunder@wunderwood.org
http://observer.wunderwood.org/  (my blog)


> On Oct 3, 2016, at 6:45 PM, Lucas Cotta <lu...@gmail.com> wrote:
> 
> I actually could also use a custom similarity class that always returns 1.0
> then I could use small boost factors such as ^1, ^2, ^3, etc.
> 
> But I want to do this only in some specific queries (that may contain other
> fields besides studentId)
> 
> How could I do this, use the custom similarity class only for some queries?
> Is it possible?
> 
> Thanks!
> 
> 2016-10-03 19:49 GMT-03:00 Lucas Cotta <lu...@gmail.com>:
> 
>> Hello,
>> 
>> I'm new in Solr (4.7.2) and I was given the following requirement:
>> 
>> Given a query such as:
>> 
>> studentId:(875141 OR 873071 OR 875198 OR 108142 OR 918841 OR 870688 OR
>> 107920 OR 870637 OR 870636 OR 870635 OR 918792 OR 107721 OR 875078 OR
>> 875166 OR 875151 OR 918829 OR 918808)
>> 
>> I want the results to be ordered by the same order the elements were
>> informed in the query. This would be similar to MySQL's ORDER BY
>> FIELD(id, 3,2,5,7,8,1).
>> 
>> I have tried to use term boosting
>> <https://wiki.apache.org/solr/SolrRelevancyCookbook#Boosting_Ranking_Terms>
>> in the query but that only works when I use big factors like this: 875078^100000
>> OR 875166^10000 OR 875151^1000 OR 918829^100OR 918808^10
>> 
>> But that would cause the query to be too big in case I have 200 ids for
>> instance.
>> 
>> So it seems I need to implement a custom FunctionQuery.
>> I'm a little lost on how to do that. Could someone please give me an idea?
>> Which classes should my custom class extend from? Where should I place this
>> class? Should I add to Solr project it self and regenerate the JAR?
>> 
>> Thanks
>> 


Re: How to implement a custom boost function

Posted by Lucas Cotta <lu...@gmail.com>.
I actually could also use a custom similarity class that always returns 1.0
then I could use small boost factors such as ^1, ^2, ^3, etc.

But I want to do this only in some specific queries (that may contain other
fields besides studentId)

How could I do this, use the custom similarity class only for some queries?
Is it possible?

Thanks!

2016-10-03 19:49 GMT-03:00 Lucas Cotta <lu...@gmail.com>:

> Hello,
>
> I'm new in Solr (4.7.2) and I was given the following requirement:
>
> Given a query such as:
>
> studentId:(875141 OR 873071 OR 875198 OR 108142 OR 918841 OR 870688 OR
> 107920 OR 870637 OR 870636 OR 870635 OR 918792 OR 107721 OR 875078 OR
> 875166 OR 875151 OR 918829 OR 918808)
>
> I want the results to be ordered by the same order the elements were
> informed in the query. This would be similar to MySQL's ORDER BY
> FIELD(id, 3,2,5,7,8,1).
>
> I have tried to use term boosting
> <https://wiki.apache.org/solr/SolrRelevancyCookbook#Boosting_Ranking_Terms>
> in the query but that only works when I use big factors like this: 875078^100000
> OR 875166^10000 OR 875151^1000 OR 918829^100OR 918808^10
>
> But that would cause the query to be too big in case I have 200 ids for
> instance.
>
> So it seems I need to implement a custom FunctionQuery.
> I'm a little lost on how to do that. Could someone please give me an idea?
> Which classes should my custom class extend from? Where should I place this
> class? Should I add to Solr project it self and regenerate the JAR?
>
> Thanks
>