Elasticsearch一些函数查询

  1. 根据价格分组统计数量,每组区间为2000,

filter_path=aggregations 设置查询结果只展示函数结果

也有date_histogram函数根据日期分组等等

复制代码
GET order/_search?filter_path=aggregations
{
  "aggs": {
    "hist_price": {
      "histogram": {
        "field": "price",
        "interval": 2000,
        # "min_doc_count": 1  # 设置只有数量大于1的才会展示
      }
    }
  }
}
查询结果:
{
  "aggregations" : {
    "hist_price" : {
      "buckets" : [
        {
          "key" : 0.0,
          "doc_count" : 1
        },
        {
          "key" : 2000.0,
          "doc_count" : 4
        },
        {
          "key" : 4000.0,
          "doc_count" : 0
        },
        {
          "key" : 6000.0,
          "doc_count" : 1
        }
      ]
    }
  }
}
  1. 查询20%之内,50%之内,100%之内的价格都在多少钱之下

查询结果为近似值跟ES的算法有关

复制代码
GET order/_search?filter_path=aggregations
{
  "aggs": {
    "percent_price": {
      "percentiles": {
        "field": "price",
        "percents": [
          20,
          50,
          100
        ]
      }
    }
  }
}
查询结果:
{
  "aggregations" : {
    "percent_price" : {
      "values" : {
        "20.0" : 1700.0000000000002,
        "50.0" : 2500.0,
        "100.0" : 6000.0
      }
    }
  }
}
  1. 查询2的相反情况,例:查询2000,和 6000之内的占比

    GET order/_search?filter_path=aggregations
    {
    "aggs": {
    "percent_price": {
    "percentile_ranks": {
    "field": "price",
    "values": [
    2000,
    6000
    ]
    }
    }
    }
    }
    查询结果:
    {
    "aggregations" : {
    "percent_price" : {
    "values" : {
    "2000.0" : 16.666666666666664,
    "6000.0" : 82.73866923818709
    }
    }
    }
    }

相关推荐
日月云棠14 小时前
各版本JDK对比:JDK 25 特性详解
java
用户83071968408214 小时前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide15 小时前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家15 小时前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺15 小时前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户9083246027315 小时前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端
桦说编程16 小时前
实战分析 ConcurrentHashMap.computeIfAbsent 的锁冲突问题
java·后端·性能优化
程序员清风20 小时前
用了三年AI,我总结出高效使用AI的3个习惯!
java·后端·面试
beata21 小时前
Java基础-13: Java反射机制详解:原理、使用与实战示例
java·后端
用户03321266636721 小时前
Java 使用 Spire.Presentation 在 PowerPoint 中添加或删除表格行与列
java