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:
http://ianibbo.me/?p=127
http://www.lucidimagination.com/blog/2010/07/20/update-spatial-search-in-apache-lucene-and-solr/

Also, Solr 1.5 (or will it be 3.1?) will have native support for spatial searches
http://blog.jteam.nl/2010/04/14/state-of-solr/
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://github.com/jrask/spatial-solr-demo.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.

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

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. 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 nl.jteam.search.solrext.spatial.SpatialQParserPlugin$SpatialQParser.extractSearchAreas(SpatialQParserPlugin.java:149)
    at nl.jteam.search.solrext.spatial.SpatialQParserPlugin$SpatialQParser.(SpatialQParserPlugin.java:96)
    at nl.jteam.search.solrext.spatial.SpatialQParserPlugin$SpatialQParser.(SpatialQParserPlugin.java:80)
    at nl.jteam.search.solrext.spatial.SpatialQParserPlugin.createParser(SpatialQParserPlugin.java:54)
    at org.apache.solr.search.QParser.getParser(QParser.java:252)
    at org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:88)
    at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:174)
    at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:131)
    at org.apache.solr.core.SolrCore.execute(SolrCore.java:1316)
    at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:338)
    at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:241)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1089)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:365)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:211)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
    at org.mortbay.jetty.Server.handle(Server.java:285)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:502)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:821)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:208)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:378)
    at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:226)
    at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)

    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. 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. Johan,

    Did you update the above instructions for version 2.0 of SSP? I get this error:
    ——-8<——–

    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:

    false

    in null

    ————————————————————-
    org.apache.solr.common.SolrException: Error loading class ‘nl.jteam.search.solrext.spatial.SpatialTierQueryParserPlugin’
    at org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:375)

    Caused by: java.lang.ClassNotFoundException: nl.jteam.search.solrext.spatial.SpatialTierQueryParserPlugin
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)

  4. 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

Close Menu