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

这篇具有很好参考价值的文章主要介绍了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文章来源地址https://www.toymoban.com/news/detail-599281.html

到了这里,关于Elasticsearch多层嵌套nested查询和多条件聚合aggregations的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 6.ELK之Elasticsearch嵌套(Nested)类型

    在Elasticsearch实际应用中经常会遇到嵌套文档的情况,而且会有“对象数组彼此独立地进行索引和查询的诉求”。在ES中这种嵌套文档称为父子文档,父子文档“彼此独立地进行查询”至少有以下两种方式: 1)父子文档。在ES的5.x版本中通过parent-child父子type实现,即一个索引

    2024年02月03日
    浏览(44)
  • ElasticSearch第五讲 ES nested嵌套文档与父子文档处理

    在ES中,处理实体之间的关系并不像关系型存储那样明显。在关系数据库中的黄金准则 - 数据规范化,在ES中并不适用。在处理关联关系,嵌套对象和父子关联关系中,我们会讨论几种可行方案的优点和缺点。 紧接着在为可扩展性而设计中,我们会讨论ES提供的一些用来快速灵

    2024年02月02日
    浏览(37)
  • Elasticsearch 8.X 如何依据 Nested 嵌套类型的某个字段进行排序?

    这是来自社区的一个真实企业场景问题。 https://elasticsearch.cn/question/13135 如下所示, 希望在查出的结果后, 对结果进行后处理,对tags列表,根据depth进行排序。 Elasticsearch 能支持的排序方式罗列如下: 包含但不限于: 基于特定字段的排序 基于Nested对象字段的排序 基于特定

    2024年02月02日
    浏览(42)
  • SQL-条件查询与聚合函数的使用

     🎉欢迎您来到我的MySQL基础复习专栏 ☆* o(≧▽≦)o *☆哈喽~我是小小恶斯法克🍹 ✨博客主页: 小小恶斯法克的博客 🎈该系列文章专栏: 重拾MySQL 🍹文章作者技术和水平很有限,如果文中出现错误,希望大家能指正🙏 📜 感谢大家的关注! ❤️ 目录 DQL-条件查询 1.语法

    2024年01月23日
    浏览(30)
  • Elasticsearch--查询(nested、join)

    嵌套类型 数据的某个值是json、object对象;不再是简单的数据类型,或者简单数据类型的数组;那么还用之前的查询方式就有问题了。因为ES在存储复杂类型的时候会把对象的复杂层次结果扁平化为一个键值对列表 。此时,需要用nested进行查询 扁平化存储 ​  用法 使用nest

    2024年02月16日
    浏览(31)
  • MySQL学习笔记3——条件查询和聚合函数

    WHERE 和 HAVING 的区别: WHERE是直接对表中的字段进行限定,来筛选结果; HAVING则需要跟分组GROUP BY一起使用,通过对分组字段或分组计算函数进行限定,来筛选结果。 虽然它们都是对查询进行限定,却有着各自的特点和适用场景。 WHERE WHERE的特点是,直接用表的

    2024年04月16日
    浏览(23)
  • Spring Boot学习随笔- 集成MyBatis-Plus(二)条件查询QueryWrapper、聚合函数的使用、Lambda条件查询

    学习视频:【编程不良人】Mybatis-Plus整合SpringBoot实战教程,提高的你开发效率,后端人员必备! 普通查询 条件构造器查询 【重要】 AbstractWrapper  是 MyBatis Plus 中的一个抽象类,用于构建 SQL 查询条件。定义了泛型  T 、 C  和  Children 。其中, T  表示实体类的类型, C  表示查

    2024年02月04日
    浏览(39)
  • Elasticsearch 基本使用(二)简单查询 & 嵌套查询

    按id查询单条记录 查询所有数据 默认只查询10条记录 设置分页条件 设置排序 两种设置方式 注意:_id 为字符串,其排序方式为按位比较,并非数值大小 filter 过滤 在 bool 查询内,可以使用 filter 对数据进行过滤。 单从最终数据来看,filter 也是用于过滤数据,用于条件查询。

    2024年02月03日
    浏览(22)
  • 【JaveWeb教程】(20) MySQL数据库开发之 基本查询、条件查询、聚合函数、分组查询、排序查询、分页查询 详细代码示例讲解

    在上次学习的内容中,我们讲解了: 使用DDL语句来操作数据库以及表结构(数据库设计) 使用DML语句来完成数据库中数据的增、删、改操作(数据库操作) 我们今天还是继续学习数据库操作方面的内容:查询(DQL语句)。 查询操作我们分为两部分学习: DQL语句-单表操作

    2024年02月02日
    浏览(64)
  • Elasticsearch查询以及聚合查询

    must:返回的文档必须满足子句的条件,并且参与计算分值 filter:返回的文档必须满足filter子句的条件,不会参与计算分值 should:返回的文档可能满足should子句的条件。 must_nout:返回的文档必须不满足must_not定义的条件。 注意:如果一个查询既有filter又有should,那么至少包含

    2023年04月13日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包