沿途的风景 - Elasticsearch
type: channel
link: https://inspirecl.asia/index.php/tag/elasticsearch/
items: Elasticsearch 入门指南：从索引到全文搜索实战

Elasticsearch 入门指南：从索引到全文搜索实战
type: item
date: 2026-03-15T18:58:01+08:00
description: 前言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": [ { …