ElasticSearch01(ES简介,安装ES,操作索引,操作文档,RestAPI)【全详解】
warning:
这篇文章距离上次修改已过193天,其中的内容可能已经有所变动。
Elasticsearch 是一个分布式的 RESTful 风格的搜索和数据分析引擎,能够解决各种搜索需求,速度远快于传统关系型数据库。
简介
Elasticsearch 是一个基于 Apache Lucene 的搜索和分析引擎。它使你能够近实时地存储、搜索和分析大量数据。Elasticsearch 是用 Java 开发的,并在 Apache 许可证下分发。
- 安装Elasticsearch
- 在Linux上安装Elasticsearch
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt-get update && sudo apt-get install elasticsearch
- 启动Elasticsearch服务
sudo systemctl start elasticsearch.service
- 设置开机启动
sudo systemctl enable elasticsearch.service
- 操作索引
- 创建索引
curl -X PUT "localhost:9200/my_index"
- 获取索引
curl -X GET "localhost:9200/my_index"
- 删除索引
curl -X DELETE "localhost:9200/my_index"
- 操作文档
- 创建文档
curl -X PUT "localhost:9200/my_index/_doc/my_id" -H 'Content-Type: application/json' -d'
{
"name": "John Doe",
"age": 30,
"about": "I love to go rock climbing"
}'
- 获取文档
curl -X GET "localhost:9200/my_index/_doc/my_id"
- 更新文档
curl -X POST "localhost:9200/my_index/_update/my_id" -H 'Content-Type: application/json' -d'
{
"doc": { "about": "I now love to go rock climbing" }
}'
- 删除文档
curl -X DELETE "localhost:9200/my_index/_doc/my_id"
RestAPI
Elasticsearch 提供了一套丰富的 REST API,用于操作索引、文档以及执行搜索等。
- 搜索文档
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"about": "rock"
}
}
}'
以上是Elasticsearch的基本介绍、安装、索引操作、文档操作和RestAPI的使用。在实际应用中,Elasticsearch 还可以与 Logstash、Kibana 等其他工具配合使用,用于日志分析、监控等场景。
评论已关闭