美文网首页
三、ElasticSearch-Head插件安装及使用

三、ElasticSearch-Head插件安装及使用

作者: 缘木与鱼 | 来源:发表于2020-09-29 16:19 被阅读0次

    ElasticSearch (3) Head插件安装及使用

    elasticsearch head插件是一个入门级的elasticsearch 前端插件,下面进行安装:

    1、 Head插件安装

    前提: head插件是Nodejs实现的,所以需要先安装Nodejs。

    1.1 安装nodejs

    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插件

    head插件的git地址

    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
    
    npm-run.png

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

    head-测试连接.png

    2、 Head插件添加和删除索引

    2.1 第一种添加索引的方式

    使用head插件添加索引: 在复合查询出添加,在地址栏输入 http://10.21.13.48:9200/student/ ,点击提交请求。右侧返回添加索引成功的信息。

    添加索引.png

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

    添加索引结果.png

    2.2 第二种添加索引的方式

    直接在索引页面新加索引。

    直接添加索引.png

    结果如下:


    直接添加索引结果.png

    2.3 删除索引的方式

    删除索引.png

    3、Head插件添加、修改、删除文档

    3.1 添加文档

    在复合查询下添加数据: post方式 http://10.21.13.48:9200/book/java/

    上述url中book是索引,java是类别, 没有写id,系统会自动生成一个id。

    索引添加数据.png

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

    添加数据结果.png

    3.2 修改文档

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

    修改文档.png

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

    修改文档结果.png

    3.3 查询文档

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

    查询文档.png

    3.4 删除文档

    删除文档 delete方式

    删除文档.png

    4、 Head插件打开和关闭索引

    4.1 关闭索引

    方式一: Rest请求方式关闭索引: POST http://10.21.13.48:9200/student/_close/

    方式二: 页面请求方式:

    关闭索引.png

    4.2 开启索引

    方式一: Rest请求方式关闭索引: POST http://10.21.13.48:9200/student/_open/

    方式二: 页面请求方式:

    开启索引.png

    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类型适合长文本,可以分词,比如文章标题,文章内容等)

    添加索引映射关系.png

    向存在的索引student中添加文档类型为 first的数据:

    http://10.21.13.48:9200/student/first/1 POST 方式

    {
        "name":"lucky",
        "birthday":"2020-01-01",
        "desc":"a lucky dog"
    }
    
    添加映射关系数据.png

    5.2 查询索引映射关系

    第一种查看方式: 利用Rest 调用方式:

    http://10.21.13.48:9200/student/ Get方式 直接使用索引名称 能查到所有信息

    查询索引映射关系.png

    第二种查看方式:


    索引映射关系.png

    相关文章

      网友评论

          本文标题:三、ElasticSearch-Head插件安装及使用

          本文链接:https://www.haomeiwen.com/subject/ebxluktx.html