如何在CentOS 7/6中安装Elasticsearch
Elasticsearch是一个灵活而强大的开源分布式实时搜索和分析引擎。
使用一组简单的api就可以进行全文搜索。
Elasticsearch在Apache 2许可下免费提供,它提供了最大的灵活性。
本教程将在CentOS、Red Hat和Fedora系统上设置Elasticsearch单节点集群。
第1步-准备
在任何系统上安装Elasticsearch都需要Java。
我们可以通过执行以下命令来检查已安装的Java版本。
java -version
步骤2 -设置Yum存储库
首先,为elasticsearch rpm包安装GPG key。
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
然后为elasticsearch创建yum存储库文件。
编辑 /etc/yum.repos.d/elasticsearch.repo 文件:
sudo vi /etc/yum.repos.d/elasticsearch.repo
添加以下内容:
[Elasticsearch-7] name=Elasticsearch repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/7.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
步骤3 -安装Elasticsearch
添加yum repository后,只需在CentOS和RHEL系统上使用以下命令安装Elasticsearch:
sudo yum install elasticsearch
安装成功后,编辑Elasticsearch配置文件 “/etc/elasticsearch/elasticsearch.yml”
,将 network.host 设置为 localhost
我们还可以将其更改为系统LAP IP地址,以便通过网络访问。
vim /etc/elasticsearch/elasticsearch.yml
network.host: localhost 或者 network.host: 服务器ip地址
然后启用elasticsearch服务并启动它。
sudo systemctl enable elasticsearch sudo systemctl start elasticsearch
ElasticSearch已经成功安装并在CentOS或RHEL系统上运行。
运行以下命令来验证服务:
curl -X GET "localhost:9200/?pretty"
你会看到类似如下的结果:
{
"name" : "theitroad",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "HY8HoLHnRCeb3QzXnTcmrQ",
"version" : {
"number" : "7.4.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "22e1767283e61a198cb4db791ea66e3f11ab9910",
"build_date" : "2019-09-27T08:36:48.569419Z",
"build_snapshot" : false,
"lucene_version" : "8.2.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
步骤4 - Elasticsearch示例(可选)
下面的示例将在Elasticsearch集群中添加、获取和搜索数据。
创建新的Bucket
curl -XPUT http://localhost:9200/mybucket
输出:
{"acknowledged":true}
向Elasticsearch添加数据
使用以下命令在Elasticsearch中添加一些数据。
命令1:
curl -XPUT 'http://localhost:9200/mybucket/user/johny' -d '{ "name" : "Hyman Kumar" }'
输出:
{"_index":"mybucket","_type":"user","_id":"johny","_version":1,"created":true}
命令2:
curl -XPUT 'http://localhost:9200/mybucket/post/1' -d '
{
"user": "Hyman",
"postDate": "01-15-2015",
"body": "This is Demo Post 1 in Elasticsearch" ,
"title": "Demo Post 1"
}'
输出:
{"_index":"mybucket","_type":"post","_id":"1","_version":1,"created":true}
命令3:
curl -XPUT 'http://localhost:9200/mybucket/post/2' -d '
{
"user": "theitroad",
"postDate": "01-15-2015",
"body": "This is Demo Post 2 in Elasticsearch" ,
"title": "Demo Post 2"
}'
输出:
{"_index":"mybucket","_type":"post","_id":"2","_version":1,"created":true}
从Elasticsearch获取数据
使用以下命令从ElasticSearch获取数据并读取输出。
curl -XGET 'http://localhost:9200/mybucket/user/johny?pretty=true' curl -XGET 'http://localhost:9200/mybucket/post/1?pretty=true' curl -XGET 'http://localhost:9200/mybucket/post/2?pretty=true'
在Elasticsearch中进行搜索
使用以下命令在Elasticsearch中搜索数据。
下面的命令将搜索与用户johny相关的所有数据。
curl 'http://localhost:9200/mybucket/post/_search?q=user:theitroad&pretty=true'
输出:
{
"took" : 145,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "mybucket",
"_type" : "post",
"_id" : "2",
"_score" : 0.30685282,
"_source":
{
"user": "theitroad",
"postDate": "01-15-2015",
"body": "This is Demo Post 2 in Elasticsearch" ,
"title": "Demo Post 2"
}
} ]
}
}
现在我们已经在Linux系统上成功配置了elasticsearch单节点集群。

