Geo search with spatial-solr-plugin

2011-05-10: Updated to support version 2.0 of the plugin.

Recently, I was lucky to be contacted by a client who wanted help implementing geo search with Solr . With spatial support, we can get answers to queries like “How many coffee shops are there within 10km from this location, and what is the distance to each location”. So, if you are sitting on data (in any format and any storage) with geo (lat/long) information you can easily benefit from this using Solr.

Since Solr currently does not have native support for this, we decided to try out spatial-solr-plugin from jteam. This blogpost describes how the plugin works and you should definitely read it since it covers much more aspects of the plugin than I will explain here. However, I had trouble getting stuff to work due to a few errors in the documentation so hopefully this post can assist there.

About changes to version 2.0 of the plugin
Once you have the plugin installed, the only change you have to do is in solrconfig.xml where the classname of the queryParser has been changed and the updateProcessor has been removed. Checkout the latest changes, start solr and execute the tests.

Since Solr now has native support for spatial searches this blog will not be updated if new versions are released.

Status of Solr and spatial searches
Before reading on, you should be aware of some limitations with this plugin, More here:

Also, Solr 1.5 (or will it be 3.1?) will have native support for spatial searches
Update:Version 3.1 has of Solr has been released!

Example code
I have an example project with a solrconfig.xml, schema.xml and java unit tests that you can play with to verify that you have successfully configured solr. The project is hosted on github so simply use git clone git:// to get the sources.

Download and install solr
First download Solr here and extract it to a directory of your choice.
In the rest of the blog, I am assuming that you are running Solr from solr_home/example with java -jar start.jar.

Download plugin
You can download the plugin here, unfortunately this requires you to register and get an email with download link but at least you will get the email right away. After you have downloaded the plugin, create a lib directory under solr_home/example/solr and put the plugin there (solr_home/example/solr/lib). Next, start Solr and verify that you do not get any errors. The download also include documentation.

Configure solrconfig.xml
Next, we need to configure solr to make use of the plugin. In my example project, spatial-solr-demo, you can find a complete solrconfig.xml that you can replace your existing solrconfig.xml with if you haven´t done any changes to it.

This is the query parser used.


Adds calculated distance to a field in the response document.




Configure schema.xml

You need to add the required fields (see solrconfig.xml) so that the plugin can do its work
properly. I have also added “type” as an application field in my unit tests. I also use field “name”
but that already exists in the default schema.

That´s it, you should now have a solr ready to index and search geo tagged information.

Executing queries
To find documents, you first have to specify a least one query which will fetch a number of documents from the index, these documents will then be filtered by the plugin based on geo information.

This query will return all documents of type:cafe that are within 10 km from Lomma (small city in sweden).
{!spatial circles=55.672,10} type:cafe
Note: Fore more info and options about the queryParser, checkout the plugin documentation.

Running JUnit tests
The JUnit tests simply index some documents and performs a search to verify that it works as expected.

Location lomma = new Location(55.672, 13.069);
Location staffanstorp = new Location(55.642972, 13.206255);
Location malmo = new Location(55.605599, 13.026653);

There is a simple class called SpatialSolrClient which has a search() method looking like this.

private static final String QUERY =
		"{!spatial circles=%s,%s,%s} %s";

public SolrDocumentList search(String query, Location loc, double radius) throws SolrServerException {
	return search(String.format(QUERY,loc.getLat(),loc.getLng(),radius,query));
// Create and index two attractions along with their location
client.index(new Attraction(staffanstorp, "cafe", "Staffanstorp coffee shop"));
client.index(new Attraction(malmo, "cafe", "Malmo coffee shop"));

// Verify that we get the expected results

// Get the list
List list ="type:cafe",lomma,30);
verifyDocument(list.get(1),"Staffanstorp coffee shop",9.1,9.3);
verifyDocument(list.get(0),"Malmo coffee shop",7.8,7.9);
private void verifyDocument(SolrDocument doc, String name, double min, double max) {
    // Verify name
    assertEquals(doc.get("name"), name);
    System.out.println("From search point (Lomma) to "+ doc.getFieldValue("name") +"; "
	  + kmDistanceFormat.format((Double)doc.getFieldValue("geo_distance")) + " km");
    // Verify distance between points

private boolean between(double value, double min, double max) {
    return (value > min && value < max);

Hopefully the tests will pass and you should get the following text in you console:
From search point (Lomma) to Staffanstorp coffee shop; 9.21 km
From search point (Lomma) to Malmo coffee shop; 7.86 km

That's it, I haven't looked at how the native support for spatial searches will work but hopefully I will have time to check it out soon.

This Post Has 4 Comments

  1. Subodh

    Hi i was trying ur example but i m getting this exception can u plz help me to resolve the issue

    INFO: [] webapp=/solr path=/update params={waitSearcher=true&waitFlush=true&wt=javabin&commit=true&version=1} status=0 QTime=388
    Feb 11, 2011 5:59:29 PM org.apache.solr.common.SolrException log
    SEVERE: org.apache.solr.common.SolrException: Circles parameter missing
    at org.apache.solr.handler.component.QueryComponent.prepare(
    at org.apache.solr.handler.component.SearchHandler.handleRequestBody(
    at org.apache.solr.handler.RequestHandlerBase.handleRequest(
    at org.apache.solr.core.SolrCore.execute(
    at org.apache.solr.servlet.SolrDispatchFilter.execute(
    at org.apache.solr.servlet.SolrDispatchFilter.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler.handle(
    at org.mortbay.jetty.servlet.SessionHandler.handle(
    at org.mortbay.jetty.handler.ContextHandler.handle(
    at org.mortbay.jetty.webapp.WebAppContext.handle(
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(
    at org.mortbay.jetty.handler.HandlerCollection.handle(
    at org.mortbay.jetty.handler.HandlerWrapper.handle(
    at org.mortbay.jetty.Server.handle(
    at org.mortbay.jetty.HttpConnection.handleRequest(
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(
    at org.mortbay.jetty.HttpParser.parseNext(
    at org.mortbay.jetty.HttpParser.parseAvailable(
    at org.mortbay.jetty.HttpConnection.handle(
    at org.mortbay.thread.BoundedThreadPool$

    Feb 11, 2011 5:59:29 PM org.apache.solr.core.SolrCore execute
    INFO: [] webapp=/solr path=/select params={wt=javabin&q={!spatial+lat%3D55.672+long%3D13.069+radius%3D1.0+unit%3Dkm+threadCount%3D2}+type:cafe&version=1} status=400 QTime=2

    thanks in advance

  2. Johan Rask

    Are you using version 2.0 of the plugin? In my example I am using the older version so this might be the problem. Next week I might have time to update the examples with the latest plugin.

  3. gavin


    Did you update the above instructions for version 2.0 of SSP? I get this error:

    HTTP ERROR: 500

    Severe errors in solr configuration.

    Check your log files for more detailed information on what may be wrong.

    If you want solr to continue after configuration errors, change:


    in null

    org.apache.solr.common.SolrException: Error loading class ‘’
    at org.apache.solr.core.SolrResourceLoader.findClass(

    Caused by: java.lang.ClassNotFoundException:

  4. Johan Rask

    I have updated the code and this post to support the latest version of the plugin. However, since native support for this has been released in version 3.1 I will not update this blogpost if newer versions are released.

Leave a Reply