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 的学习和使用中取得更多成果!


参考资源:

相关工具:


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

相关推荐
程序员差不多先生4 小时前
Anthropic 用 5 个月重写了 Git:当 AI Agent 把版本控制系统逼到墙角
人工智能·git·elasticsearch·deepseek v4 pro·kimi k3·glm 5.3·qwen3.7 max
Elastic 中国社区官方博客5 小时前
Jina AI:Elasticsearch 集成
大数据·运维·人工智能·elasticsearch·搜索引擎·全文检索·jina
草莓熊Lotso5 小时前
【C++ 基于微服务的即时通讯系统】Elasticsearch 从安装到 C++ 客户端二次封装全落地
linux·网络·数据库·c++·elasticsearch·微服务
大菠萝爱上小西瓜5 小时前
基于 Spark + Iceberg 湖仓一体的 Netflix 流媒体用户行为全链路数仓与 ALS 智能推荐决策系统(含穿透下钻大屏)
大数据·分布式·spark
吴建旭 智宅焕6 小时前
智能家居售后服务体系的架构设计:分布式售后风险的集中式兜底方案
分布式·智能家居
.冰块.15 小时前
ELK 日志分析系统实验:部署 Elasticsearch 双节点集群实现 Apache 日志采集与 Kibana 可视化
elk·elasticsearch·apache·日志
lisanmengmeng16 小时前
Ganglia分布式集群监控系统安装手册
分布式
顧棟21 小时前
Elasticsearch Scroll :分页进度到底记在哪?
大数据·elasticsearch·搜索引擎
Smoothcloud润云1 天前
GPU算力平台推荐:新手租GPU时,镜像环境和显存应该怎么选?
大数据·人工智能·pytorch·分布式·深度学习·gpu算力
程序员大阳1 天前
副队长大数据教程(6)--四节点分布式集群搭建
大数据·分布式·集群·搭建·四节点