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
    }
    }
    }
    }

相关推荐
花哥码天下37 分钟前
apifox登录后设置token到环境变量
java·后端
浩瀚地学1 小时前
【Java】常用API(二)
java·开发语言·经验分享·笔记·学习
hashiqimiya2 小时前
springboot事务触发滚动与不滚蛋
java·spring boot·后端
PPPHUANG2 小时前
一次 CompletableFuture 误用,如何耗尽 IO 线程池并拖垮整个系统
java·后端·代码规范
恩创软件开发2 小时前
创业日常2026-1-8
java·经验分享·微信小程序·小程序
想用offer打牌3 小时前
一站式了解Spring AI Alibaba的流式输出
java·人工智能·后端
Lonely丶墨轩3 小时前
从登录入口窥见架构:一个企业级双Token认证系统的深度拆解
java·数据库·sql
掘根4 小时前
【仿Muduo库项目】EventLoop模块
java·开发语言