Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB聚合式分面搜索_Mongodb_Facet_Faceted Search - Fatal编程技术网

MongoDB聚合式分面搜索

MongoDB聚合式分面搜索,mongodb,facet,faceted-search,Mongodb,Facet,Faceted Search,以集合为例,如下所示: db.products.insert([ {"product_name": "Product 1", "filters": [{"name": "brand", "value": "Brand 1"}, {"name": "color", "value": "Color 1"}]}, {"product_name": "Product 2", "filters": [{"name": "brand", "value": "Brand 2"}, {"name

以集合为例,如下所示:

db.products.insert([
    {"product_name": "Product 1", "filters": [{"name": "brand", "value": "Brand 1"}, {"name": "color", "value": "Color 1"}]},
    {"product_name": "Product 2", "filters": [{"name": "brand", "value": "Brand 2"}, {"name": "color", "value": "Color 2"}]},
    {"product_name": "Product 3", "filters": [{"name": "brand", "value": "Brand 1"}, {"name": "color", "value": "Color 2"}]},
    {"product_name": "Product 4", "filters": [{"name": "brand", "value": "Brand 3"}, {"name": "color", "value": "Color 1"}]},
    {"product_name": "Product 5", "filters": [{"name": "brand", "value": "Brand 3"}, {"name": "color", "value": "Color 3"}]}
])
如果必须在字典列表上进行聚合,并获得每个字典的分组计数,如下所示:

(品牌)
品牌1:2
品牌2:1
品牌3:2

(彩色)
颜色1:2
颜色2:2
颜色3:3

获取此信息的相关方法是什么?

您必须通过数据获取平面数据集,而不是通过筛选值

db.products.aggregate(
{
    $unwind: "$filters",
}, {
    $group: { 
        _id: "$filters.value",
        product_names: { $push: "$product_name" } ,
        count: { $sum: 1 }
    }
});
返回

{
    "result" : [ 
        {
            "_id" : "Color 3",
            "product_names" : [ 
                "Product 5"
            ],
            "count" : 1
        }, 
        {
            "_id" : "Brand 3",
            "product_names" : [ 
                "Product 4", 
                "Product 5"
            ],
            "count" : 2
        }, 
        {
            "_id" : "Color 2",
            "product_names" : [ 
                "Product 2", 
                "Product 3"
            ],
            "count" : 2
        }, 
        {
            "_id" : "Color 1",
            "product_names" : [ 
                "Product 1", 
                "Product 4"
            ],
            "count" : 2
        }, 
        {
            "_id" : "Brand 2",
            "product_names" : [ 
                "Product 2"
            ],
            "count" : 1
        }, 
        {
            "_id" : "Brand 1",
            "product_names" : [ 
                "Product 1", 
                "Product 3"
            ],
            "count" : 2
        }
    ],
    "ok" : 1
}

按如下方式使用此聚合:

db.products.aggregate({
  "$unwind": "$filters"
}, {
  "$group": {
    "_id": {
      "value": "$filters.value",
      "name": "$filters.name"
    },
    "count": {
      "$sum": 1
    }
  }
}, {
  "$group": {
    "_id": "$_id.name",
    "data": {
      "$push": {
        "value": "$_id.value",
        "totalCount": "$count"
      }
    }
  }
}).pretty()

可能是@BlakesSeven的复制品:可能不是。你提供的链接是非常具体的猫鼬这是完美的工作,根据我原来的查询!你知道如何使用与solr中相同的上述数据获得类似于multi-select的镶嵌面吗