1

I've a list of documents (operators) and they have devices. Each of these devices has or more more tags. I want to create a unique list of tags for each operator.

Operator1: Device1 tag_1 tag_2 Device2 tag_1 tag_3

Operator2: Device1 tag_4 tag_2 Device2 tag_1 tag_3

I want the output to be like

    Operator1 -> Tags: [tag_1,tag_2,tag_3]
    Operator2 -> Tags: [tag_1,tag_2,tag_3,tag_4]

How do I do this using a view (map reduce) ? I was able to emit the documents using the below mapper.

    function(doc) {
        if (doc.key && doc.operator && doc.tags) {
            for (tag in doc.tags) {
              emit(doc.operator, tag.toLowerCase());
            }
         }
    }

This emits

    Key           Value
    Operator1 - > tag_1
    Operator1 - > tag_2
    Operator1 - > tag_1
    Operator1 - > tag_3
    Operator2 - > tag_1
    Operator2 - > tag_2
    Operator2 - > tag_3
    Operator2 - > tag_4

I couldn't figure how to group them to:

    Operator1 -> Tags: [tag_1,tag_2,tag_3]
    Operator2 -> Tags: [tag_1,tag_2,tag_3,tag_4]
4

2 回答 2

2

您可以使用 reduce 函数来完成此操作:

function (keys, values, rereduce) {
  var tags = {};
  values.forEach(function(tag) {
    if (!tags[tag]) tags[tag] = true;
  });
  return Object.keys(tags).sort();
}

这将迭代values(即:“标签”)并返回它们的唯一列表。

一般来说,reduce 函数的输出应该非常小,比如简单的数字。根据数据集的大小和多样性,您可能需要配置服务器以允许更大的 reduce 输出。

于 2015-01-30T16:33:58.390 回答
0

添加减少功能。

function(keys, values) {
   return sum(values);
}

然后使用参数 group=true 进行查询。

于 2015-06-03T14:48:02.803 回答