首页
直播
壁纸
友链
搜索
1
MySQL如何解决深度分页问题?
11 阅读
2
buildadmin百度编辑器放两个只能显示一个
8 阅读
3
网站被 CC 攻击了?别慌,教你几招接地气的防护办法
7 阅读
4
thinkphp6 消息队列think-queue
6 阅读
5
microsoft store安装codex失败
5 阅读
服务器运维
后端技术
前端技术
梯子
数据库
小程序
登录
搜索
标签搜索
fastadmin
Redis
RabbitMQ
Go
服务器
codex
buildadmin
小程序
mysql
Nginx
Docker
Vue3
Node.js
MySQL优化
Linux
TypeScript
JWT
消息队列
Elasticsearch
搜索引擎
沿途的风景
累计撰写
37
篇文章
累计收到
0
条评论
首页
栏目
服务器运维
后端技术
前端技术
梯子
数据库
小程序
页面
直播
壁纸
友链
搜索到
1
篇与
» Elasticsearch
的结果
2026-07-24
Elasticsearch 入门指南:从索引到全文搜索实战
前言Elasticsearch 是基于 Lucene 的分布式搜索和分析引擎,支持全文检索、结构化搜索、聚合分析等。本文将带你从零开始掌握 ES 的核心概念和实战操作。一、核心概念Elasticsearch关系型数据库说明IndexDatabase索引(数据库)DocumentRow文档(一行记录)FieldColumn字段MappingSchema字段类型定义ShardPartition分片,数据水平拆分二、Docker 部署docker run -d --name es \ -p 9200:9200 -p 9300:9300 \ -e "discovery.type=single-node" \ -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \ -e "xpack.security.enabled=false" \ elasticsearch:8.13.0验证:curl http://localhost:9200三、索引操作3.1 创建索引与 MappingPUT /articles { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word" }, "content": { "type": "text", "analyzer": "ik_max_word" }, "author": { "type": "keyword" }, "tags": { "type": "keyword" }, "view_count": { "type": "integer" }, "publish_date": { "type": "date", "format": "yyyy-MM-dd" } } } }text 类型:会被分词,适合全文搜索keyword 类型:不分词,适合精确匹配和聚合ik_max_word:中文分词器,最细粒度切分3.2 新增文档POST /articles/_doc/1 { "title": "Elasticsearch 入门指南", "content": "Elasticsearch 是基于 Lucene 的分布式搜索引擎", "author": "admin", "tags": ["搜索", "ES"], "view_count": 100, "publish_date": "2026-07-24" }3.3 批量操作POST /_bulk {"index": {"_index": "articles", "_id": "2"}} {"title": "文章2", "content": "内容2", "author": "admin", "tags": ["测试"], "view_count": 50, "publish_date": "2026-07-24"} {"index": {"_index": "articles", "_id": "3"}} {"title": "文章3", "content": "内容3", "author": "user", "tags": ["搜索"], "view_count": 200, "publish_date": "2026-07-23"}四、搜索查询4.1 全文搜索GET /articles/_search { "query": { "match": { "content": "分布式搜索引擎" } } }4.2 多字段搜索GET /articles/_search { "query": { "multi_match": { "query": "Elasticsearch 分布式", "fields": ["title^3", "content"] } } }title^3 表示 title 字段权重是 content 的 3 倍。4.3 精确匹配GET /articles/_search { "query": { "term": { "author": "admin" } } }4.4 组合查询(bool)GET /articles/_search { "query": { "bool": { "must": [ { "match": { "content": "搜索引擎" } } ], "filter": [ { "term": { "author": "admin" } }, { "range": { "view_count": { "gte": 50 } } } ], "must_not": [ { "term": { "tags": "测试" } } ] } } }must:必须匹配(参与算分)filter:必须匹配(不算分,走缓存,性能更好)must_not:必须不匹配should:应该匹配(满足则加分)4.5 高亮搜索结果GET /articles/_search { "query": { "match": { "content": "搜索引擎" } }, "highlight": { "fields": { "content": { "pre_tags": ["<em>"], "post_tags": ["</em>"] } } } }4.6 聚合查询GET /articles/_search { "size": 0, "aggs": { "by_author": { "terms": { "field": "author", "size": 10 }, "aggs": { "avg_views": { "avg": { "field": "view_count" } } } } } }五、中文分词5.1 安装 IK 分词器./bin/elasticsearch-plugin install \ https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.13.0/elasticsearch-analysis-ik-8.13.0.zip5.2 两种分词模式GET /_analyze { "analyzer": "ik_smart", "text": "Elasticsearch是分布式搜索引擎" } // 结果: Elasticsearch / 是 / 分布式 / 搜索引擎 GET /_analyze { "analyzer": "ik_max_word", "text": "Elasticsearch是分布式搜索引擎" } // 结果: Elasticsearch / 是 / 分布 / 式 / 分布式 / 搜索 / 引擎 / 搜索引擎ik_smart:粗粒度,适合索引时分词ik_max_word:细粒度,适合搜索时分词六、性能优化要点索引字段合理选型:不需要搜索的字段不建索引filter 代替 query:不需要算分的场景用 filter,走缓存分页用 search_after:避免深度分页的 from + size控制分片数量:单分片建议 10-50GB使用批量操作:bulk 比单条操作快 10 倍以上总结Elasticsearch 在全文搜索、日志分析、数据可视化等场景中几乎是标配。掌握 Mapping 设计、查询 DSL 和分词器配置是使用 ES 的基本功。生产环境中注意合理设置分片副本、映射字段类型和查询优化策略。
2026年07月24日
1 阅读
0 评论
0 点赞
0:00