启动
ElasticSearch
1、进入目录 elasticsearch-5.6.16
2、打开Terminal,键入:./elasticsearch
3、检查是否启动成功
Kibana
1、进入目录 kibana-5.6.16-darwin-x86_64
2、打开Terminal,键入:./kibana
3、检查是否启动成功
4、打开http://localhost:5601
Kibana常用操作
1、快速检查集群的健康状况
GET /_cat/health?v
注意:后面如果跟上?v会显示表头,否则不显示表头
- green:每个索引的primary shard和replica shard都是active状态的
- yellow:每个索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态
- red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了
Q&A 为什么现在会处于一个yellow状态?
/**
我们现在就一个笔记本电脑,就启动了一个es进程,相当于就只有一个node。现在es中有一个index,就是kibana自己内置建立的index。
由于默认的配置是给每个index分配5个primary shard和5个replica shard,而且primary shard和replica shard不能在同一台机器上(为了容错)。
现在kibana自己建立的index是1个primary shard和1个replica shard。当前就一个node,
所以只有1个primary shard被分配了和启动了,但是一个replica shard没有第二台机器去启动。
*/
2、快速查看集群中有哪些索引
GET /_cat/indices?v
3、简单的索引操作
创建索引: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
删除索引: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
4、商品的CRUD操作
PUT /Index/Type/Id:新增/替换
GET /Index/Type/id: 查询
POST /Index/Type/id/_update : 修改
DELETE /Index/Type/Id : 删除
4.1 新增商品:新增文档,建立索引
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
}
es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索
4.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"
]
}
}
4.3 修改商品:替换文档
PUT /ecommerce/product/1
{
"name" : "jiaqiangban gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
替换方式有一个不好,即使必须带上所有的field,才能去进行信息的修改,例如
PUT /ecommerce/product/1
{
"name" : "jiaqiangban gaolujie yagao"
}
通过查询,发现id为1的商品只有那么字段了
4.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
}
}
只会更新name字段
4.5 删除商品:删除文档
DELETE /ecommerce/product/1
返回结果:
{
"found": true,
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 9,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
如果删除的document在原来的type中不存在
{
"_index": "ecommerce",
"_type": "product"