Elasticsearch 入门指南:从索引到全文搜索实战

Elasticsearch 入门指南:从索引到全文搜索实战

admin
2026-07-24 / 0 评论 / 1 阅读

前言

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 创建索引与 Mapping

PUT /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.zip

5.2 两种分词模式

GET /_analyze
{
  "analyzer": "ik_smart",
  "text": "Elasticsearch是分布式搜索引擎"
}
// 结果: Elasticsearch / 是 / 分布式 / 搜索引擎

GET /_analyze
{
  "analyzer": "ik_max_word",
  "text": "Elasticsearch是分布式搜索引擎"
}
// 结果: Elasticsearch / 是 / 分布 / 式 / 分布式 / 搜索 / 引擎 / 搜索引擎
  • ik_smart:粗粒度,适合索引时分词
  • ik_max_word:细粒度,适合搜索时分词

六、性能优化要点

  1. 索引字段合理选型:不需要搜索的字段不建索引
  2. filter 代替 query:不需要算分的场景用 filter,走缓存
  3. 分页用 search_after:避免深度分页的 from + size
  4. 控制分片数量:单分片建议 10-50GB
  5. 使用批量操作:bulk 比单条操作快 10 倍以上

总结

Elasticsearch 在全文搜索、日志分析、数据可视化等场景中几乎是标配。掌握 Mapping 设计、查询 DSL 和分词器配置是使用 ES 的基本功。生产环境中注意合理设置分片副本、映射字段类型和查询优化策略。

0

评论 (0)

取消
0:00