Elasticsearch多层嵌套nested查询和多条件聚合aggregations

 当在aggregations中使用嵌套多条件,需要用bool包起来,如果直接在filters[]里写,将会分多个桶,分别按单个条件统计;

 部分片段截取:

"filters": {
            "filters": [
              {
                "bool": {
                  "filter": [
                    {
                      "terms": {
                        "timePeriodList.timePeriod": [
                          0
                        ],
                        "boost": 1
                      }
                    },
                    {
                      "range": {
                        "timePeriodList.freeDuration": {
                          "from": 60,
                          "to": null,
                          "include_lower": true,
                          "include_upper": true,
                          "boost": 1
                        }
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              }
            ]
}

当需要将多层嵌套nested用多个条件AND起来时:

{
          "nested": {
            "query": {
              "bool": {
                "filter": [
                  {
                    "terms": {
                      "timePeriodList.timePeriod": [
                        0
                      ],
                      "boost": 1
                    }
                  },
                  {
                    "range": {
                      "timePeriodList.freeDuration": {
                        "from": 60,
                        "to": null,
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "nested": {
                      "query": {
                        "bool": {
                          "filter": [
                            {
                              "term": {
                                "timePeriodList.skuPriceList.priceDuration": {
                                  "value": 60,
                                  "boost": 1
                                }
                              }
                            },
                            {
                              "term": {
                                "timePeriodList.skuPriceList.standardType": {
                                  "value": "CPT",
                                  "boost": 1
                                }
                              }
                            }
                          ],
                          "adjust_pure_negative": true,
                          "boost": 1
                        }
                      },
                      "path": "timePeriodList.skuPriceList",
                      "ignore_unmapped": false,
                      "score_mode": "none",
                      "boost": 1
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1
              }
            },
            "path": "timePeriodList",
            "ignore_unmapped": false,
            "score_mode": "none",
            "boost": 1
          }
        }
}

全部片段:

GET entity_point_sku/_search
{
  "from": 0,
  "size": 0,
  "timeout": "60s",
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "operatorType": {
              "value": "CINEMA",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "saleModel": {
              "value": "SPLIT",
              "boost": 1
            }
          }
        },
        {
          "range": {
            "pointDate": {
              "from": "2022-09-10",
              "to": "2022-09-10",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "freeDuration": {
              "from": 60,
              "to": null,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "terms": {
            "city": [
              "440400"
            ],
            "boost": 1
          }
        },
        {
          "term": {
            "resourceEntityInfoId": {
              "value": "be6de436aaa2404d93517dd64ff1facb",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "resourcePointId": {
              "value": "1a4a487bc5e54b8f9d592030012d6ad5",
              "boost": 1
            }
          }
        },
        {
          "prefix": {
            "pointNumber": {
              "value": "A",
              "boost": 1
            }
          }
        },
        {
          "nested": {
            "query": {
              "bool": {
                "filter": [
                  {
                    "terms": {
                      "timePeriodList.timePeriod": [
                        0
                      ],
                      "boost": 1
                    }
                  },
                  {
                    "range": {
                      "timePeriodList.freeDuration": {
                        "from": 60,
                        "to": null,
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "nested": {
                      "query": {
                        "bool": {
                          "filter": [
                            {
                              "term": {
                                "timePeriodList.skuPriceList.priceDuration": {
                                  "value": 60,
                                  "boost": 1
                                }
                              }
                            },
                            {
                              "term": {
                                "timePeriodList.skuPriceList.standardType": {
                                  "value": "CPT",
                                  "boost": 1
                                }
                              }
                            }
                          ],
                          "adjust_pure_negative": true,
                          "boost": 1
                        }
                      },
                      "path": "timePeriodList.skuPriceList",
                      "ignore_unmapped": false,
                      "score_mode": "none",
                      "boost": 1
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1
              }
            },
            "path": "timePeriodList",
            "ignore_unmapped": false,
            "score_mode": "none",
            "boost": 1
          }
        }
      ],
      "must_not": [
        {
          "terms": {
            "antiIndustry": [
              "MEDICAL_KQK"
            ],
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "aggregations": {
    "resourcePointId_Count": {
      "value_count": {
        "field": "resourcePointId"
      }
    },
    "pointDateReachTime_Sum": {
      "sum": {
        "field": "pointDateReachTime"
      }
    },
    "timePeriodList_Name": {
      "nested": {
        "path": "timePeriodList"
      },
      "aggregations": {
        "timePeriodList_FilterAgg": {
          "filters": {
            "filters": [
              {
                "bool": {
                  "filter": [
                    {
                      "terms": {
                        "timePeriodList.timePeriod": [
                          0
                        ],
                        "boost": 1
                      }
                    },
                    {
                      "range": {
                        "timePeriodList.freeDuration": {
                          "from": 60,
                          "to": null,
                          "include_lower": true,
                          "include_upper": true,
                          "boost": 1
                        }
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              }
            ],
            "other_bucket": false,
            "other_bucket_key": "_other_"
          },
          "aggregations": {
            "timePeriodList.skuPriceList_Name": {
              "nested": {
                "path": "timePeriodList.skuPriceList"
              },
              "aggregations": {
                "timePeriodList.skuPriceList_FilterAgg": {
                  "filters": {
                    "filters": [
                      {
                        "bool": {
                          "filter": [
                            {
                              "term": {
                                "timePeriodList.skuPriceList.priceDuration": {
                                  "value": 60,
                                  "boost": 1
                                }
                              }
                            },
                            {
                              "term": {
                                "timePeriodList.skuPriceList.standardType": {
                                  "value": "CPT",
                                  "boost": 1
                                }
                              }
                            }
                          ],
                          "adjust_pure_negative": true,
                          "boost": 1
                        }
                      }
                    ],
                    "other_bucket": false,
                    "other_bucket_key": "_other_"
                  },
                  "aggregations": {
                    "salePrice_SUM": {
                      "sum": {
                        "field": "timePeriodList.skuPriceList.salePrice"
                      }
                    },
                    "stickerPrice_SUM": {
                      "sum": {
                        "field": "timePeriodList.skuPriceList.stickerPrice"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Elasticsearch 多层嵌套nested 多条件聚合aggregations

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值