In this post we will be using curl to construct a geospatial index using Riak Search 2 (also known as Yokozuna) which is backed by Solr.
I’ll be using Riak Pre11 for this.
on: (It was on line
411 for me)
Make sure your
ulimit is 4096 or greater:
Then start Riak. I’m using
console myself, but
would also work.
Let’s create a schema so that Solr indexes our data correctly (file available here):
The important parts here are the schema name being
our two fields.
Pretty much everything else is boilerplate so that the Riak Solr integration works.
We can now upload this schema (I saved the schema above as schema.xml in my current directory):
…and we create an index named “my_geo_index” which uses the schema (name = “geotest”) we just uploaded.
They should both return 204 responses.
Next we’ll create a bucket type named “geo_type” using the
riak-admin command. Our bucket type won’t have any special
properties, it just needs to exist.
We also need to activate our new bucket type:
We will now create a bucket named “stuff” under the
geo_type bucket type. In addition, this command associates
the Solr index
my_geo_index with the bucket
That’s it. Let’s index some data!
Now for the fun part. Let’s find all of our data, scored and sorted by distance. The score will return a distance (in degrees). We are querying from a location in Palo Alto, California, so we should see fairly small distances to Mountain View, San Jose and San Francisco.
The query returns the results:
You’ll notice that there are two San Franciscos. This is because I inserted data twice into Riak without using a VClock the second time (While I was writing this post), resulting in siblings. This issue is easily resolvable by resolving the siblings as mentioned here.
Now, we can convert to miles by multiplying the score (which
is degrees) by
69.09341. If we do this for San Jose it
.2221485 * 69.09341, or about
For Kilometers we use
111.1951, which gives us about
Since our query location was from Palo Alto, California, we can see that San Jose is indeed, approximately that 15 miles away. Our search was successful!