Search code examples
searchsolrpolygongeospatialpoint

Solr spatial search with input point and query which polygon within


I have some polygons indexed in Solr. Is it possible to query with a point(lat,lon) and see which polygon has that point inside?


Solution

  • Yes it is possible and described here: http://wiki.apache.org/solr/SolrAdaptersForLuceneSpatial4 Your Solr Version must be 4 or higher and you have to import the JTS jar-file which you can get from http://sourceforge.net/projects/jts-topo-suite/ You have to define a field with a fieldType of location_rpt

    <fieldType name="location_rpt"   class="solr.SpatialRecursivePrefixTreeFieldType"
               spatialContextFactory="com.spatial4j.core.context.jts.JtsSpatialContextFactory"
               distErrPct="0.025"
               maxDistErr="0.000009"
               units="degrees"
            />
    

    in the schema.xml. Then you have to index the polygons like:

    <field name="geo">POLYGON((-10 30, -40 40, -10 -20, 40 20, 0 0, -10 30))</field>
    

    But i think you already did it this way because you wrote that you already have them indexed.

    For the query you simply have to use the filter query fq=geo:"Intersects(10.12 50.02)" where 10.12 and 50.02 represent the latitude and longitude of your point.