目录

  • 使用Kibana
  • 支持的命令
  • 1 GET _cat/health?v
  • 2 创建索引
  • 2.1创建索引时指定分片
  • 3 修改索引
  • 4 删除索引
  • 5 查看索引信息
  • 5.1 往索引里面存数据(新增Document)
  • 5.1.1PUT语法
  • 5.1.2 POST语法
  • 5.2 查询Document
  • 5.2.1 GET ID单数据查询
  • 5.2.2 GET _mget批量查询
  • 5.2.3 查询全部
  • 5.3 修改Document
  • 5.3.1 替换Document(全量替换)
  • 5.3.1 更新Document(partial update)
  • 5.4 删除Document
  • 6 bulk批量增删改(重点记忆)
  • 6.1 新增数据
  • 6.2 PUT操作新增或全量替换
  • 6.3 POST更新数据
  • 6.4 DELETE删除数据
  • 6.5 批量写操作


使用Kibana

kibana 往es存数据 使用kibana查询es数据_python

支持的命令

1 GET _cat/health?v

kibana 往es存数据 使用kibana查询es数据_Elastic_02


这个命令的意思是看这个软件的运行状态,也就是看这个软件的健康状态

Epoch(编号)      timestamp(时间戳)  cluster(集群名称)       status(健康状态)    node.total(节点总数)   node.data(数据节点数)   shards(分片数) 
1531290005 14:20:05    Elasticsearch   green           1         1        2
Pri(主分片数量)         relo(备份节点)                  init(正在初始化的)      unassign(未分配的)       pending_tasks(正在等待执行的任务) 
  2        0                    0        0             0
max_task_wait_time(挂起任务的等待时间)               active_shards_percent(活动的分片的占有百分比)
                 -                100.0%

2 创建索引

相当于创建一个数据库,这里所说的节点是 集群的数量

命令语法:PUT 索引名{索引配置参数}
index名称必须是小写的,且不能以下划线’_’,’-’,’+'开头。
在Elasticsearch中,默认的创建索引的时候,会分配5个primary shard,并为每个primary shard分配一个replica shard。在Elasticsearch中,默认的限制是:如果磁盘空间不足15%的时候,不分配replica shard。如果磁盘空间不足5%的时候,不再分配任何的primary shard。Elasticsearch中对shard的分布是有要求的。Elasticsearch尽可能保证primary shard平均分布在多个节点上。Replica shard会保证不和他备份的那个primary shard分配在同一个节点上。
创建默认索引。默认索引在7版本之前是5个,到7.x之后改成1个。

kibana 往es存数据 使用kibana查询es数据_kibana 往es存数据_03

PUT test_index1

执行了上面的这个命令,我们看到的结果是

kibana 往es存数据 使用kibana查询es数据_python_04

{
  "acknowledged" : true,      为true是执行成功
  "shards_acknowledged" : true,    分片也执行成功
  "index" : "test_index1"    这个数据库表的名字,也就是索引的名字
}

GET _cat/indices 查看所有索引

kibana 往es存数据 使用kibana查询es数据_Elastic_05

GET _cat/shareds 查看所有分片

一共10个,5个是主,5个是从

kibana 往es存数据 使用kibana查询es数据_数据_06

2.1创建索引时指定分片

注意: 编写时大括号要和put命令等不在一行。重点不能把{放在索引名后,kibana会把{当成索引名的一部分。
PUT test_index2
{
“settings”:{
“number_of_shards” : 2,
“number_of_replicas” : 1
}
}
解释说明:
number_of_shards:主分片数量
number_of_relicas:每个主分片的副本分片数量。

kibana 往es存数据 使用kibana查询es数据_数据_07

3 修改索引

命令语法:PUT 索引名/_settings{索引配置参数}
注意:索引一旦创建,primary shard数量不可变化,可以改变replica shard数量。
PUT test_index2/_settings
{
“number_of_replicas” : 2
}

kibana 往es存数据 使用kibana查询es数据_python_08

4 删除索引

命令语法:DELETE 索引名1[, 索引名2 …]
DELETE test_index1

5 查看索引信息

相当于查看表的信息

GET _cat/indices?v


health  status   index      uuid                      pri  rep   docs.count 
yellow  open   test_index   2PJFQBtzTwOUhcy-QjfYmQ   5    1          0  
docs.deleted       store.size        pri.store.size
          0       460b           460b

5.1 往索引里面存数据(新增Document)

kibana 往es存数据 使用kibana查询es数据_数据_09

在索引中增加文档。在index中增加document。
Elasticsearch有自动识别机制。如果增加的document对应的index不存在,自动创建index;如果index存在,type不存在,则自动创建type。如果index和type都存在,则使用现有的index和type。

5.1.1PUT语法

此操作为手工指定id的Document新增方式。
语法:PUT 索引名/类型名/唯一ID{字段名:字段值}

put test_index1/test_type/1
{
   "name":"test_doc_01",
   "remark":"first test elastic search",
   "order_no":1
}

以上代码的意思是 插入了一条数据

{
  "_index": "test_index", 新增的document在什么index中,
  "_type": "test_type", 新增的document在index中的哪一个type中。
  "_id": "1", 指定的id是多少
  "_version": 1, document的版本是多少,版本从1开始递增,每次写操作都会+1
  "result": "created", 本次操作的结果,created创建,updated修改,deleted删除
  "_shards": { 分片信息
    "total": 2, 分片数量只提示primary shard
    "successful": 1, 数据document一定只存放在index中的某一个primary shard中
    "failed": 0
  },
  "_seq_no": 0, 执行的序列号
  "_primary_term": 1 词条比对。
}

以上是插入数据之后返回的东西

如果使用PUT语法对同id的Document执行多次操作。是一种覆盖操作(全量替换)。如果需要Elasticsearch辅助检查PUT的Document是否已存在,可以使用强制新增语法。使用强制新增语法时,如果Document的id在Elasticsearch中已存在,则会报错。(version conflict, document already exists)

语法: 
PUT 索引名/类型名/唯一ID/_create{字段名:字段值} 
或 
PUT 索引名/类型名/唯一ID?op_type=create{字段名:字段值}。
PUT test_index/test_type/1/_create
{
   "name":"new_test_doc_01",
   "remark":"first test elastic search",
   "order_no":1
}
5.1.2 POST语法

POST相比Put功能多一个允许主键自动生成方式。
此操作为Elasticsearch自动生成id的新增Document方式。
语法:POST 索引名/类型名{字段名:字段值}

POST test_index/test_type
{
   "name":"test_doc_04",
   "remark":"forth test elastic search",
   "order_no":4
}

post 不仅仅支持明确主键,而且还支持自动生成主键6

5.2 查询Document

5.2.1 GET ID单数据查询

语法:GET 索引名/类型名/唯一ID
如:
GET test_index/test_type/1

查询结果是:

{
  "_index" : "test_index1",
  "_type" : "test_type",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "test_doc_01",
    "remark" : "first test elastic search",
    "order_no" : 1
  }
}
5.2.2 GET _mget批量查询
批量查询可以提高查询效率。推荐使用(相对于单数据查询来说)。

语法如下:


GET _mget
{
  "docs" : [
    {
      "_index" : "索引名",
      "_type" : "类型名",
      "_id" : "唯一ID值"
    }, {}, {}
  ]
}


GET 索引名/_mget
{
  "docs" : [
    {
      "_type" : "类型名",
      "_id" : "唯一ID值"
    }, {}, {}
  ]
}


GET 索引名/类型名/_mget
{
  "docs" : [
    {
     "_id" : "唯一ID值"
    },
{
     "_id" : "唯一ID值"
    }
  ]
}
5.2.3 查询全部

查询全部语法
GET 索引名/类型名/_search

5.3 修改Document

5.3.1 替换Document(全量替换)

和新增的PUT语法是一致。

PUT 索引名/类型名/唯一ID{字段名:字段值}

本操作相当于覆盖操作。全量替换的过程中,Elasticsearch不会真的修改Document中的数据,而是标记Elasticsearch中原有的Document为deleted状态,再创建一个新的Document来存储数据,当Elasticsearch中的数据量过大时,Elasticsearch后台回收deleted状态的Document。

5.3.1 更新Document(partial update)

语法:POST 索引名/类型名/唯一ID/_update{doc:{字段名:字段值}}

只更新某Document中的部分字段。这种更新方式也是标记原有数据为deleted状态,创建一个新的Document数据,将新的字段和未更新的原有字段组成这个新的Document,并创建。对比全量替换而言,只是操作上的方便,在底层执行上几乎没有区别。

POST test_index/test_type/1/_update
{
   "doc":{
      "name":" test_doc_01_for_update"
   }
}

5.4 删除Document

Elasticsearch中执行删除操作时,Elasticsearch先标记Document为deleted状态,而不是直接物理删除。当Elasticsearch存储空间不足或工作空闲时,才会执行物理删除操作。标记为deleted状态的数据不会被查询搜索到。

语法:DELETE 索引名/类型名/唯一ID

DELETE     test_index/test_type/1

6 bulk批量增删改(重点记忆)

使用bulk语法执行批量增删改。语法格式如下:

注意:_bulk的两个{}都不要出现换行等操作,两个{}使用回车符进行识别。

POST _bulk
{ “action_type” : { “metadata_name” : “metadata_value” } }
{ document datas | action datas }

语法中的action_type可选值为:
create : 强制创建,相当于PUT 索引名/类型名/唯一ID/_create。主键必须有。
index: 普通的POST操作,相当于创建Document或全量替换
update: 更新操作(partial update),相当于 POST 索引名/类型名/唯一ID/_update
delete: 删除操作

6.1 新增数据

新增数据:
POST _bulk
{ "create" : { "_index" : "test_index" , "_type" : "test_type", "_id" : "1" } }
{ "field_name" : "field value" }

6.2 PUT操作新增或全量替换

PUT操作新增或全量替换
POST _bulk
{ "index" : { "_index" : "test_index", "_type" : "test_type" , "_id" : "2" } }
{ "field_name" : "field value 2" }

6.3 POST更新数据

POST _bulk
{ "update" : { "_index" : "test_index", "_type" : "test_type" , "_id" : 2, "_retry_on_conflict" : 3 } }
{ "doc" : { "field_name" : "partial update field value" } }

6.4 DELETE删除数据

POST _bulk
{ "delete" : { "_index" : "test_index", "_type" : "test_type", "_id" : "2" } }

6.5 批量写操作

POST _bulk
{ "create" : { "_index" : "test_index" , "_type" : "test_type", "_id" : "10" } }
{ "field_name" : "field value" }
{ "index" : { "_index" : "test_index", "_type" : "test_type" , "_id" : "20" } }
{ "field_name" : "field value 2" }
{ "update" : { "_index" : "test_index", "_type" : "test_type" , "_id" : 20,
 "_retry_on_conflict" : 3 } }
{ "doc" : { "field_name" : "partial update field value" } }
{ "delete" : { "_index" : "test_index", "_type" : "test_type", "_id" : "2" } }
注意:
bulk语法中要求一个完整的json串不能有换行。不同的json串必须使用换行分隔。
多个操作中,如果有错误情况,不会影响到其他的操作,只会在批量操作返回结果中标记失败。
bulk语法批量操作时,bulk request会一次性加载到内存中,如果请求数据量太大,
性能反而下降(内存压力过高),需要反复尝试一个最佳的bulk request size。
一般从1000~5000条数据开始尝试,逐渐增加。如果查看bulk request size的话,
一般是5~15MB之间为好。


bulk语法要求json格式是为了对内存的方便管理,和尽可能降低内存的压力。
如果json格式没有特殊的限制,Elasticsearch在解释bulk请求时,需要对任意格式的json进行解释处理,
需要对bulk请求数据做json对象会json array对象的转化,那么内存的占用量至少翻倍,
当请求量过大的时候,对内存的压力会直线上升,且需要jvm gc进程对垃圾数据做频繁回收,
影响Elasticsearch效率。
生产环境中,bulk api常用。


都是使用java代码实现循环操作。一般一次bulk请求,执行一种操作。如:批量新增10000条数据等。