繁体   English   中英

ES术语汇总,包括总计数,每个存储桶计数和过滤结果

[英]ES term aggregation with total count, count per bucket and filtered results

我对Elasticsearch还是很陌生,想使用术语聚合来完成以下工作:

  • 获取与类别和选定的规格(功能)匹配的产品列表
  • 规格和可能的值的列表,以及如果选择它会得到的产品数量
  • 规范列表应始终包含整个数据集的所有规范,即使其计数为0,但仅在所选类别内
  • 产品必须至少包含选定规范的选定值之一

这是我的映射:

{
  "mapping": {
    "product": {
      "properties": {
        "category": {
          "type": "keyword"
        },
        "features": {
          "properties": {
            "breedte": {
              "properties": {
                "id": {
                  "type": "long"
                },
                "measure": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            },
            "diepte": {
              "properties": {
                "id": {
                  "type": "long"
                },
                "measure": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            },
            "hoogte": {
              "properties": {
                "id": {
                  "type": "long"
                },
                "measure": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            }
          }
        },
        "name": {
          "type": "keyword"
        },
        "url": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

这是我目前拥有的(用2个值选择“ breedte”,用1个选择“ hoogte”)

GET _search 
{  
   "from": 0,
   "size": 15,
   "sort":{  
      "name":"desc"
   },
   "stored_fields":[  
      "_id",
      "name"
   ],
   "query":{  
      "constant_score":{  
         "filter":{  
            "bool":{  
               "must":[  
                  {  
                     "term":{  
                        "category":"Gaskookplaat"
                     }
                  },
                  {  
                     "term":{  
                        "features.hoogte.value":"55"
                     }
                  }
               ],
               "should":[  
                  {  
                     "term":{  
                        "features.breedte.value":"750"
                     }
                  },
                  {  
                     "term":{  
                        "features.breedte.value":"590"
                     }
                  }
               ]
            }
         }
      }
   },
   "aggs":{  
      "categories":{  
         "filter":{  
            "bool":{  
               "should":[  

               ]
            }
         },
         "aggs":{  
            "breedte":{  
               "terms":{  
                  "field":"features.breedte.value",
                  "min_doc_count":0
               },
               "meta":{  
                  "id":"id",
                  "measure":"mm",
                  "title":"Breedte"
               }
            },
            "hoogte":{  
               "terms":{  
                  "field":"features.hoogte.value",
                  "min_doc_count":0
               },
               "meta":{  
                  "value":"features.hoogte.value"
               }
            }
         }
      }
   }
}

结果

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 6,
    "successful": 5,
    "skipped": 0,
    "failed": 1,
    "failures": [
      {
        "shard": 0,
        "index": ".kibana",
        "node": "2fjW10M0RSa_imRTIMutww",
        "reason": {
          "type": "query_shard_exception",
          "reason": "No mapping found for [name] in order to sort on",
          "index_uuid": "x4687TCDRTalALulM9xLSg",
          "index": ".kibana"
        }
      }
    ]
  },
  "hits": {
    "total": 9,
    "max_score": null,
    "hits": [
      {
        "_index": "products",
        "_type": "product",
        "_id": "2014195",
        "_score": null,
        "sort": [
          "5ZTAKGF87"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2014049",
        "_score": null,
        "sort": [
          "5RVSAKGF87"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2023697",
        "_score": null,
        "sort": [
          "9ZTAKG95"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2009530",
        "_score": null,
        "sort": [
          "RVSAKG959"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2016478",
        "_score": null,
        "sort": [
          "RVSAKG675"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2015225",
        "_score": null,
        "sort": [
          "5IXBBGW7"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2010352",
        "_score": null,
        "sort": [
          "75IXBGW"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2018012",
        "_score": null,
        "sort": [
          "0IXBBGW6"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2009793",
        "_score": null,
        "sort": [
          "60IXBGW"
        ]
      }
    ]
  },
  "aggregations": {
    "categories": {
      "meta": {},
      "doc_count": 9,
      "hoogte": {
        "meta": {
          "value": "features.hoogte.value"
        },
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 55,
            "doc_count": 9
          },
          {
            "key": 5,
            "doc_count": 0
          },
          {
            "key": 6,
            "doc_count": 0
          },
          {
            "key": 8,
            "doc_count": 0
          },
          {
            "key": 10,
            "doc_count": 0
          },
          {
            "key": 13,
            "doc_count": 0
          },
          {
            "key": 16,
            "doc_count": 0
          },
          {
            "key": 18,
            "doc_count": 0
          },
          {
            "key": 30,
            "doc_count": 0
          },
          {
            "key": 32,
            "doc_count": 0
          }
        ]
      },
      "breedte": {
        "meta": {
          "measure": "mm",
          "id": "id",
          "title": "Breedte"
        },
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 750,
            "doc_count": 5
          },
          {
            "key": 590,
            "doc_count": 4
          },
          {
            "key": 70,
            "doc_count": 0
          },
          {
            "key": 83,
            "doc_count": 0
          },
          {
            "key": 85,
            "doc_count": 0
          },
          {
            "key": 94,
            "doc_count": 0
          },
          {
            "key": 98,
            "doc_count": 0
          },
          {
            "key": 110,
            "doc_count": 0
          },
          {
            "key": 120,
            "doc_count": 0
          },
          {
            "key": 165,
            "doc_count": 0
          }
        ]
      }
    }
  }
}

我认为我走在正确的轨道上,结果似乎还不错,但是每个规范的值是OR而不是AND,如果选择一个值,则规范的其他值将显示为0。 我想要的是将值添加到选择中时得到的计数。 我希望有人能指出我正确的方向。

我想我离现在有点近了。 我将所选规格与每个存储桶一起发送。 我现在还有以下两个问题:

  • 我无法过滤原始产品,因为选定规格的所有未选定值的存储桶计数将归零(请参见下文), 我发现我可以使用post_filter解决此post_filter
  • 如果该值是从该特定规范中唯一选择的值,那么我将获得计数。 但是,如果我在规范中选择2个值,则需要将这些数字相加

这就是我现在所拥有的。 关于如何进行的任何想法?

GET _search 
{  
   "from": 0,
   "size": 15,
   "sort":{  
      "name":"desc"
   },
   "stored_fields":[  
      "_id",
      "name"
   ],
   "query":{  
      "constant_score":{  
         "filter":{  
            "bool": {
               "must": [
                 {
                     "terms": {
                        "features.breedte.value": [
                           "1200"
                        ]
                     }
                  }
               ]
            }
         }
      }
   },
   "aggs":{  
     "filtered_hoogte": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "terms": {
                        "features.breedte.value": [
                           "1200"
                        ]
                     }
                  }
               ]
            }
         },
         "aggs": {
            "hoogte_aggs": {
               "terms": {
                  "field": "features.hoogte.value",
                  "min_doc_count": 0
               },
               "meta":{  
                  "value":"features.hoogte.value"
               }
            }
         }
      },
      "filtered_breedte": {
         "filter": {
            "bool": {
               "must": [
               ]
            }
         },
         "aggs": {
            "breedte_aggs": {
               "terms": {
                  "field": "features.hoogte.value",
                  "min_doc_count": 0
               },
               "meta":{  
                  "value":"features.hoogte.value"
               }
            }
         }
      }
   }
}

结果:

{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 6,
    "successful": 5,
    "skipped": 0,
    "failed": 1,
    "failures": [
      {
        "shard": 0,
        "index": ".kibana",
        "node": "2fjW10M0RSa_imRTIMutww",
        "reason": {
          "type": "query_shard_exception",
          "reason": "No mapping found for [name] in order to sort on",
          "index_uuid": "x4687TCDRTalALulM9xLSg",
          "index": ".kibana"
        }
      }
    ]
  },
  "hits": {
    "total": 210,
    "max_score": null,
    "hits": [
      {
        "_index": "products",
        "_type": "product",
        "_id": "2011475",
        "_score": null,
        "sort": [
          "0IXGBVFPS12"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2014465",
        "_score": null,
        "sort": [
          "20IXGVFPS1"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2014455",
        "_score": null,
        "sort": [
          "120IXBVFPS"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2013879",
        "_score": null,
        "sort": [
          "120IXVFPS"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2011478",
        "_score": null,
        "sort": [
          "20ANGBVFPS1"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2014466",
        "_score": null,
        "sort": [
          "20ANGVFPS1"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2014457",
        "_score": null,
        "sort": [
          "20ANBVFPS1"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2014444",
        "_score": null,
        "sort": [
          "120ANVFPS"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2009698",
        "_score": null,
        "sort": [
          "02IXGBVFPS12"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2011493",
        "_score": null,
        "sort": [
          "202IXGVFPS1"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2011491",
        "_score": null,
        "sort": [
          "02IXBVFPS12"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2012677",
        "_score": null,
        "sort": [
          "1202IXVFPS"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2009701",
        "_score": null,
        "sort": [
          "1202ANGBVFPS"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2009697",
        "_score": null,
        "sort": [
          "1202ANGVFPS"
        ]
      },
      {
        "_index": "products",
        "_type": "product",
        "_id": "2011492",
        "_score": null,
        "sort": [
          "1202ANBVFPS"
        ]
      }
    ]
  },
  "aggregations": {
    "filtered_breedte": {
      "meta": {},
      "doc_count": 210,
      "breedte_aggs": {
        "meta": {
          "value": "features.breedte.value"
        },
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 1200,
            "doc_count": 210
          },
          {
            "key": 70,
            "doc_count": 0
          },
          {
            "key": 83,
            "doc_count": 0
          },
          {
            "key": 85,
            "doc_count": 0
          },
          {
            "key": 94,
            "doc_count": 0
          },
          {
            "key": 98,
            "doc_count": 0
          },
          {
            "key": 110,
            "doc_count": 0
          },
          {
            "key": 120,
            "doc_count": 0
          },
          {
            "key": 165,
            "doc_count": 0
          },
          {
            "key": 270,
            "doc_count": 0
          }
        ]
      }
    },
    "filtered_hoogte": {
      "doc_count": 210,
      "hoogte_aggs": {
        "meta": {
          "value": "features.hoogte.value"
        },
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 18,
        "buckets": [
          {
            "key": 890,
            "doc_count": 40
          },
          {
            "key": 327,
            "doc_count": 4
          },
          {
            "key": 628,
            "doc_count": 3
          },
          {
            "key": 642,
            "doc_count": 3
          },
          {
            "key": 700,
            "doc_count": 3
          },
          {
            "key": 744,
            "doc_count": 3
          },
          {
            "key": 85,
            "doc_count": 2
          },
          {
            "key": 185,
            "doc_count": 2
          },
          {
            "key": 239,
            "doc_count": 2
          },
          {
            "key": 245,
            "doc_count": 2
          }
        ]
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM