Search code examples
riakriak-search

How can I ask Riak 1.4 to index and then be able to search my document based on properties of embedded document?


Working with Riak 1.4.7

I have a document that has a property that is an array of embedded documents. Here is a sample of the document:

{"prospect":true, "name":"HzNUeioPYynsGdXL6iSFvQ",
"contact_email":"[email protected]",
"e_shops":[{"store_url":"www.store.url.com","display_name":"hello there"},
           {"store_url":"www.store2.url.com","display_name":"hello2 there"}]
}

The corresponding bucket has index enabled and works fine. For example, the following search command locate the object without problem:

search-cmd search index_name contact_email:[email protected]

The question here is how can I search by the store_url for example.

store_url is a property of an embedded document which, in turn, is an element of an array property of the main document.

1) Do I have to specify a custom schema file in order for the index to index these properties?

2) Do I have to query using some kind of special syntax?


Solution

  • The default JSON extractor should handle that case by joining all of the values in the array in a space separated list. Nested names are handled by joining them with an underscore. So in this case, the field e_shops_store_url would contain www.store.url.com www.store2.url.com. You can query that field normally.

    I ran a quick example to demonstrate:

    root@node1:~# search-cmd install searchtest
    
     :: Installing Riak Search <--> KV hook on bucket 'searchtest'.
    root@node1:~# curl 172.31.0.1:8098/buckets/searchtest/keys/test1 \
    -XPUT -H"content-type:application/json" \
    -d '{"prospect":true, "name":"HzNUeioPYynsGdXL6iSFvQ",
    > "contact_email":"[email protected]",
    > "e_shops":[{"store_url":"www.store.url.com","display_name":"hello there"},
    >            {"store_url":"www.store2.url.com","display_name":"hello2 there"}]
    > }'
    root@node1:~# curl 172.31.0.1:8098/buckets/searchtest/keys/test2 \
    -XPUT -H"content-type:application/json" \
    -d '{"prospect":true, "name":"-HzNUeioPYynsGdXL6iSFvQ",
    >"contact_email":"[email protected]",
    >"e_shops":[{"store_url":"www.store.url.com","display_name":"hello there"},
    >           {"store_url":"www.store3.url.com","display_name":"hello3 there"}]
    >}'
    root@node1:~# search-cmd search-doc searchtest e_shops_store_url:www.store.url.com
    
     :: Searching for 'e_shops_store_url:www.store.url.com' / '' in searchtest...
    
    ------------------------------
    
    index/id: searchtest/test1
    <<"contact_email">> => <<"[email protected]">>
    <<"e_shops_display_name">> => <<"hello there hello2 there">>
    <<"e_shops_store_url">> => <<"www.store.url.com www.store2.url.com">>
    <<"name">> => <<"HzNUeioPYynsGdXL6iSFvQ">>
    <<"prospect">> => <<"true">>
    
    ------------------------------
    
    index/id: searchtest/test2
    <<"contact_email">> => <<"[email protected]">>
    <<"e_shops_display_name">> => <<"hello there hello3 there">>
    <<"e_shops_store_url">> => <<"www.store.url.com www.store3.url.com">>
    <<"name">> => <<"-HzNUeioPYynsGdXL6iSFvQ">>
    <<"prospect">> => <<"true">>
    
    ------------------------------
    
     :: Found 2 results.
     :: Maximum score "0.353553".