0

I'm having a problem with Solr 3.4, I'm using it's spacial search functions like Geodist, and Geofilt. Everything seems ok and the results are return supposedly sorted by distance form a given center point.

However since Solr 3.4 lacks the ability to return function results in the data I had to calculate it manually (by PHP in this case).

I read the docs and the geodist should be a function that implements the haversine function of geo distance between 2 lat/lng points. I ported the function to PHP (easy!), and made sure that it give correct result.

The problem is: Solr calculate the distance in different formula that I couldn't find. So when I re-calculate the distance in PHP it results a inconsistent data distances (e.g. 132Mile instead of 83Mile), that's not a difference I can tolerate.

My Solution: I said OK it's handy to create a function comparison to see if I made a mistake in my port to the data, I dug into Solr code and extracted the literal implementation of havesine in org.apache.solr.search.function.distance.HaversineConstFunction, and the result was almost identical. and made this testing script (full source code and data).

My conclusion that Solr (or Lucene) does not use haversine as a geodist implemenation. But I don't know which equation.

UPDATE The bug was resolved. I think I went too far with my tests. The incorrect results occurred because of wrong parameter naming, I was using order (the one from SQL) instead of sort (Solr convention) to change the order of the results from the Solr web-service.

Omar Al-Ithawi
  • 4,988
  • 5
  • 36
  • 47
  • Are you sure that you're not simply getting the correct numbers and using wrong units? 83 miles is at least roughly 132 km. – jarnbjo Dec 05 '11 at 14:10
  • To continue with jarnbjo, the most common mistakes I see with Haversine are 1) forgetting to convert lat/lon from degrees to radians, and 2) not realizing the units for the radius of the earth will be the output distance units. What radius of the earth did you use in your Haversine formula? – TreyA Dec 05 '11 at 21:46
  • @jarnbjo: I took units in consideration in all the points but I guess there is one that I've missed, which is the Solr webservice unit! – Omar Al-Ithawi Dec 06 '11 at 08:06
  • @TreyA I already put the source code of my tests at https://gist.github.com/1433194 , but the radius I'm using is: `DistanceUtils.EARTH_MEAN_RADIUS_KM * DistanceUtils.KM_TO_MILES = 6371.009 * 0.621371192 exactly` – Omar Al-Ithawi Dec 06 '11 at 08:09

1 Answers1

1

See the update, bug have been resolved. Thanks to @jarnbjo, and @TreyA for reminding me of a stupid issue. I should look to stupid mistakes in my code before debugging the libraries code in the future.

Omar Al-Ithawi
  • 4,988
  • 5
  • 36
  • 47