Elasticsearch 7.X DSL 入门教程
基础概念什么是 DSLDSL (Domain Specific Language) 是 Elasticsearch 的查询语言基于 JSON 格式用于执行各种数据操作。核心概念{ index: 索引名, // 类似数据库 type: 类型名, // 7.x 已废弃统一为 _doc id: 文档ID, // 类似主键 document: 文档内容 // 类似记录 }索引操作创建索引PUT /products { settings: { number_of_shards: 3, number_of_replicas: 1 }, mappings: { properties: { name: { type: text, analyzer: ik_max_word }, price: { type: double }, category: { type: keyword }, description: { type: text, analyzer: ik_max_word }, stock: { type: integer }, created_at: { type: date, format: yyyy-MM-dd HH:mm:ss }, is_active: { type: boolean } } } }查看索引GET /products GET /products/_mapping GET /_cat/indices?v删除索引DELETE /products文档操作创建文档POST /products/_doc { name: iPhone 15 Pro, price: 7999, category: 手机, description: 苹果最新款智能手机, stock: 100, created_at: 2024-01-15 10:00:00, is_active: true }创建指定 ID 文档PUT /products/_doc/1 { name: MacBook Pro, price: 12999, category: 电脑, description: 苹果专业笔记本电脑, stock: 50, created_at: 2024-01-15 11:00:00, is_active: true }批量创建文档POST /_bulk { index: { _index: products, _id: 2 } } { name: iPad Air, price: 4399, category: 平板, description: 轻薄平板电脑, stock: 80, created_at: 2024-01-15 12:00:00, is_active: true } { index: { _index: products, _id: 3 } } { name: AirPods Pro, price: 1899, category: 耳机, description: 无线降噪耳机, stock: 200, created_at: 2024-01-15 13:00:00, is_active: true } { index: { _index: products, _id: 4 } } { name: Apple Watch, price: 2999, category: 手表, description: 智能手表, stock: 150, created_at: 2024-01-15 14:00:00, is_active: true }查询文档GET /products/_doc/1更新文档POST /products/_update/1 { doc: { price: 11999, stock: 45 } }删除文档DELETE /products/_doc/1基础查询查询所有文档GET /products/_search { query: { match_all: {} } }分页查询GET /products/_search { query: { match_all: {} }, from: 0, size: 10 }指定返回字段GET /products/_search { query: { match_all: {} }, _source: [name, price, category] }排序GET /products/_search { query: { match_all: {} }, sort: [ { price: { order: desc } } ] }查询类型详解1. Match Query全文搜索GET /products/_search { query: { match: { name: 苹果 手机 } } }2. Term Query精确匹配GET /products/_search { query: { term: { category: 手机 } } }3. Terms Query多值精确匹配GET /products/_search { query: { terms: { category: [手机, 电脑] } } }4. Range Query范围查询GET /products/_search { query: { range: { price: { gte: 1000, lte: 10000 } } } }范围操作符gt: 大于gte: 大于等于lt: 小于lte: 小于等于5. Prefix Query前缀匹配GET /products/_search { query: { prefix: { name: iPh } } }6. Wildcard Query通配符匹配GET /products/_search { query: { wildcard: { name: *Pro* } } }7. Fuzzy Query模糊查询GET /products/_search { query: { fuzzy: { name: { value: iPone, fuzziness: 2 } } } }8. Bool Query布尔查询GET /products/_search { query: { bool: { must: [ { match: { name: 苹果 } } ], must_not: [ { term: { category: 耳机 } } ], should: [ { range: { price: { lte: 5000 } } } ], filter: [ { term: { is_active: true } } ] } } }Bool 子句说明must: 必须匹配影响得分must_not: 必须不匹配should: 应该匹配影响得分filter: 必须匹配不影响得分复合查询1. Multi Match Query多字段搜索GET /products/_search { query: { multi_match: { query: 苹果, fields: [name, description] } } }2. Query String Query查询字符串GET /products/_search { query: { query_string: { fields: [name, description], query: (苹果 AND 手机) OR (iPad) } } }3. Nested Bool Query嵌套布尔查询GET /products/_search { query: { bool: { must: [ { bool: { should: [ { match: { name: 苹果 } }, { match: { description: 苹果 } } ] } }, { range: { price: { gte: 1000 } } } ] } } }聚合查询1. Terms Aggregation词项聚合GET /products/_search { size: 0, aggs: { category_count: { terms: { field: category, size: 10 } } } }2. Stats Aggregation统计聚合GET /products/_search { size: 0, aggs: { price_stats: { stats: { field: price } } } }返回count、min、max、avg、sum3. Range Aggregation范围聚合GET /products/_search { size: 0, aggs: { price_ranges: { range: { field: price, ranges: [ { to: 2000 }, { from: 2000, to: 5000 }, { from: 5000 } ] } } } }4. Date Histogram Aggregation日期直方图GET /products/_search { size: 0, aggs: { products_over_time: { date_histogram: { field: created_at, calendar_interval: day, format: yyyy-MM-dd } } } }5. Bucket Aggregation Metric Aggregation桶聚合 指标聚合GET /products/_search { size: 0, aggs: { by_category: { terms: { field: category }, aggs: { average_price: { avg: { field: price } }, max_price: { max: { field: price } } } } } }6. Filter Aggregation过滤聚合GET /products/_search { size: 0, aggs: { active_products: { filter: { term: { is_active: true } }, aggs: { avg_price: { avg: { field: price } } } } } }高级功能1. Highlight高亮显示GET /products/_search { query: { match: { name: 苹果 } }, highlight: { fields: { name: {}, description: {} } } }2. Script Fields脚本字段GET /products/_search { query: { match_all: {} }, script_fields: { discount_price: { script: { source: doc[price].value * 0.9 } } } }3. Source Filtering源过滤GET /products/_search { query: { match_all: {} }, _source: { includes: [name, price], excludes: [description] } }4. Explain解释查询GET /products/_search { query: { match: { name: 苹果 } }, explain: true }5. Profile性能分析GET /products/_search { profile: true, query: { match: { name: 苹果 } } }实战示例示例 1: 电商商品搜索GET /products/_search { query: { bool: { must: [ { multi_match: { query: 苹果, fields: [name^2, description], type: best_fields } } ], filter: [ { term: { is_active: true } }, { range: { price: { gte: 1000, lte: 15000 } } } ] } }, aggs: { categories: { terms: { field: category, size: 10 } }, price_ranges: { range: { field: price, ranges: [ { to: 2000, key: 低价 }, { from: 2000, to: 5000, key: 中价 }, { from: 5000, key: 高价 } ] } } }, sort: [ { _score: { order: desc } } ], from: 0, size: 20, highlight: { fields: { name: {}, description: {} }, pre_tags: [em], post_tags: [/em] } }示例 2: 日志分析GET /logs/_search { query: { bool: { must: [ { range: { timestamp: { gte: now-24h, lte: now } } } ], filter: [ { term: { level: ERROR } } ] } }, aggs: { errors_by_service: { terms: { field: service_name, size: 20 }, aggs: { error_types: { terms: { field: error_type, size: 10 } }, timeline: { date_histogram: { field: timestamp, calendar_interval: 1h } } } } }, sort: [ { timestamp: { order: desc } } ], size: 100 }示例 3: 用户行为分析GET /user_actions/_search { query: { bool: { must: [ { range: { timestamp: { gte: now-7d/d, lte: now/d } } } ] } }, aggs: { daily_stats: { date_histogram: { field: timestamp, calendar_interval: day }, aggs: { unique_users: { cardinality: { field: user_id } }, action_types: { terms: { field: action_type } } } }, top_users: { terms: { field: user_id, size: 10, order: { action_count: desc } }, aggs: { action_count: { value_count: { field: _id } } } } } }示例 4: 复杂的多条件查询GET /products/_search { query: { bool: { must: [ { bool: { should: [ { match: { name: 手机 } }, { match: { description: 手机 } } ], minimum_should_match: 1 } } ], must_not: [ { term: { category: 配件 } } ], should: [ { range: { price: { lte: 3000 } } }, { term: { stock: { value: 0 } } } ], filter: [ { term: { is_active: true } }, { range: { created_at: { gte: now-30d } } } ] } }, aggs: { category_stats: { terms: { field: category }, aggs: { price_stats: { stats: { field: price } }, stock_stats: { stats: { field: stock } } } } }, sort: [ { price: { order: asc } }, { _score: { order: desc } } ], from: 0, size: 20 }常用操作符和参数查询参数参数说明from起始位置size返回数量timeout超时时间track_total_hits跟踪总命中数request_cache请求缓存排序参数参数说明order排序方向asc/descmode排序模式min/max/sum/avg/medianmissing缺失值处理_last/_first聚合参数参数说明size返回桶的数量order排序方式min_doc_count最小文档数性能优化建议1. 使用 filter 而非 query// 不推荐 GET /products/_search { query: { term: { is_active: true } } } // 推荐 GET /products/_search { query: { bool: { filter: [ { term: { is_active: true } } ] } } }2. 限制返回字段GET /products/_search { _source: [name, price], query: { match_all: {} } }3. 使用 scroll 处理大量数据GET /products/_search { scroll: 1m, size: 1000, query: { match_all: {} } }4. 避免深度分页// 使用 search_after 代替 from/size GET /products/_search { size: 100, query: { match_all: {} }, sort: [ { _id: asc } ], search_after: [last_document_id] }总结Elasticsearch DSL 的核心要点查询类型match、term、range、bool 等复合查询bool 查询的 must、must_not、should、filter聚合分析terms、stats、range、date_histogram 等高级功能highlight、script、explain、profile性能优化使用 filter、限制字段、避免深度分页通过本教程你应该能够创建和管理索引执行各种类型的查询进行数据聚合分析优化查询性能继续实践和探索你会发现 Elasticsearch DSL 的强大之处

相关新闻

小米今年的薪资,“性价比”拉满了!

小米今年的薪资,“性价比”拉满了!

小米最近也开奖了!不得不说,软件开发岗位的薪资“性价比”确实拉满了,在北京这边,白菜价只有 18k * 15,再高一点有 (20~24)k * 15 的,普遍开的不高。 不过,小米今年在自…

2026/7/3 17:01:34 阅读更多 →
【Linux系统编程】(二十二)从磁盘物理结构到地址映射:Ext 系列文件系统硬件底层原理深度剖析

【Linux系统编程】(二十二)从磁盘物理结构到地址映射:Ext 系列文件系统硬件底层原理深度剖析

目录 前言 一、认识文件系统的硬件基础:从机房到磁盘 1.1 机房、机柜、服务器与磁盘的层级关系 1.2 磁盘物理结构:机械硬盘的 “五脏六腑” 1.3 磁盘存储结构:磁道、柱面与扇区的三维布局 1.3.1 磁道(Track) 1.…

2026/7/3 17:01:35 阅读更多 →
基于微信小程序的在线预约挂号系统(源码+lw+部署文档+讲解等)

基于微信小程序的在线预约挂号系统(源码+lw+部署文档+讲解等)

课题介绍 本课题聚焦基于微信小程序的在线预约挂号系统的设计与实现,后端依托SpringBoot架构提供稳定业务支撑,针对性解决传统医疗就诊中挂号排队耗时久、号源管控混乱、医生排班不透明、就诊提醒缺失、跨院病历不通等核心痛点,构建集在线挂号…

2026/7/3 17:01:42 阅读更多 →

最新新闻

C++容器——vector的基本实现(下)

C++容器——vector的基本实现(下)

在上一篇博客中已经讲述了vector的基本使用方法。为了更好的理解其底层原理和提高一定的代码能力,本篇博客将针对vector进行一个简单的基础实现。一.vector的基础实现由于vector是模板类,所以类内函数的定义和声明不能分开编写,否则会出现编译…

2026/7/3 17:29:55 阅读更多 →
sql语法- MyBatis 中 <association> 标签的作用 1对1的情况

sql语法- MyBatis 中 <association> 标签的作用 1对1的情况

之前我们都是普通查询, 现在我们有个疑问如果出现下面的定义的model 数据库能直接查询么, 就是sql查询出来能够映射 对象作为另外一个对象的属性 // 主对象 public class ProjInfoModel {private Long projId;private String projName;private ProjAppInfoModel projAppInfoMod…

2026/7/3 17:27:54 阅读更多 →
打造你的终极数字伙伴:用DyberPet桌面宠物框架重新定义桌面互动体验

打造你的终极数字伙伴:用DyberPet桌面宠物框架重新定义桌面互动体验

打造你的终极数字伙伴:用DyberPet桌面宠物框架重新定义桌面互动体验 【免费下载链接】DyberPet Desktop Cyber Pet Framework based on PySide6 项目地址: https://gitcode.com/GitHub_Trending/dy/DyberPet 你是否厌倦了单调的桌面背景?是否渴望…

2026/7/3 17:25:54 阅读更多 →
PIC18F8722外部EEPROM存储扩展实战指南

PIC18F8722外部EEPROM存储扩展实战指南

1. 为什么需要外部EEPROM存储扩展在嵌入式系统开发中,PIC18F8722这类微控制器自带有限的内部存储空间。以PIC18F8722为例,其内部EEPROM容量仅为1024字节(1KB),这对于需要存储大量配置参数、历史数据或日志记录的应用场…

2026/7/3 17:21:52 阅读更多 →
高效低查重!AI教材生成工具助力教师轻松完成教材编写

高效低查重!AI教材生成工具助力教师轻松完成教材编写

谁没有在编写教材时感到困惑呢? 面对一页空白的文档,沉思了半个多小时,知识点的整理似乎毫无头绪——是先讲解基本概念,还是先分享案例呢?章节的划分该按照逻辑、还是依据课时呢?不断修改的大纲总是无法符…

2026/7/3 17:21:52 阅读更多 →
从8万美元跌至千元级,车载激光雷达成本暴跌96%背后:芯片化、规模化与全场景落地实战

从8万美元跌至千元级,车载激光雷达成本暴跌96%背后:芯片化、规模化与全场景落地实战

目录 摘要 一、行业综述:激光雷达从天价科研设备到民用标配的蜕变 1.1 十年价格迭代核心数据 1.2 市场格局与产业现状 二、核心降本逻辑一:芯片化架构重构,从分立器件到单芯片集成 2.1 传统分立架构的致命成本缺陷 2.2 芯片化自研的核心降本原理 2.3 头部厂商差异化…

2026/7/3 17:19:52 阅读更多 →

日新闻

Nginx防御TLS重协商攻击实战:从原理到配置与监控

Nginx防御TLS重协商攻击实战:从原理到配置与监控

1. 项目概述:为什么TLS重协商攻击至今仍需警惕十多年前的CVE-2011-1473,一个关于TLS/SSL协议重协商机制的漏洞,现在提起来还有必要吗?很多运维和开发朋友可能会觉得,这都老掉牙了,现代服务器和客户端不都默…

2026/7/3 0:03:59 阅读更多 →
华为防火墙双通道远程管理实战:Web与SSH配置详解

华为防火墙双通道远程管理实战:Web与SSH配置详解

1. 项目概述:为什么需要双通道远程管理防火墙?在任何一个稍具规模的企业网络里,防火墙都是那个默默守护在边界的关键角色。作为网络工程师,我们不可能每次都跑到机房,插上console线去配置它。远程管理能力,…

2026/7/3 0:03:59 阅读更多 →
AD74413R与PIC18F65K40的高精度工业数据采集方案

AD74413R与PIC18F65K40的高精度工业数据采集方案

1. 项目概述:AD74413R与PIC18F65K40的协同工作在工业自动化和精密测量领域,同时实现高精度模数转换(ADC)和数模转换(DAC)功能是许多复杂系统的核心需求。AD74413R作为一款四通道可配置模拟输入/输出器件,与PIC18F65K40微控制器的组合&#xf…

2026/7/3 0:05:59 阅读更多 →

周新闻

月新闻