如何在Elasticsearch中设置父子聚合?

时间:2018-04-12 05:26:56

标签: elasticsearch

使用父子映射捕获产品和子项以覆盖产品。

我们使用父子组合在Elasticsearch中存储产品目录。用户可以覆盖主产品并存储为子产品。我在聚合方面有问题。当我们在那时汇总数据时,它将根据父子项给出行号的所有记录计数。

如果孩子有行名,则父文件将被排除,子文件没有行名称,则行名称从父记录中获取。

PUT /pdm
{
  "mappings": {
    "product": {
    "properties": {
           "product_name": {
              "type": "text",
              "fielddata": true,
              "fields": {
                 "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                 }
              }
           },
           "linename": {
              "type": "text",
              "fielddata": true,
              "fields": {
                 "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                 }
              }
           }
        }
    },
    "dist_product": {
      "_parent": {
    "type": "product" 
      },
      "properties": {
    "product_name": {
              "type": "text",
              "fielddata": true,
              "fields": {
                 "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                 }
              }
           },
           "linename": {
              "type": "text",
              "fielddata": true,
              "fields": {
                 "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                 }
              }
           }       
       }
    }
  }
}

指数3父母和几个孩子。

POST /pdm/product/_bulk
{ "index": { "_id": "101" }}
{ "product_name": "Zip-It Document Holder", "linename": "Sweda"}
{ "index": { "_id": "102" }}
{ "product_name": "Stratus Reversible Umbrella", "linename": "Sweda"}
{ "index": { "_id": "103" }}
{ "product_name": "Leatherette Tumbler", "linename": "Sweda"}

儿童

POST /pdm/dist_product/_bulk
{ "index": { "_id": 1, "parent": "101" }}
{ "linename": "Greco" }
{ "index": { "_id": 2, "parent": "102" }}
{ "linename": "Greco"}

聚合查询

POST /pdm/_search
{  
   "size": 0, 
   "aggs":{  
      "my-linename":{  
       "terms":{  
          "field":"linename"
       }
    }
   }
}

结果如下

"aggregations": {
  "my-linename": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": "sweda",
           "doc_count": 3
        },
        {
           "key": "greco",
           "doc_count": 2
        }
     ]
  }
}

预期结果应如下所示

"aggregations": {
    "my-linename": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": "sweda",
           "doc_count": 1
        },
        {
           "key": "greco",
           "doc_count": 2
        }
     ]
    }
}

请提出一些建议。

0 个答案:

没有答案