How can I query Riak 1.4 for indexing and then search my document based on the properties of the embedded document?
Working with Riak 1.4.7
I have a document that has a property that is an array of inline documents. Here's an example doc:
{"prospect":true, "name":"HzNUeioPYynsGdXL6iSFvQ",
"contact_email":"contact@HzNUeioPYynsGdXL6iSFvQ.gr",
"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 an index on and is working fine. For example, the following search command finds the object without issue:
search-cmd search index_name contact_email:contact@HzNUeioPYynsGdXL6iSFvQ.gr
The question here is how can I search for eg store_url
.
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 for the index to index these properties?
2) Do I need to ask for some special syntax?
source to share
By default, the JSON extractor should handle this case by concatenating all the values ββin the array in a space-separated list. Nested names are processed by combining them with an underscore. Therefore, in this case, the field e_shops_store_url
will contain www.store.url.com www.store2.url.com
. You can request this field as usual.
I've demonstrated a quick example:
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":"contact@HzNUeioPYynsGdXL6iSFvQ.gr",
> "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":"contact@-HzNUeioPYynsGdXL6iSFvQ.gr",
>"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">> => <<"contact@HzNUeioPYynsGdXL6iSFvQ.gr">>
<<"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">> => <<"contact@-HzNUeioPYynsGdXL6iSFvQ.gr">>
<<"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".
source to share