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

相关推荐
kong79069282 小时前
Java-Intellij IDEA 自动导包设置
java·ide·intellij-idea
twj_one6 小时前
Arthas使用
java
lizz316 小时前
C++模板编程:从入门到精通
java·开发语言·c++
shoubepatien7 小时前
JAVA -- 05
java·开发语言
寰天柚子7 小时前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
memgLIFE7 小时前
Springboot 分层结构
java·spring boot·spring
shoubepatien7 小时前
JAVA -- 08
java·后端·intellij-idea
kong79069287 小时前
Java新特性-(二)Java基础语法
java·新特性·java 基础语法
yangminlei7 小时前
springboot pom.xml配置文件详细解析
java·spring boot·后端
黄俊懿7 小时前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——全局事务的提交
java·后端·spring·spring cloud·微服务·架构·架构师