一、Kibana插件的安装和使用
1.Kibana是ElasticSearch的可视化平台,依赖ElasticSearch,需要优先安装ElasticSearch。
kibana需要依赖elasticsearch,到https://www.elastic.co/downloads/past-releases下载和es版本对应的kibana的版本号
然后到kibana安装目录的config下,编辑kibana.yml配置文件,添加如下配置:
#配置本机的ip
server.host: "localhost"
elasticsearch.url: "http://localhost:9200"
2.进入bin目录下面启动kibana,在浏览器上访问localhost:5601访问kibana
3.kibana对Elasticsearch的相关操作。
查询es的状态 GET _cat/health?v; 也可以使用 GET /_cat/nodes?v 来查看当前节点数;
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1541137503 13:45:03 elasticsearch yellow 1 1 6 6 0 0 6 1 - 50.0%
如何快速了解集群的健康状况?green、yellow、red?
green:每个索引的primary shard和replica shard都是active状态的
yellow:每个索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态
red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了
3.1 创建索引:PUT /test_index?pretty
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_index XmS9DTAtSkSZSwWhhGEKkQ 5 1 0 0 650b 650b
yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb
3.2 删除索引:DELETE /test_index?pretty
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb
新增商品:新增文档,建立索引
PUT /index/type/id
{
"json数据"
}
PUT /ecommerce/product/1
{
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
PUT /ecommerce/product/2
{
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags": [ "fangzhu" ]
}
PUT /ecommerce/product/3
{
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags": [ "qingxin" ]
}
es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索
(2)查询商品:检索文档
GET /index/type/id
GET /ecommerce/product/1
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
(3)修改商品:替换文档
PUT /ecommerce/product/1
{
"name" : "jiaqiangban gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
PUT /ecommerce/product/1
{
"name" : "jiaqiangban gaolujie yagao"
}
替换方式有一个不好,即使必须带上所有的field,才能去进行信息的修改
(4)修改商品:更新文档
POST /ecommerce/product/1/_update
{
"doc": {
"name": "jiaqiangban gaolujie yagao"
}
}
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 8,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
(5)删除商品:删除文档
DELETE /ecommerce/product/1
{
"found": true,
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 9,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"found": false
}
二、Head插件安装和使用
2.1 下载head插件:https://github.com/mobz/elasticsearch-head
Elasticsearch 5.x后不支持插件,head作为单独服务独立运行。
step1:下载zip到本地,并解压到当前文件夹
step2:安装node.js,因为之前有在本地安装过就不再演示了,
step3:进入nodejs包下面,我的路径是C:\Program Files\nodejs。将grunt安装为全局命令 npm install -g grunt -cli
进入head目录下面,输入npm install,进行安装pathomjs
step4: 运行head插件。命令: grunt server
默认情况下,elasticsearch会在elasticsearch-head连接到的端口9200上公开http rest API。
在elasticsearch中启用CORS
如果不作为elasticsearch的插件运行(甚至不能从版本5运行),则必须在elasticsearch中启用CORS,否则您的浏览器将拒绝看似不安全的请求。
在elasticsearch.yml中添加配置:
http.cors.enabled: true
http.cors.allow-origin: "*"
访问:localhost:9100 就可以访问