当前位置: 首页 > ds >正文

【ElasticSearch】json查询语法

【ElasticSearch】json查询语法

  • 【一】Elasticsearch查询语法详解
    • 【1】基本查询结构
    • 【2】关键字
      • (1)一般查询(Query)
      • (2)指标聚合(Metric Aggregations)​​
      • (3)桶聚合(Bucket Aggregations)​​
      • (4)管道聚合(Pipeline Aggregations)​​
    • 【3】常用查询类型
      • (1)匹配查询-match
      • (2)多字段搜索-multi_match(在名称和描述中搜索)
      • (3)精确过滤-must(查找中国产电子产品)
      • (4)范围查询-range(价格在1000-5000之间的商品)
      • (5)组合查询(查找库存大于100的服装)
      • (6)分页排序(按价格降序排列)
      • (7)高亮显示(高亮搜索结果)
      • (8)布尔查询(Bool)
      • (9)地理位置搜索
    • 【4】聚合语法
      • (1)基本词项聚合-Terms(按商品分类分组)
      • (2)指标聚合-Metrics(计算平均价格)
      • (3)多级聚合-Metrics(按分类分组后计算平均价格)
      • (4)范围聚合-Range(按价格区间分组)
      • (5)复杂多级聚合(按产地分组后按分类分组)
      • (6)排序聚合-Order(按平均价格升序排列)
      • (7)嵌套聚合-Nested
      • (8)统计聚合-Stats
      • (9)扩展统计聚合-Extended Stats
  • 【二】完整查询与聚合组合案例
    • 【1】聚合案例
      • (1)案例1:带过滤条件的聚合分析
      • (2)案例2:高亮搜索与聚合结合
      • (3)案例3:多条件过滤聚合(中国产电子产品价格分析)
      • (4)案例4:日期范围聚合(按创建月份分组)
      • (5)案例5:指标聚合(Metric Aggregations)​​
      • (6)案例6:桶聚合(Bucket Aggregations)​​
      • (7)案例7:范围聚合(Range Aggregation)​​
      • (8)案例8:嵌套聚合(Nested Aggregation)​​
      • (9)​案例9:管道聚合(Pipeline Aggregation)​​
      • (10)案例10:组合查询和聚合
    • 【2】实践技巧
      • (1)性能优化
      • (2)复合聚合(处理大数据集)
      • (3)使用搜索模板
      • (4)使用索引别名
    • 【3】常见问题
      • (1)查询性能慢
      • (2)聚合桶数量不足
      • (3)处理空值聚合

【一】Elasticsearch查询语法详解

【1】基本查询结构

GET /index_name/_search
{"query": {// 查询条件},"aggs": {// 聚合条件},"sort": [// 排序条件],"from": 0,"size": 10,"highlight": {// 高亮设置}
}

【2】关键字

(1)一般查询(Query)

​匹配查询(Match Query)​​:用于全文搜索。
词条查询(Term Query)​​:用于精确值匹配。
​范围查询(Range Query)​​:用于范围过滤。
​布尔查询(Bool Query)​​:组合多个查询条件(must, should, must_not, filter)。
​多匹配查询(Multi Match Query)​​:在多个字段上执行匹配查询。
​前缀查询(Prefix Query)​​:匹配以指定前缀开头的词条。
​通配符查询(Wildcard Query)​​:使用通配符匹配。
​正则表达式查询(Regexp Query)​​:使用正则表达式匹配。
​模糊查询(Fuzzy Query)​​:匹配与指定词条相似的词条。
​嵌套查询(Nested Query)​​:查询嵌套对象。

(2)指标聚合(Metric Aggregations)​​

avg:平均值。
sum:求和。
min:最小值。
max:最大值。
count:计数。
stats:包含count, min, max, avg, sum。
extended_stats:扩展的统计信息,包括方差、标准差等。
cardinality:基数统计(类似distinct count)

(3)桶聚合(Bucket Aggregations)​​

terms:按词条分组。
range:按范围分组。
date_range:日期范围分组。
histogram:直方图(数值间隔分组)。
date_histogram:日期直方图。
nested:嵌套聚合。
reverse_nested:从嵌套聚合返回父级。
filter:按过滤条件分组。
filters:多个过滤条件分组。
missing:处理缺失字段。

(4)管道聚合(Pipeline Aggregations)​​

对其它聚合的结果进行再聚合。
avg_bucket:计算多个桶的平均值。
sum_bucket:计算多个桶的总和。
min_bucket:计算多个桶的最小值。
max_bucket:计算多个桶的最大值。

【3】常用查询类型

(1)匹配查询-match

基本匹配查询(查找所有华为手机)

GET /product_info/_search
{"query": {"match": {"product_name": "华为手机"}}
}

(2)多字段搜索-multi_match(在名称和描述中搜索)

GET /product_info/_search
{"query": {"multi_match": {"query": "防水 智能","fields": ["product_name", "description"],"type": "best_fields"}}
}

(3)精确过滤-must(查找中国产电子产品)

GET /product_info/_search
{"query": {"bool": {"must": [{"match": {"category": "电子产品"}}],"filter": [{"term": {"origin": "中国"}}]}}
}

(4)范围查询-range(价格在1000-5000之间的商品)

GET /product_info/_search
{"query": {"range": {"price": {"gte": 1000,"lte": 5000}}}
}

(5)组合查询(查找库存大于100的服装)

GET /product_info/_search
{"query": {"bool": {"must": [{"match": {"category": "服装"}}],"filter": [{"range": {"stock": {"gt": 100}}}]}}
}

(6)分页排序(按价格降序排列)

GET /product_info/_search
{"query": {"match_all": {}},"sort": [{"price": {"order": "desc"}}],"from": 0,"size": 10
}

(7)高亮显示(高亮搜索结果)

GET /product_info/_search
{"query": {"match": {"description": "防水"}},"highlight": {"fields": {"description": {}},"pre_tags": ["<strong>"],"post_tags": ["</strong>"]}
}

(8)布尔查询(Bool)

{"query": {"bool": {"must": [{ "match": { "field1": "value1" } }],"should": [{ "match": { "field2": "value2" } }],"must_not": [{ "term": { "field3": "value3" } }],"filter": [{ "range": { "price": { "gte": 100 } } }]}}
}
GET /product_info/_search
{"query": {"bool": {"must": [{ "match": { "product_name": "手机" } }],"filter": [{ "term": { "status": "active" } },{ "range": { "price": { "gte": 1000, "lte": 5000 } } }]}}
}

(9)地理位置搜索

GET /stores/_search
{"query": {"bool": {"must": [{ "match": { "name": "咖啡" } }],"filter": {"geo_distance": {"distance": "2km","location": {"lat": 31.2304,"lon": 121.4737}}}}},"sort": [{"_geo_distance": {"location": {"lat": 31.2304,"lon": 121.4737},"order": "asc","unit": "km"}}]
}

【4】聚合语法

(1)基本词项聚合-Terms(按商品分类分组)

GET /product_info/_search
{"size": 0,"aggs": {"by_category": {"terms": {"field": "category","size": 5}}}
}
GET /product_info/_search
{"query": {"bool": {"must": [{ "match": { "product_name": "智能手机" } }],"filter": [{ "term": { "status": "active" } },{ "range": { "price": { "gte": 1000, "lte": 5000 } } }]}},"sort": [{ "price": { "order": "asc" } }],"from": 0,"size": 10,"highlight": {"fields": {"product_name": {},"description": {}}}
}

(2)指标聚合-Metrics(计算平均价格)

GET /product_info/_search
{"size": 0,"aggs": {"avg_price": {"avg": {"field": "price"}}}
}
GET /product_info/_search
{"query": {"multi_match": {"query": "防水 智能","fields": ["product_name", "description"],"type": "best_fields"}},"aggs": {"by_category": {"terms": {"field": "category","size": 5},"aggs": {"avg_price": {"avg": { "field": "price" }}}},"price_stats": {"stats": { "field": "price" }}},"size": 0
}

(3)多级聚合-Metrics(按分类分组后计算平均价格)

GET /product_info/_search
{"size": 0,"aggs": {"by_category": {"terms": {"field": "category","size": 5},"aggs": {"avg_price": {"avg": {"field": "price"}}}}}
}
{"aggs": {"avg_price": {"avg": { "field": "price" }},"max_price": {"max": { "field": "price" }},"min_price": {"min": { "field": "price" }},"sum_price": {"sum": { "field": "price" }}}
}

(4)范围聚合-Range(按价格区间分组)

GET /product_info/_search
{"size": 0,"aggs": {"price_ranges": {"range": {"field": "price","ranges": [{"to": 1000},{"from": 1000, "to": 5000},{"from": 5000}]}}}
}
GET /product_info/_search
{"query": {"range": {"stock": {"gt": 0}}},"aggs": {"by_category_origin": {"terms": {"field": "category","size": 10},"aggs": {"by_origin": {"terms": {"field": "origin","size": 5,"order": { "avg_price": "asc" }},"aggs": {"avg_price": {"avg": { "field": "price" }},"price_ranges": {"range": {"field": "price","ranges": [{ "to": 1000 },{ "from": 1000, "to": 3000 },{ "from": 3000 }]}}}}}},"global_price_stats": {"extended_stats": { "field": "price" }}},"size": 0
}

(5)复杂多级聚合(按产地分组后按分类分组)

GET /product_info/_search
{"size": 0,"aggs": {"by_origin": {"terms": {"field": "origin","size": 5},"aggs": {"by_category": {"terms": {"field": "category","size": 3},"aggs": {"avg_price": {"avg": {"field": "price"}}}}}}}
}

(6)排序聚合-Order(按平均价格升序排列)

GET /product_info/_search
{"size": 0,"aggs": {"by_origin": {"terms": {"field": "origin","size": 5,"order": {"avg_price": "asc"}},"aggs": {"avg_price": {"avg": {"field": "price"}}}}}
}

(7)嵌套聚合-Nested

{"aggs": {"nested_agg": {"nested": {"path": "specifications"},"aggs": {"by_spec": {"terms": {"field": "specifications.name"}}}}}
}

(8)统计聚合-Stats

{"aggs": {"price_stats": {"stats": { "field": "price" }}}
}

(9)扩展统计聚合-Extended Stats

{"aggs": {"price_extended_stats": {"extended_stats": { "field": "price" }}}
}

【二】完整查询与聚合组合案例

【1】聚合案例

(1)案例1:带过滤条件的聚合分析

GET /product_info/_search
{"query": {"bool": {"filter": [{"term": {"status": "active"}},{"range": {"stock": {"gt": 0}}}]}},"size": 0,"aggs": {"by_category": {"terms": {"field": "category","size": 5},"aggs": {"by_origin": {"terms": {"field": "origin","size": 3,"order": {"avg_price": "asc"}},"aggs": {"avg_price": {"avg": {"field": "price"}},"min_price": {"min": {"field": "price"}},"max_price": {"max": {"field": "price"}}}}}},"global_stats": {"stats": {"field": "price"}}}
}

(2)案例2:高亮搜索与聚合结合

GET /product_info/_search
{"query": {"match": {"description": "智能"}},"highlight": {"fields": {"description": {}}},"aggs": {"price_stats": {"stats": {"field": "price"}}},"size": 5
}

(3)案例3:多条件过滤聚合(中国产电子产品价格分析)

GET /product_info/_search
{"query": {"bool": {"must": [{"match": {"category": "电子产品"}}],"filter": [{"term": {"origin": "中国"}},{"range": {"price": {"gte": 1000}}}]}},"size": 0,"aggs": {"by_brand": {"terms": {"field": "product_name.keyword","size": 5},"aggs": {"avg_price": {"avg": {"field": "price"}},"price_distribution": {"histogram": {"field": "price","interval": 1000}}}}}
}

(4)案例4:日期范围聚合(按创建月份分组)

GET /product_info/_search
{"size": 0,"aggs": {"by_month": {"date_histogram": {"field": "created_at","calendar_interval": "month","format": "yyyy-MM"},"aggs": {"total_sales": {"sum": {"field": "sales"}}}}}
}

(5)案例5:指标聚合(Metric Aggregations)​​

计算商品的平均价格、最高价格、最低价格:

GET /product_info/_search
{"size": 0,"aggs": {"avg_price": { "avg": { "field": "price" } },"max_price": { "max": { "field": "price" } },"min_price": { "min": { "field": "price" } }}
}

(6)案例6:桶聚合(Bucket Aggregations)​​

按商品类别分组,并计算每组的平均价格:

GET /product_info/_search
{"size": 0,"aggs": {"by_category": {"terms": { "field": "category" },"aggs": {"avg_price": { "avg": { "field": "price" } }}}}
}

(7)案例7:范围聚合(Range Aggregation)​​

按价格范围分组:

GET /product_info/_search
{"size": 0,"aggs": {"price_ranges": {"range": {"field": "price","ranges": [{ "to": 1000 },{ "from": 1000, "to": 5000 },{ "from": 5000 }]},"aggs": {"avg_rating": { "avg": { "field": "rating" } }}}}
}

(8)案例8:嵌套聚合(Nested Aggregation)​​

对嵌套的规格属性进行聚合,统计内存大小的分布:

GET /product_info/_search
{"size": 0,"aggs": {"specs": {"nested": { "path": "specifications" },"aggs": {"memory_sizes": {"filter": { "term": { "specifications.name": "内存" } },"aggs": {"sizes": { "terms": { "field": "specifications.value" } }}}}}}
}

(9)​案例9:管道聚合(Pipeline Aggregation)​​

按类别分组后,再计算每个类别的平均价格,然后按平均价格排序:

GET /product_info/_search
{"size": 0,"aggs": {"by_category": {"terms": {"field": "category","order": { "avg_price": "desc" }  // 按子聚合avg_price降序排序},"aggs": {"avg_price": { "avg": { "field": "price" } }}}}
}

(10)案例10:组合查询和聚合

查询价格在1000到5000之间的活跃商品,按类别分组,并计算每组的平均价格和商品数量,并按平均价格降序排序:

GET /product_info/_search
{"size": 0,"query": {"bool": {"filter": [{ "term": { "status": "active" } },{ "range": { "price": { "gte": 1000, "lte": 5000 } } }]}},"aggs": {"by_category": {"terms": {"field": "category","size": 10,"order": { "avg_price": "desc" }},"aggs": {"avg_price": { "avg": { "field": "price" } },"product_count": { "value_count": { "field": "id" } }}}}
}

【2】实践技巧

(1)性能优化

GET /product_info/_search
{"query": {"bool": {"filter": [{"term": {"category": "电子产品"}}]}},"size": 0,"aggs": {"by_origin": {"terms": {"field": "origin","execution_hint": "map",  -- 使用map执行模式"size": 5}}}
}

(2)复合聚合(处理大数据集)

GET /product_info/_search
{"size": 0,"aggs": {"by_category_origin": {"composite": {"sources": [{"category": {"terms": {"field": "category"}}},{"origin": {"terms": {"field": "origin"}}}],"size": 100},"aggs": {"avg_price": {"avg": {"field": "price"}}}}}
}

(3)使用搜索模板

POST /_scripts/product_search_template
{"script": {"lang": "mustache","source": {"query": {"bool": {"must": [{"match": {"category": "{{category}}"}}],"filter": [{"term": {"origin": "{{origin}}"}}]}},"aggs": {"price_stats": {"stats": {"field": "price"}}},"size": "{{size}}"}}
}-- 调用模板
GET /product_info/_search/template
{"id": "product_search_template","params": {"category": "电子产品","origin": "中国","size": 10}
}

(4)使用索引别名

-- 创建别名
POST /_aliases
{"actions": [{"add": {"index": "product_info","alias": "current_products","filter": {"term": {"status": "active"}}}}]
}-- 使用别名查询
GET /current_products/_search
{"query": {"match_all": {}}
}

【3】常见问题

(1)查询性能慢

​优化方案​:添加路由和过滤条件

GET /product_info/_search?routing=category
{"query": {"bool": {"filter": [{"term": {"category": "电子产品"}}]}}
}

(2)聚合桶数量不足

​解决方案​:增加size参数

GET /product_info/_search
{"size": 0,"aggs": {"by_category": {"terms": {"field": "category","size": 20  -- 默认是10}}}
}

(3)处理空值聚合

​解决方案​:使用missing参数

GET /product_info/_search
{"size": 0,"aggs": {"by_origin": {"terms": {"field": "origin","missing": "未知产地"}}}
}
http://www.xdnf.cn/news/18944.html

相关文章:

  • 美团一面“保持好奇”
  • Spring Boot 项目打包成可执行程序
  • HTML应用指南:利用POST请求获取全国三星门店位置信息
  • Ubuntu安装及配置Git(Ubuntu install and config Git Tools)
  • Next.js 15.5.0:探索 Turbopack Beta、稳定的 Node.js 中间件和 TypeScript 的改进
  • 30.throw抛异常
  • 【图像算法 - 23】工业应用:基于深度学习YOLO12与OpenCV的仪器仪表智能识别系统
  • 【P2P】P2P主要技术及RELAY服务1:python实现
  • Kubernetes 构建高可用、高性能 Redis 集群
  • 线性回归入门:从原理到实战的完整指南
  • k8sday17安全机制
  • 真实应急响应案例记录
  • 一键终结Win更新烦恼!你从未见过如此强大的更新暂停工具!
  • PNP机器人介绍:全球知名具身智能/AI机器人实验室介绍之多伦多大学机器人研究所
  • PC端逆向会用到的常见伪指令
  • 解读 “货位清则标识明,标识明则管理成” 的实践价值
  • 灰狼算法+四模型对比!GWO-CNN-BiLSTM-Attention系列四模型多变量时序预测
  • EasyClick 生成唯一设备码
  • 【CV】图像基本操作——①图像的IO操作
  • XC95144XL-10TQG144I Xilinx XC9500XL 高性能 CPLD
  • 从0到1:用 Qwen3-Coder 和 高德MCP 助力数字文旅建造——国庆山西游
  • 我的小灶坑
  • Web程序设计
  • 《 nmcli网络管理学习》
  • 28 FlashAttention
  • sudo 升级
  • 牛客周赛 Round 106(小苯的方格覆盖/小苯的数字折叠/ 小苯的波浪加密器/小苯的数字变换/小苯的洞数组构造/ 小苯的数组计数)
  • “华生科技杯”2025年全国青少年龙舟锦标赛在海宁举行
  • Linux网络套接字
  • 0825 http梳理作业