基于对象属性值在Cloudant(CouchDB)中创建视图
我一直在试图找到解决这个问题的方法,但我遇到了很多死胡同 我正在使用Cloudant作为用户文档的数据存储。每个用户文档都有一个名为“items”的字段(属性),它是一个对象数组 因此,用户文档如下所示:基于对象属性值在Cloudant(CouchDB)中创建视图,couchdb,cloudant,Couchdb,Cloudant,我一直在试图找到解决这个问题的方法,但我遇到了很多死胡同 我正在使用Cloudant作为用户文档的数据存储。每个用户文档都有一个名为“items”的字段(属性),它是一个对象数组 因此,用户文档如下所示: { "_id":"userid1", "_rev":"XX", "username": "foobaruser" "items": [ { "date":1357879144069, "text":"d
{
"_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”
{
"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"
}
]
}]
}
在CouchDB中,不能将查询参数的动态值传递给视图(在您的情况下是cat=determination)。CouchDB中的方法是创建一个更通用的视图,然后在调用视图获取所需数据时调整结果的排序方式 您需要在db的设计文档中创建自定义视图以实现此目的:
byUserItemCat: {
map: function (doc) {
if ( !doc.items || doc.items.length === 0 ) return;
for ( var i=0; i<doc.items.length; i++ ) {
emit(doc.items[i].cat,{doc._id,doc.items[i].date,doc.items[i].text});
}
}
}
如果您只是对索引键(即cat
)为“确定”的结果感兴趣,您应该:
curl -X GET http://127.0.0.1:5984/<db-name>/_design/<design-doc-name>/_view/byUserItemCat?key="determination"
curl-X GEThttp://127.0.0.1:5984//_design//_view/byUserItemCat?key=“决心”
Cloudant的搜索在返回文档的粒度上工作。您的索引功能可以索引文档中项
s中使用的类别,然后您的搜索将返回包含您提到的类别的项
s的文档。然后,您的应用程序代码需要向下过滤到项
s才能返回到客户端
您的索引函数类似于(无耻地抄袭Wintamute):
如果(!doc.items | | doc.items.length==0)返回;
var项目;
对于(var i=0;i这实际上可以通过Cloudant搜索完成。我有一个类似的问题需要解决。特别感谢Cloudant的Mike Bresslin的帮助
function(doc){
if (doc.items) {
for (var n in doc.items) {
if (doc.items[n].cat) {
index("cat", doc.items[n].cat, { store : true });
}
}
}
}
您可以通过以下方式在搜索中查询:
?q=cat:hardwork OR cat:determination
你试过了吗?如果它不能满足你的需要,你需要创建一个视图。对不起,我不知道你的意思。我试过什么了吗?你试过搜索了吗?它可能适合你的需要(而且你最适合尝试它,看看它是否有效)。我试过进行搜索,但它不起作用,因为你需要创建“项目”的索引,类型为数组(对象)。但我正在搜索的关键字是纯文本字符串(例如“determination”)。搜索无法遍历单个属性并检查“cat”=“determination”.或者我在索引创建方面遗漏了什么?我也有同样的问题。目前,我有一个搜索索引,希望能够将其添加到搜索索引中,而不是将其与二级索引结合。我正在等待Cloudant的一个非常精明的人的回复。如果我收到一个解决方案,我会发布它。这正是我所做的寻找感谢!我还通过数据模型改进了一点,因为如果数组的大小增加,我上面给出的嵌套模型将出现问题。
function(doc){
if (doc.items) {
for (var n in doc.items) {
if (doc.items[n].cat) {
index("cat", doc.items[n].cat, { store : true });
}
}
}
}
?q=cat:hardwork OR cat:determination