我一直在尝试为这个要求找到解决方案,但我遇到了很多死胡同。
我使用Cloudant作为用户文档的数据存储。每个用户文档都有一个名为“items”的字段(属性),它是一个对象数组。
所以一个用户文档看起来像这样:
{
"_id":"userid1",
"_rev":"XX",
"username": "foobaruser"
"items":
[
{
"date":1357879144069,
"text":"don't cry because it's over, smile because it happened.",
"cat":"determination"
},
{
"date":1357879179209,
"text":"those who mind don't matter, and those who matter don't mind.",
"cat":"fitness"
},
{
"date":1357883809736,
"text":"be the change that you wish to see in the world.",
"cat":"determination"
},
{
"date":1357879179209,
"text":"those who mind don't matter, and those who matter don't mind.",
"cat":"hardwork"
},
{
"date":1357879179209,
"text":"those who mind don't matter, and those who matter don't mind.",
"cat":"determination"
}
]
}
- 数据存储中有多个这样的用户文档,每个文档都有属性“项目”
要求:
理想情况下,我想在视图上使用搜索功能并传入“cat”的值,然后返回所有文档中与“cat”值匹配的所有“项目”。
例如 https://[用户名].cloudant.com/dbname/_design/views/_search/doc?q=determination
上面的搜索将返回所有用户文档中“项目”中的所有对象,这些文档具有“cat = 确定”,格式类似于:
{ "total_rows": 2, "rows": [{ "id": "userid1", "items": [ { "date":1357879144069, "text":"don't cry because it's over, smile because it happened.", "cat":"determination" }, { "date":1357883809736, "text":"be the change that you wish to see in the world.", "cat":"determination" }, { "date":1357879179209, "text":"those who mind don't matter, and those who matter don't mind.", "cat":"determination" } ] }, { "id": "userid2", "items": [ { "date":135787966655, "text":"Some text", "cat":"determination" } ] }] }
如果使用“搜索”无法做到这一点,那么是否可以使用二级索引来实现这一点?