三、ElasticSearch-Head插件安装及使用
ElasticSearch (3) Head插件安装及使用
elasticsearch head插件是一个入门级的elasticsearch 前端插件,下面进行安装:
1、 Head插件安装
前提: head插件是Nodejs实现的,所以需要先安装Nodejs。
1.1 安装nodejs
下载linux64位:
[root@localhost nodejs]# wget https://nodejs.org/dist/v12.18.4/node-v12.18.4-linux-x64.tar.xz
[root@localhost nodejs]# tar -xf node-v12.18.4-linux-x64.tar.xz
[root@localhost nodejs]# ll
total 14352
drwxr-xr-x 6 jiangsu jiangsu 108 Sep 15 14:43 node-v12.18.4-linux-x64
-rw-r--r-- 1 root root 14696128 Sep 15 14:44 node-v12.18.4-linux-x64.tar.xz
[root@localhost nodejs]# vim /etc/profile
# 添加 如下配置
export NODE_HOME=/home/nodejs/node-v12.18.4-linux-x64
export PATH=$NODE_HOME/bin:$PATH
# 保存后,使环境变量生效
[root@localhost nodejs]# source /etc/profile
[root@localhost nodejs]# node -v
v12.18.4
[root@localhost nodejs]# npm -v
6.14.6
npm 是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题。
1.2 安装git
需要使用git方式下载head插件,下面安装git:
[root@localhost git]# yum install -y git
[root@localhost git]# git --version
git version 1.8.3.1
git的卸载命令 yum remove git
1.3 下载及安装head插件
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
open http://localhost:9100/
执行如下:
[root@localhost plugin]# git clone git://github.com/mobz/elasticsearch-head.git
[root@localhost plugin]# cd elasticsearch-head
[root@localhost elasticsearch-head]# npm install
...... 安装过程中可以先执行下面一步,配置elasticsearch.yml
安装完成后重启配置elasticsearch, 见第五步,测试
1.4 配置elasticsearch,允许head插件访问
修改config目录下的elasticsearch.yml 配置文件:
# vim /home/elasticsearch/elasticsearch-5.5.2/config/elasticsearch.yml
# 在最后添加一下内容:
http.cors.enabled: true
http.cors.allow-origin: "*"
1.5 测试
启动elasticsearch,进入到head目录,执行npm run start
[root@localhost elasticsearch-head]# su elastic
[elastic@localhost elasticsearch-head]$ sh /home/elasticsearch/elasticsearch-5.5.2/bin/elasticsearch -d
[elastic@localhost elasticsearch-head]$ npm run start

启动成功后,在浏览器访问:http://10.21.13.48:9100/ ,内部输入 http://10.21.13.48:9200/ 点击连接测试,输出黄色背景字体说明配置OK。

2、 Head插件添加和删除索引
2.1 第一种添加索引的方式
使用head插件添加索引: 在复合查询出添加,在地址栏输入 http://10.21.13.48:9200/student/ ,点击提交请求。右侧返回添加索引成功的信息。

在概览首页刷新,可以查到新添加的索引student。

2.2 第二种添加索引的方式
直接在索引页面新加索引。

结果如下:

2.3 删除索引的方式

3、Head插件添加、修改、删除文档
3.1 添加文档
在复合查询下添加数据: post方式 http://10.21.13.48:9200/book/java/
上述url中book是索引,java是类别, 没有写id,系统会自动生成一个id。

在数据浏览可以查看到刚添加的数据。

3.2 修改文档
修改文档的方式和添加一样,需要指定已经存在的id, 输入地址:http://10.21.13.48:9200/book/java/AXTOU6xFZCb1EGhtwyJw/

根据返回的结果和数据浏览查看数据修改成功。

3.3 查询文档
查询文档可以通过请求 http://10.21.13.48:9200/book/java/AXTOU6xFZCb1EGhtwyJw/ get方式

3.4 删除文档
删除文档 delete方式

4、 Head插件打开和关闭索引
4.1 关闭索引
方式一: Rest请求方式关闭索引: POST http://10.21.13.48:9200/student/_close/
方式二: 页面请求方式:

4.2 开启索引
方式一: Rest请求方式关闭索引: POST http://10.21.13.48:9200/student/_open/
方式二: 页面请求方式:

5、 Head插件增加索引和删除映射
5.1 增加索引映射
elasticsearch HTTP API 允许向索引(index)添加文档类型(type),或者向文档类型(type)中添加字段(field)。
如下: http://10.21.13.48:9200/student/_mapping/first/ PUT方式
添加json内容:
{
"properties": {
"name": {
"type": "keyword"
},
"birthday": {
"type": "date"
},
"desc": {
"type": "text"
}
}
}
简单说明:
mappings: 映射关键字
properties: 是添加指定文档类型的字段的关键字
first: 添加文档类型
name 类型是keyword
(keyword类型适合短词汇内容,比如邮件,姓名,性别等等,text类型适合长文本,可以分词,比如文章标题,文章内容等)

向存在的索引student中添加文档类型为 first的数据:
http://10.21.13.48:9200/student/first/1 POST 方式
{
"name":"lucky",
"birthday":"2020-01-01",
"desc":"a lucky dog"
}

5.2 查询索引映射关系
第一种查看方式: 利用Rest 调用方式:
http://10.21.13.48:9200/student/ Get方式 直接使用索引名称 能查到所有信息

第二种查看方式:
