我一直在尝试找到满足此要求的解决方案,但遇到了很多死胡同。
我在用着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"
}
]
}
- 数据存储中有多个这样的用户文档,每个文档都有属性“items”
要求:
-
理想情况下,我想在视图上使用搜索功能并传入“cat”值,然后返回所有文档中与“cat”值匹配的所有“项目”。
例如
https://[用户名].cloudant.com/dbname/_design/views/_search/doc?q=测定
-
上面的搜索将返回所有用户文档中“items”中的所有对象,这些对象具有“cat=decision”,格式类似于以下内容:
{
"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"
}
]
}]
}
如果使用“搜索”不可能做到这一点,那么是否可以使用二级索引来实现这一点?