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

相关推荐
Drifter_yh3 小时前
【黑马点评】Redisson 分布式锁核心原理剖析
java·数据库·redis·分布式·spring·缓存
莫寒清4 小时前
Spring MVC:@RequestParam 注解详解
java·spring·mvc
没有医保李先生5 小时前
字节对齐的总结
java·开发语言
Elastic 中国社区官方博客5 小时前
使用 Elastic 进行网络监控:统一网络可观测性
大数据·开发语言·网络·人工智能·elasticsearch·搜索引擎·全文检索
甲枫叶6 小时前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
海兰6 小时前
Elasticsearch 9.x 借助神经模型优化中文文本分析
大数据·elasticsearch·搜索引擎
六件套是我6 小时前
无法访问org.springframeword.beans.factory.annotation.Value
java·开发语言·spring boot
LYS_06187 小时前
C++学习(5)(函数 指针 引用)
java·c++·算法
forestsea7 小时前
Spring Cloud Alibaba 2025.1.0.0 正式发布:拥抱 Spring Boot 4.0 与 Java 21+ 的新时代
java·spring boot·后端