使用Riak 1.4.7
我有一个文档,其属性是嵌入文档的数组。以下是该文档的示例:
{"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"}]
}
相应的存储桶已启用索引并且工作正常。例如,以下搜索命令可以毫无问题地找到对象:
search-cmd search index_name contact_email:contact@HzNUeioPYynsGdXL6iSFvQ.gr
这里的问题是如何通过store_url
进行搜索。
store_url
是嵌入式文档的属性,而后者又是主文档的数组属性的元素。
1)我是否必须指定自定义模式文件才能使索引为这些属性编制索引?
2)我是否需要使用某种特殊语法进行查询?
答案 0 :(得分:1)
默认的JSON提取器应该通过在空格分隔列表中连接数组中的所有值来处理这种情况。嵌套名称通过使用下划线连接来处理。因此,在这种情况下,字段e_shops_store_url
将包含www.store.url.com www.store2.url.com
。您可以正常查询该字段。
我跑了一个简单的例子来证明:
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".