Filtering by distance vs. field value in Solr

2019-08-10 01:37发布

问题:

I have a schema which includes a maximum radius for a result to show up - for example, 50. I am able to calculate distance and filter by that without problem, but I really need to filter by that field, which changes from document to document. I've tried:

fq={!frange l=0 u=radius_field}geodist()

and

fq={!func}geodist():[0 TO radius_field]

to no effect. Is there any way to do this? I could filter client-side, worst case, but I figure there's somebody out there who has had the same use case.

回答1:

Not a direct answer, but could you create a new function is_in_range(radius_field) that returns a boolean result if the geodist() is less than the radius_field parameter? This might circumvent the issues you are finding with trying to filter by a range using a parameter.



回答2:

It's ugly but I think I got it to work without a custom function. I know this is old, but wanted to post in case someone else is looking.

fq={!frange l=0 h=12742}sub(radius_field,geodist(field,point))

The 12742 is the diameter of the earth in km as I still needed a hard number for that, but I doubt most are searching in space. So basically we subtract the distance from radius_field to find out if it is in range.

radius_field - distance

If the results are a positive number than it is within range. If it is a negative number than it is not. Please let me know if I screwed up my logic. Thanks.



标签: solr