我需要为couchbase视图创建一个map函数,该函数接收多个键,其中一些键存储在文档结构的更深层。我不能使用N1QL,因为我必须使用CouchBase3.0版,并且搜索带有两个"UNNEST“的查询需要太多时间(如果我理解正确的话,可以从3.5版开始进行数组索引)。
我知道如何从couchbase web上解释的示例中搜索数组中的一个键:http://docs.couchbase.com/admin/admin/Views/views-querySample.html
但是如何在多个数组中搜索多个键呢?这有可能吗?
假设我有这样的文档结构:
{
"title": "Fried chilli potatoes",
"servings": "4",
"totaltime": "30",
"ingredients": [
{
"ingredient": "chilli powder",
"meastext": "3-6 tsp"
},
{
"ingredient": "potatoes",
"meastext": "900 g"
},
{
"ingredient": "vegetable oil",
"meastext": ""
}
...
],
"actions": [
{
"action": "frying",
"subject": "chicken wings",
"time": "10 min"
},
{
"action": "boiling",
"subject": "potatoes",
"time": "15 min"
},
...
]
}如果我想搜索所有需要土豆的接收器,那么map函数将是:
function(doc, meta)
{
if (doc.ingredients)
{
for (i=0; i < doc.ingredients.length; i++)
{
emit(doc.ingredients[i].ingredient, null);
}
}
}我如何找到在配料列表中有“土豆”,在动作列表中有“煎炸”以及一些更多信息的文档,这些文档位于文档的第一级(如服务和总时间)?
所以我猜emit函数中返回的键应该是这样的:
emit([doc.ingredients[i].ingredient, doc.actions[i].action, doc.servings, doc.totaltime], null)查询:->key(["potato", "frying", "4", "30"])
但是我不知道怎么写循环。
发布于 2016-11-08 05:20:41
唯一的选择是发出密钥的所有可能的排列
emit([doc.ingredients[i].ingredient, doc.actions[i].action, doc.servings, doc.totaltime], null)
emit([doc.ingredients[i].ingredient, doc.actions[i].action, doc.totaltime, doc.servings], null)
emit([doc.ingredients[i].ingredient, doc.servings, doc.actions[i].action, doc.totaltime], null)
emit([doc.ingredients[i].ingredient, doc.servings, doc.totaltime, doc.actions[i].action], null)
emit([doc.ingredients[i].ingredient, doc.totaltime, doc.actions[i].action, doc.servings], null)
emit([doc.ingredients[i].ingredient, doc.totaltime, doc.servings, doc.actions[i].action], null)
emit([doc.actions[i].action, doc.ingredients[i].ingredient, doc.totaltime, doc.servings], null)
emit([doc.actions[i].action, doc.ingredients[i].ingredient, doc.servings, doc.totaltime], null)
emit([doc.actions[i].action, doc.servings, doc.totaltime, doc.ingredients[i].ingredient], null)
emit([doc.actions[i].action, doc.servings, doc.ingredients[i].ingredient, doc.totaltime], null)
emit([doc.actions[i].action, doc.totaltime, doc.servings, doc.ingredients[i].ingredient], null)
emit([doc.actions[i].action, doc.totaltime, doc.ingredients[i].ingredient, doc.servings], null)
emit([doc.servings, doc.ingredients[i].ingredient, doc.actions[i].action, doc.totaltime], null)
emit([doc.servings, doc.ingredients[i].ingredient, doc.totaltime, doc.actions[i].action], null)
emit([doc.servings, doc.actions[i].action, doc.ingredients[i].ingredient, doc.totaltime], null)
emit([doc.servings, doc.actions[i].action, doc.totaltime, doc.ingredients[i].ingredient], null)
emit([doc.servings, doc.totaltime, doc.ingredients[i].ingredient, doc.actions[i].action], null)
emit([doc.servings, doc.totaltime, doc.actions[i].action, doc.ingredients[i].ingredient], null)
emit([doc.totaltime, doc.ingredients[i].ingredient, doc.servings, doc.actions[i].action], null)
emit([doc.totaltime, doc.ingredients[i].ingredient, doc.actions[i].action, doc.servings], null)
emit([doc.totaltime, doc.actions[i].action, doc.servings, doc.ingredients[i].ingredient], null)
emit([doc.totaltime, doc.actions[i].action, doc.ingredients[i].ingredient, doc.servings], null)
emit([doc.totaltime, doc.servings, doc.actions[i].action, doc.ingredients[i].ingredient], null)
emit([doc.totaltime, doc.servings, doc.ingredients[i].ingredient, doc.actions[i].action], null)https://stackoverflow.com/questions/40472816
复制相似问题