Drupal 7 local apache solr: Geospatial search by radius
In this blog we ll explain how to set up a geospatial search by radius using apache solr geospatial extension in a drupal 7.
You ll need the following modules to make the geospatial search by radius work:
location, search_api, search_api_solr, search_api_location, gmap, views
Optionally you can also use the facetapi to allow for facets.
Now we need to set up a solr instance. Thanks to http://ygerasimov.com who already compiled a version of apachesolr with the extension in it. He also copied the search_api_solr schema.xml and solrconfig.xml into it. We can download it here: http://ygerasimov.com/geo/sites/default/files/apachesolr.tar.gz
You can download it to /etc/apachesolr.tar.gz and extract it with
cd /etc wget <a href="http://ygerasimov.com/geo/sites/default/files/apachesolr.tar.gz tar">http://ygerasimov.com/geo/sites/default/files/apachesolr.tar.gz tar</a> -xvf apachesolr.tar.gz rm apachesolr.tar.gz cd apachesolr
You can start it up, to test it:
java -jar start.jar
Go to port 8983 http://127.0.0.1:8983/solr/ and check out the interface.
Now a start up script would be nice. So you can start it and stop it easly and it will run in the background.
Create a file /etc/init.d/solr and paste this
#!/bin/sh -e # Starts, stops, and restarts solr SOLR_DIR="/etc/apachesolr" JAVA_OPTIONS="-Xmx1024m -DSTOP.PORT=8079 -DSTOP.KEY=stopkey -jar start.jar" LOG_FILE="/var/log/solr.log" JAVA="/usr/bin/java" case $1 in start) echo "Starting Solr" cd $SOLR_DIR $JAVA $JAVA_OPTIONS 2> $LOG_FILE & ;; stop) echo "Stopping Solr" cd $SOLR_DIR $JAVA $JAVA_OPTIONS --stop ;; restart) $0 stop sleep 1 $0 start ;; *) echo "Usage: $0 {start|stop|restart}" >&2 exit 1 ;; esac
Next thing you ll need is an alias so you can use the commands from anywhere on the server.
nano .bashrc #It is normaly located in the user folder #solr alias alias solr="/etc/init.d/solr"
Exit and test with "solr start" or "solr stop" or "solr restart" (kill and restart your shell to make it take effect)
If you want solr to start when the server boots you ll need this to add it to the boot sequence.
update-rc.d solr defaults
Now we are done on the server level. Lets get to our drupal instance. Check out admin/config/search/search_api, you ll see that we ll need and index and a server.
First we create a server by selecting our solr
/admin/config/search/search_api/add_server. Fill in the location of our solr which is http://localhost:8983/solr. Feel free to set up authentication but this will not be treated in this post.
Now create an index by selecting our solr server we created. Select in the fields tab all the fields you want in the index. These field will be used to perform the search onto. Make sure you have the longitude latitude field enabled (its provided by location module) because these coordinates will be used to do the radius search on.
Create a content type and on the admin/structure/types/manage/[content-type]/edit page enable the location functionality. Do not use this together with location cck field. It wont work.
Adminster the location settings over here:/admin/config/content/location. Enable "Use a Google Map to set latitude and longitude ". Set up a goolge map api key to allow this to work (link is on the page).
Administer the map settings here:admin/config/services/gmap and here admin/config/services/gmap_location. Yo have a macro generator which comes with the gmap module. So turn that module on to generate your own macro to use for your maps.
Now create a couple of nodes. Set their location not to far from each other so we can test easly.
We are almost there. Now to create a search page we ll need views. So create a view with the search index as base table. Add a filter called "Search: Radius" and expose it. Fill in the exposed settings being the coordinates of the center of the circle and default radius. Do NOT select gmap for a view display it should be HTML List. Change type of Exposed form style to Search API Location. See image:
In Search API location exposed form style options set following string to GMap macro: [gmap align=Center |zoom=9 |center=52.0904,5.1004 |width=600px |height=500px |control=Small |type=Map]
Now you should have something like this:
To see it working on a live demo:
http://ygerasimov.com/geo/search_location
Thanks to http://ygerasimov.com to contribute the module and the precompiled package.
Add new comment