Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 514
andrexus Avatar asked Jan 18 '13 15:01

andrexus


People also ask

What is Sfield in SOLR?

Solr field: sfield=store. Point to search/sort from: pt=36.35,-97.51.

What is spatial SOLR?

Solr supports location data for use in spatial/geospatial searches. Using spatial search, you can: Index points or other shapes. Filter search results by a bounding box or circle or by other shapes. Sort or boost scoring by distance between points, or relative area between rectangles.

How do you query in SOLR?

Trying a basic queryThe main query for a solr search is specified via the q parameter. Standard Solr query syntax is the default (registered as the “lucene” query parser). If this is new to you, please check out the Solr Tutorial. Adding debug=query to your request will allow you to see how Solr is parsing your query.

What is spatial search?

Spatial search is the task of identifying and investigating spatially distributed choice alternatives which serve as the basis for a locational decision.


2 Answers

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_
like image 83
Paige Cook Avatar answered Oct 19 '22 23:10

Paige Cook


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)

like image 38
HTIT Avatar answered Oct 20 '22 00:10

HTIT