andrexus
andrexus

Reputation: 165

How to get distance in Solr 4 geospatial search?

I'm playing with the new Solr 4 geospatial search. Like in an example from http://wiki.apache.org/solr/SolrAdaptersForLuceneSpatial4 I'm trying to get the results like so:

http://localhost:8983/solr/allopenhours/select?  
&q=foobar  
&fq=geo:%22Intersects(Circle(54.729696,-98.525391%20d=0.08992))%22  
&q={!%20score=distance}  
&fl=*,score 

But it doesn't work. How can I get distance and score fields in the results set?

Upvotes: 6

Views: 9172

Answers (2)

HTIT
HTIT

Reputation: 51

The answer Paige gave is correct. However, the error is shown depending on query given.

Error parsing fieldname: geodist - not enough parameters:[]

geodist needs the sfield (field which holds the location in the document) and a pt (the central point of the circle). If it can't find any of these, it will throw the error shown.

Either add these two to the URL

&pt=52.373,4.899&sfield=store&fl=_dist_:geodist()

Or add the two (or actually 3: pt, lat and lon) to the geodist() function call:

&fl:_dist_:geodist(store,52.373,4.899)

Note that in the first case, if you have additional geo functions (like geofilt) in your query, the pt and sfield are used for that as well (unless locally overridden)

Upvotes: 5

Paige Cook
Paige Cook

Reputation: 22555

According to the reference Spatial Search - Returning the distance you can edit your fields parameter to do one of the following:

  • &fl=*,score,geodist()
  • &fl=*,score,_dist_:geodist() - this one will return the distance in the alias _dist_

Upvotes: 8

Related Questions