如何在 Ubuntu 22.04 上安装和配置 Elasticsearch
Elasticsearch 是一个开源分布式搜索和分析引擎。它基于 Apache Lucene,允许搜索各种类型的结构化和非结构化数据,例如文本、数字或地理空间数据。它是一种流行的搜索引擎,专为具有复杂搜索需求的应用程序而设计。您可以将 Elasticsearch 与您的应用程序集成以添加搜索功能。
本教程将向您展示如何在 Ubuntu 22.04 上安装 Elasticsearch。
要求
- 运行 Ubuntu 22.04 的服务器。
- 服务器上设置了 root 密码。
安装Java
由于Elasticsearch是基于Java的,因此您需要在服务器上安装Java。您可以使用以下命令安装 Java JDK:
apt install default-jdk -y
Java 安装完成后,您可以使用以下命令检查 Java 版本:
java --version
您应该看到以下输出:
openjdk 11.0.15 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)
添加 Elasticsearch 存储库
默认情况下,Elasticsearch 软件包不包含在 Ubuntu 22.04 的默认存储库中。因此,您需要将官方存储库添加到APT中。
首先,使用以下命令安装所需的依赖项:
apt install curl wget gnupg2 wget -y
安装所有依赖项后,使用以下命令添加 Elasticsearch GPG 密钥:
curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | gpg --dearmor -o /usr/share/keyrings/elastic.gpg
接下来,使用以下命令将 Elasticsearch 存储库添加到 APT:
echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | tee -a /etc/apt/sources.list.d/elastic-7.x.list
接下来,使用以下命令更新存储库的缓存:
apt update -y
更新存储库后,您可以继续下一步。
在 Ubuntu 22.04 上安装 Elasticsearch
现在使用以下命令安装 Elasticsearch:
apt install elasticsearch -y
安装 Elasticsearch 包后,编辑 Elasticsearch 配置文件:
nano /etc/elasticsearch/elasticsearch.yml
更改以下行:
network.host: localhost
保存并关闭文件,然后使用以下命令启动 Elasticsearch 服务:
systemctl start elasticsearch
您可以使用以下命令检查Elasticsearch的状态:
systemctl status elasticsearch
您应该看到以下输出:
? elasticsearch.service - Elasticsearch
Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2022-05-28 10:44:01 UTC; 8s ago
Docs: https://www.elastic.co
Main PID: 7259 (java)
Tasks: 68 (limit: 2292)
Memory: 1.2G
CPU: 45.941s
CGroup: /system.slice/elasticsearch.service
??7259 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.tt>
??7417 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller
May 28 10:43:20 ubuntu2204 systemd[1]: Starting Elasticsearch...
May 28 10:44:01 ubuntu2204 systemd[1]: Started Elasticsearch.
验证 Elasticsearch 安装
安装Elasticsearch后,您可以使用以下命令验证Elasticsearch安装:
curl -X GET 'http://localhost:9200'
您应该看到以下输出:
{
"name" : "ubuntu2204",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "lrkrHPovRgiCdF67bVnS9w",
"version" : {
"number" : "7.17.4",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "79878662c54c886ae89206c685d9f1051a9d6411",
"build_date" : "2022-05-18T18:04:20.964345128Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
如何使用 Elasticsearch
借助Elasticsearch,您可以使用curl 命令创建、读取、更新和删除数据。要将条目添加到 Elasticsearch,请运行以下命令:
curl -XPOST -H "Content-Type: application/json" 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
您将得到以下输出:
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
您现在可以使用以下命令检索添加的条目:
curl -X GET -H "Content-Type: application/json" 'http://localhost:9200/tutorial/helloworld/1'
您将得到以下输出:
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{ "message": "Hello World!" }}
要修改现有条目,请使用 put 命令,如下所示:
curl -X PUT -H "Content-Type: application/json" 'localhost:9200/tutorial/helloworld/1?pretty' -d ' { "message": "Hello, People!" }'
您将得到以下输出:
{
"_index" : "tutorial",
"_type" : "helloworld",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
现在,您可以使用以下命令以更易读的格式检索添加的条目:
curl -X GET -H "Content-Type: application/json" 'http://localhost:9200/tutorial/helloworld/1?pretty'
您将得到以下输出:
{
"_index" : "tutorial",
"_type" : "helloworld",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"message" : "Hello, People!"
}
}
结论
恭喜。您已在 Ubuntu 22.04 上成功安装 Elasticsearch。现在您可以将 Elasticsearch 集成到您的应用程序中并实时搜索数据。