Skip to content

Instantly share code, notes, and snippets.

@imsickofmaps
Last active December 11, 2015 14:18
Show Gist options
  • Save imsickofmaps/4612905 to your computer and use it in GitHub Desktop.
Save imsickofmaps/4612905 to your computer and use it in GitHub Desktop.
HOW-TO: Set-up, configure and use Riak Search on existing buckets with Python

HOW-TO: Set-up, configure and use Riak Search on existing buckets with Python

1. Enable search on nodes

From Riak docs:

Riak Search is enabled in the app.config file. Simply change the setting to “true” in Riak Search Config section (shown below).

%% Riak Search Config  
{riak_search, [  
           %% To enable Search functionality set this 'true'.  
           {enabled, false}  
          ]},  

Restart riak node:

$ sudo riak restart

2. Enable bucket for search

From Riak docs:

$ search-cmd install my_bucket_name

3. Re-load existing data for indexing

Because we just added a post-commit hook the data already in there won't be in the index (sadtrombone.wav)

Warning: Probably not wise to do this if you have millions of keys already ... :)

With simple Python script:

import riak
rc = riak.RiakClient(host="127.0.0.1", port=8087, prefix="riak", transport_class=riak.RiakPbcTransport)
keys = rc.bucket("my_bucket_name").get_keys()
for key in keys:
	key1 = rc.bucket("my_bucket_name").get(key)
	key1.store()

4. Manually check the result

Using the command line (docs):

Example json data in my_bucket_name:

{
	"titlet":"Laptop bag",
	"price":10.0
}

Test command:

$ search-cmd search my_bucket_name "title:Laptop"

5. Retrieving results with Python

Simple:

import riak
rc = riak.RiakClient(host="127.0.0.1", port=8087, prefix="riak", transport_class=riak.RiakPbcTransport)
search_query = rc.search("my_bucket_name", "title:Laptop")
    for result in search_query.run():
        key = result.get()
        key_data = key.get_data()
        print "Loaded key:", key.get_key()
        print "Key data:\n", key_data

Just return the field we search on:

import riak
rc = riak.RiakClient(host="127.0.0.1", port=8087, prefix="riak", transport_class=riak.RiakPbcTransport)
field = "title"
keyword = "Laptop"
search_query = rc.search("my_bucket_name", field + ":" + keyword)
for result in search_query.run():
    key = result.get()
    key_data = key.get_data()
    print "Loaded key:", key.get_key()
    print field + ":", key_data[field]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment