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

相关推荐
五岳1 天前
分库分表数据源ShardingSphereDataSource的Connection元数据误用问题分析
java·mysql·爬坑
带刺的坐椅1 天前
迈向 MCP 集群化:Solon AI (支持 Java8+)在解决 MCP 服务可扩展性上的探索与实践
java·ai·llm·solon·mcp
鼠爷ねずみ1 天前
SpringCloud前后端整体开发流程-以及技术总结文章实时更新中
java·数据库·后端·spring·spring cloud
代码or搬砖1 天前
String字符串
android·java·开发语言
Elastic 中国社区官方博客1 天前
Elasticsearch:圣诞晚餐 BBQ - 图像识别
大数据·数据库·elasticsearch·搜索引擎·ai·全文检索
AM越.1 天前
Java设计模式详解--装饰器设计模式(含uml图)
java·设计模式·uml
5980354151 天前
【java工具类】小数、整数转中文大写
android·java·开发语言
努力成为一个程序猿.1 天前
1.ElasticSearch单节点部署
大数据·elasticsearch·搜索引擎
JIngJaneIL1 天前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot