Saturday, August 22, 2015

How to recreate index for a bucket in Riak

As we all know that all of the data in the Riak is indexed using solr. And if we have to add another field into the index then we have to re-index the entire data again. I had to do this task last week so here I am sharing the steps that I followed.


1. Disassociate the index from the bucket. 

If you are using custom bucket type then use below:

Format:
curl -v -XPUT http://localhost:8098/types/acme/buckets/Users/props \
-H 'Content-Type: application/json' \
-d '{"props":{"search_index":"_dont_index_"}}'

Example:
curl -v -XPUT http://localhost:8098/types/acme/buckets/Users/props \
-H 'Content-Type: application/json' \
-d '{"props":{"search_index":"_dont_index_"}}'

If you are using default bucket type then use below:


Format:
curl -v -XPUT http://localhost:8098/buckets/Users/props \
-H 'Content-Type: application/json' \
-d '{"props":{"search_index":"_dont_index_"}}'

Example:
curl -v -XPUT http://localhost:8098/buckets/Users/props \
-H 'Content-Type: application/json' \
-d '{"props":{"search_index":"_dont_index_"}}'

2. Delete the index

Format:
curl -v -XDELETE http://localhost:8098/search/index/_index

Example:
curl -XDELETE http://localhost:8098/search/index/Users_index

3. Create the new schema which has the new field to be indexed.

Format:
curl -XPUT http://localhost:8098/search/schema/ \
     -H 'Content-Type:application/xml' \
     --data-binary @

Example:
curl -XPUT http://localhost:8098/search/schema/users-schema \
     -H 'Content-Type:application/xml' \
     --data-binary @solr-schema.xml

4. Re-create the index using new schema

Format:
curl -XPUT http://localhost:8098/search/index/_index \
     -H 'Content-Type: application/json' \
     -d '{"schema":""}'

Example:
curl -XPUT http://localhost:8098/search/index/Users_index \
     -H 'Content-Type: application/json' \
     -d '{"schema":"users-schema"}'

5. Associate the new index to the bucket.

If you are using custom bucket type then use below:

Format:
curl -v -XPUT http://localhost:8098/types/acme/buckets/Users/props \
-H 'Content-Type: application/json' \
-d '{"props":{"search_index":""}}'

Example:
curl -v -XPUT http://localhost:8098/buckets/Users/props \
-H 'Content-Type: application/json' \
-d '{"props":{"search_index":"Users_index"}}'

6. Re-write all of the data in the bucket

For this I wrote some python code using the Riak python module.
Sudo code is as follows:
Get all keys from the bucket
FOREACH key
  Get the document for the key
  Save it back

This will re-index the data using the new schema.