Elasticsearch 入门教学:从零开始掌握分布式搜索引擎

引言

Elasticsearch 是一个开源的分布式搜索引擎,基于 Apache Lucene 构建,能够实现近乎实时的数据搜索和分析。它广泛应用于日志分析、全文搜索、数据可视化等场景。本文将带你从零开始学习 Elasticsearch,掌握其基本概念、安装配置、数据操作及搜索功能。

1. Elasticsearch 简介

1.1 什么是 Elasticsearch?

Elasticsearch 是一个分布式的 RESTful 搜索引擎,能够快速地存储、搜索和分析大量数据。它的核心特点包括:

  • 分布式:数据可以分布在多个节点上,支持水平扩展。
  • 实时性:数据几乎可以实时地被索引和搜索。
  • 全文搜索:支持复杂的全文搜索功能。
  • RESTful API:通过 HTTP 接口进行操作,易于集成。

1.2 Elasticsearch 的核心概念

  • 索引(Index):类似于关系数据库中的"数据库",用于存储相关文档。
  • 类型(Type):在 Elasticsearch 7.x 及之前版本中,索引可以包含多个类型,类似于"表"。但在 7.x 之后,类型逐渐被弃用,建议每个索引只包含一个类型。
  • 文档(Document):索引中的基本数据单元,类似于关系数据库中的"行"。
  • 字段(Field):文档中的属性,类似于关系数据库中的"列"。
  • 分片(Shard):索引可以被分成多个分片,分布在不同节点上,以实现分布式存储和搜索。
  • 副本(Replica):每个分片可以有多个副本,用于提高数据的可用性和容错性。

2. 安装与配置

2.1 安装 Elasticsearch

Elasticsearch 可以在多种操作系统上运行,以下以 Linux 为例介绍安装步骤。

  1. 下载 Elasticsearch

    访问 Elasticsearch 官方下载页面,选择适合的版本进行下载。

    bash 复制代码
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.1-linux-x86_64.tar.gz
  2. 解压并安装

    bash 复制代码
    tar -xzf elasticsearch-7.10.1-linux-x86_64.tar.gz
    cd elasticsearch-7.10.1/
  3. 启动 Elasticsearch

    bash 复制代码
    ./bin/elasticsearch

    默认情况下,Elasticsearch 会在 localhost:9200 上启动。

2.2 配置 Elasticsearch

Elasticsearch 的配置文件位于 config/elasticsearch.yml,常见的配置项包括:

  • 集群名称cluster.name
  • 节点名称node.name
  • 网络绑定地址network.host
  • 端口http.port

例如,修改集群名称:

yaml 复制代码
cluster.name: my_cluster

3. 基本操作

3.1 使用 RESTful API 操作 Elasticsearch

Elasticsearch 提供了丰富的 RESTful API,可以通过 HTTP 请求进行操作。以下是一些常用的操作示例。

3.1.1 创建索引
bash 复制代码
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}'
3.1.2 插入文档
bash 复制代码
curl -X POST "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "title": "Elasticsearch 入门",
  "content": "这是一篇关于 Elasticsearch 的入门教程。",
  "tags": ["搜索", "教程"]
}'
3.1.3 查询文档
bash 复制代码
curl -X GET "localhost:9200/my_index/_doc/1"
3.1.4 搜索文档
bash 复制代码
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "content": "入门"
    }
  }
}'

3.2 使用 Kibana 进行操作

Kibana 是 Elasticsearch 的可视化工具,提供了更友好的界面来操作 Elasticsearch。你可以通过 Kibana 的 Dev Tools 来执行上述的 RESTful API 操作。

4. 进阶操作

4.1 索引管理

Elasticsearch 提供了丰富的索引管理功能,包括索引的创建、删除、映射定义等。

4.1.1 删除索引
bash 复制代码
curl -X DELETE "localhost:9200/my_index"
4.1.2 定义映射

映射(Mapping)用于定义索引中的字段类型。

bash 复制代码
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "tags": { "type": "keyword" }
    }
  }
}'

4.2 复杂查询

Elasticsearch 支持多种复杂的查询方式,如布尔查询、范围查询、聚合查询等。

4.2.1 布尔查询
bash 复制代码
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "content": "入门" }},
        { "match": { "tags": "教程" }}
      ]
    }
  }
}'
4.2.2 聚合查询
bash 复制代码
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "popular_tags": {
      "terms": { "field": "tags" }
    }
  }
}'

5. 总结

通过本文,你已经了解了 Elasticsearch 的基本概念、安装配置、数据操作及搜索功能。Elasticsearch 是一个功能强大的搜索引擎,适用于各种数据搜索和分析场景。接下来,你可以进一步学习 Elasticsearch 的高级功能,如集群管理、性能优化等。

希望这篇入门教程对你有所帮助,祝你在 Elasticsearch 的学习和使用中取得更多成果!


参考资源

相关工具


如果你有任何问题或建议,欢迎在评论区留言!

相关推荐
元拓数智1 天前
智能分析落地卡壳?先补好「数据关系+语义治理」这层技术基建
大数据·分布式·ai·spark·数据关系·语义治理
liu_sir_1 天前
升级谷歌webview
大数据·elasticsearch·搜索引擎
GIS数据转换器1 天前
农村生活污水治理智慧管控平台
大数据·人工智能·分布式·数据分析·生活·智慧城市
Trouvaille ~1 天前
【Redis篇】初识 Redis:特性、应用场景与版本演进
数据结构·数据库·redis·分布式·缓存·中间件·持久化
米高梅狮子1 天前
Ceph 分布式存储 部署
linux·运维·数据库·分布式·ceph·docker·华为云
郭龙_Jack1 天前
跨境电商 平台 - ERP - 内部子系统 交互方式总图
分布式·教育电商
喝醉酒的小白1 天前
Kafka 集群应急故障排查手册
分布式·kafka
无籽西瓜a1 天前
【西瓜带你学Kafka | 第八期】 Kafka的主从同步、消息可靠性、流处理与顺序消费(文含图解)
java·分布式·后端·kafka·消息队列·mq
qqVHU1 天前
kafka笔记
笔记·分布式·kafka
醉颜凉1 天前
Kafka 消息过期时间设置与清理机制全解析
分布式·kafka·linq