Elasticsearch(ES)是一个开源的分布式搜索和分析引擎,常用于全文搜索、日志分析和数据分析。以下是 Elasticsearch 的入门介绍及常用命令:
Elasticsearch 基本概念
-
索引(Index):
- 类似于关系数据库中的数据库,它是存储文档的地方。每个索引由多个文档组成。
-
文档(Document):
- 类似于关系数据库中的行,是索引中的基本数据单元,表示一个 JSON 对象。
-
字段(Field):
- 类似于关系数据库中的列,文档中的数据项。
-
分片(Shard):
- 索引的数据被分为多个分片,以支持水平扩展。每个分片是一个独立的 Lucene 实例。
-
副本(Replica):
- 分片的副本,提供数据的冗余备份和负载均衡。
Elasticsearch 安装和启动
-
下载和安装:
从 Elasticsearch 官网 下载适合的版本,解压并安装。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.x.x-linux-x86_64.tar.gz tar -xzf elasticsearch-8.x.x-linux-x86_64.tar.gz cd elasticsearch-8.x.x
-
启动 Elasticsearch:
./bin/elasticsearch
默认情况下,Elasticsearch 会在
localhost:9200
上启动。
常用命令
-
检查集群状态:
curl -X GET "localhost:9200/_cluster/health?pretty"
-
创建索引:
curl -X PUT "localhost:9200/my_index?pretty"
这里的
my_index
是索引名称。 -
查看所有索引:
curl -X GET "localhost:9200/_cat/indices?v"
-
删除索引:
curl -X DELETE "localhost:9200/my_index?pretty"
-
索引文档:
curl -X POST "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d' { "user": "john", "message": "Hello, Elasticsearch!" } '
-
查询文档:
curl -X GET "localhost:9200/my_index/_doc/1?pretty"
-
搜索文档:
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d' { "query": { "match": { "message": "Hello" } } } '
-
更新文档:
curl -X POST "localhost:9200/my_index/_doc/1/_update" -H 'Content-Type: application/json' -d' { "doc": { "message": "Updated message" } } '
-
删除文档:
curl -X DELETE "localhost:9200/my_index/_doc/1?pretty"
-
集群节点信息:
curl -X GET "localhost:9200/_nodes?pretty"
以上是 Elasticsearch 的基本概念和常用命令,可以帮助您入门并开始使用 Elasticsearch 进行数据存储、搜索和分析。