# 安装软件
Elasticsearch(简称 ES)是一个基于 Apache Lucene(TM)的开源搜索引擎,无论在开源还是专有领域,Lucene 可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。
# Homestead
首先在 Homestead.yaml
配置文件中添加 elasticsearch
,具体版本可以访问 Github 查看
features:
...
- elasticsearch:
version: 7.12.1
然后执行重起 homestead 命令 ,由于网络问题安装可能非常慢,请耐心等待。
homestead reload --provision
# 服务器安装
下面演示在 ubuntu 服务器安装 ES,首先设置安装源
sudo apt update
sudo apt install apt-transport-https ca-certificates wget
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
添加 Elasticsearch 数据源,你可以根据需要将 7.x 修改为你的版本
sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
接着就可以安装 elasticsearch 了
sudo apt update
sudo apt install elasticsearch
# 管理服务
开机启动
systemctl enable elasticsearch.service
启动服务
systemctl start elasticsearch.service
重起服务
sudo service elasticsearch restart
不需要 elasticsearch 时可以禁止开机启动
sudo systemctl disable elasticsearch.service
# 检测安装
登录服务器执行以下命令查看安装结果
➜ ~ curl http://127.0.0.1:9200/
{
"name" : "homestead",
"cluster_name" : "homestead",
"cluster_uuid" : "UCGpKIUSRiO2eQm7vCxq8A",
"version" : {
"number" : "7.12.1",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "3186837139b9c6b6d23c3200870651f10d3343b7",
"build_date" : "2021-04-20T20:56:39.040728659Z",
"build_snapshot" : false,
"lucene_version" : "8.8.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
# 外网访问
如果要求宿主机可访问 homestead 内 elasticsearch,需要编辑/etc/elasticsearch/elasticsearch.yml
文件,在末尾添加
network.host: 0.0.0.0
discovery.seed_hosts: ["127.0.0.1", "::1"]
然后重起 elasticsearch 服务
sudo service elasticsearch restart
# 插件管理
elasticsearch 的插件存放在 usr/share/elasticsearch/plugins
目录
可以通地以下命令查看已经安装的插件
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin list
# 中文分词
对中文支持需要安装 elasticsearch-analysis-ik 插件
# 安装插件
快速安装
在 elasticsearch 安装目录 /usr/share/elasticsearch
执行命令
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
上面的 7.12.1 可以更换成其他发生版本
# 运行测试
执行下面命令测试中文分词是否安装成功
curl -X POST "http://192.168.10.10:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
"analyzer": "ik_max_word",
"text": "向军老师在后盾人录制视频"
}'
如果显示以下结果 ,表示安装成功
...
{
"token" : "在后",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "后盾",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
...
手动下载安装包
如果以上命令由于网络原因无法执行,可以通过自行下载releases zip (opens new window)包来安装。
在 elasticsearch 插件目录创建存 ik 目录用于放置插件
cd /usr/share/elasticsearch/plugins
sudo mkdir ik
cd ik
在ik
目录下载插件压缩包,请自行更换版本号 (opens new window)
sudo wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
解压缩包下载包
sudo unzip elasticsearch-analysis-ik-7.12.1.zip
再执行命令查看插件安装情况
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin list
# CURL
为了体验 Elasticsearch 先掌握 curl 命令的基本使用。
基本用法
curl https://www.houdunren.com
-X
参数指定请求方式,下面是以 POST 方式发送 HTTP 请求
$ curl -X POST https://www.example.com
-o
参数将服务器的响应保存成文件,等同于 wget 命令
curl -o houdunren.html https://www.houdunren.com
-O
参数将服务器响应保存成文件,并将 URL 的最后部分当作文件名
$ curl -O https://www.houdunren.com/index.zip
上面命令将服务器回应保存成文件,文件名为 index.zip。
-H
参数添加 HTTP 请求的标头
$ curl -H 'Content-Type: application/json' https://www.houdunren.com
-d
参数用于发送 POST 请求的数据体
$ curl -X https://www.houdunren.com -d "name=houdunren&user=向军老师"
# 常见问题
# 启动超归
启动时出现:Job for elasticsearch.service failed because a timeout was exceeded,这是因为服务器配置低造成的。
修改配置文件 sudo vim /usr/lib/systemd/system/elasticsearch.service
中的超时时间为 300 秒
TimeoutStartSec=300
然后重起服务器,更推荐升级服务器或使用云 elasticsearch 服务。
基本使用 →