ES:Head插件下载安装及数据导入

一. head插件下载及安装

1.head插件下载

下载安装head插件是为了更直观的看到数据的变化,并可以实现对数据的简单操作。elasticsearch5.0以下版本可以直接在elasticsearch目录下执行对应的命令安装head插件,但是5.0以上不支持直接安装,本篇介绍5.6.9以上head插件的安装步骤。

下载地址:https://github.com/mobz/elasticsearch-head

可解压到任意目录,但要与elasticsearch的安装目录区分开!!!

2.node.js下载并自行安装

因为head是一个Node.js项目。所以,如果没有安装nodejs需要先安装Node.js,以 Node.js v4.4.3 LTS(长期支持版本)版本为例。

检测环境变量中是否配置了Node.js,打开命令行输入命令"path"后可查看:
ES:Head插件下载安装及数据导入
输入命令"node --version"可查看node版本:
ES:Head插件下载安装及数据导入

3.安装grunt

因为运行head需要借助grunt命令,所以需要安装grunt。进入Node.js目录下,执行命令npm install -g grunt-cli,将grunt安装位全局命令。
ES:Head插件下载安装及数据导入
进入head主目录执行npm install安装grunt,安装完成后执行grunt -version查看是否安装成功,会显示安装的版本号。
ES:Head插件下载安装及数据导入

4.配置Head

1). 进入安装目录下的config目录,修改elasticsearch.yml文件,在文件的末尾加入以下代码

http.cors.enabled: true 
http.cors.allow-origin: "*"
node.master: true
node.data: true

2)network.host: 192.168.0.1的注释并改为network.host: 0.0.0.0;去掉cluster.name node.name http.port 的注释(也就是去掉#)
3). 双击elasticsearch.bat重启es
4). 在下载好的head文件夹内找到Gruntfile.js,在对应的位置加上hostname: ‘*’
5).启动head:命令行进入head文件夹执行执行npm install 安装完成后执行grunt server 或者npm run start 运行head插件,如果不成功重新安装grunt。成功如下:

Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

6).浏览器下访问http://localhost:9100/

5.postman导入数据示例

1).创建索引和mapping

--put请求  localhost:9200/local_library索引名
mappings:
{
    "mappings":{
        "book":{
            "properties":{
                "book_id":{
                    "type":"long",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "book_code":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "book_name":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "book_price":{
                    "type":"integer",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "book_author":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "book_desc":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                }
            }
        }
    }
}

ES:Head插件下载安装及数据导入
如果想试试用postman新增数据,可以添加如下代码

-- post请求  localhost:9200/local_library/book/1   索引/对象/id

{
	"book_id": 1,
	"book_code": "A0001",
	"book_name": "琴帝",
	"book_price": 180,
	"book_author": "唐家三少",
	"book_desc": "最好看的玄幻小说!"
}

结果可在head数据浏览标签下查看,也可以通过postman运行如下代码查看:

-- post请求  localhost:9200/local_library/_search?pretty
{
	"query": {
		"match": {
			"id": "1"
		}
	}
}