Skip to content

Instantly share code, notes, and snippets.

@u10313335
Last active September 3, 2015 11:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save u10313335/5649488bc3a4037e1357 to your computer and use it in GitHub Desktop.
Save u10313335/5649488bc3a4037e1357 to your computer and use it in GitHub Desktop.
Spatial search in ckanext-spatial under Solr 5.x & solr-spatial-field backend
  • Download JTS 1.13 from Maven:

http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22

  • Put the downloaded jar file to /opt/solr/server/solr-webapp/webapp/WEB-INF/lib:
sudo -u solr cp jts-1.13.jar /opt/solr/server/solr-webapp/webapp/WEB-INF/lib/.
  • Add the following field type and field to your Solr schema file:
<types>
    <!-- ... -->
    <fieldType name="location_rpt" class="solr.SpatialRecursivePrefixTreeFieldType"
        spatialContextFactory="com.spatial4j.core.context.jts.JtsSpatialContextFactory"
        autoIndex="true"
        distErrPct="0.025"
        maxDistErr="0.000009"
        distanceUnits="degrees"/>
</types>
<fields>
    <!-- ... -->
    <field name="spatial_geom"  type="location_rpt" indexed="true" stored="true" multiValued="true"/>
</fields>
  • Change the following lines of ckanext/spatial/plugin.py in ckanext-spatial extension:
diff --git a/ckanext/spatial/plugin.py b/ckanext/spatial/plugin.py
index 0c4979e..1cb610b 100644
--- a/ckanext/spatial/plugin.py
+++ b/ckanext/spatial/plugin.py
@@ -306,8 +306,7 @@ class SpatialQuery(p.SingletonPlugin):

         '''
         search_params['fq_list'] = search_params.get('fq_list', [])
-        search_params['fq_list'].append('+spatial_geom:"Intersects({minx} {miny} {maxx} {maxy})"'
-                                     .format(minx=bbox['minx'],miny=bbox['miny'],maxx=bbox['maxx'],maxy=bbox['maxy']))
+        search_params['fq_list'].append('+spatial_geom:"Intersects(ENVELOPE({minx}, {maxx}, {maxy}, {miny}))"'.format(minx=bbox['minx'],miny=bbox['miny'],maxx=bbox['maxx'],maxy=bbox['maxy']))

         return search_params
  • Define the backend to use for the spatial search in your development.ini:
ckanext.spatial.search_backend = solr-spatial-field
  • Rebuild search index:
(pyenv) paster --plugin=ckan search-index rebuild -c /etc/ckan/default/development.ini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment