Mailing List Archive

Change score with distance SortField
Hi,

I would like to influence the score using geographical distance. More distant documents lower the score.
I have sort field:
SortField geoSort = LatLonDocValuesField.newDistanceSort("location", pos.getLatitude(), pos.getLongitude());

Then I tried add this sort field to SimpleBindings. I've found some code, where SortField is added to SimpleBindings, but
lucene 9.4.x API doesn't have this capability.

What is the proper way to do this?

thank you, Michal Hlavac
Re: Change score with distance SortField [ In reply to ]
Unfortunately this wouldn’t have worked in earlier versions of lucene where you could pass a SortField to the bindings API either, as that only understood certain types of sort - part of the reason we removed it in favour of the more explicit DoubleValuesSource API.

You have a few options here:
- if you want to continue to use expressions, then there is some example code in TestDemoExpressions that uses separate latitude and longitude fields and the javascript haversin function that should act as a guide.
- another option with expressions is to code up your own DoubleValuesSource that re-uses some of the code from LatLonPointDistanceComparator to produce the distance for each document from a particular point. This is a bit of coding, but I can see that it would be generally useful and might be a nice contribution
- My more general recommendation would be to avoid using expressions and instead combine your scores using a boolean query with a LatLonPoint.newDistanceFeatureQuery() as a SHOULD clause. This is likely to be much more efficient, particularly if you don’t need an exact total hit count, as feature queries can make use of skip data to avoid scoring large numbers of documents.

- Alan

> On 6 Feb 2023, at 09:24, Michal Hlavac <miso@hlavki.eu> wrote:
>
> Hi,
>
> I would like to influence the score using geographical distance. More distant documents lower the score.
> I have sort field:
> SortField geoSort = LatLonDocValuesField.newDistanceSort("location", pos.getLatitude(), pos.getLongitude());
>
> Then I tried add this sort field to SimpleBindings. I've found some code, where SortField is added to SimpleBindings, but
> lucene 9.4.x API doesn't have this capability.
>
> What is the proper way to do this?
>
> thank you, Michal Hlavac


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