版权声明

本文为Elastic开源社区版权所有,保证独立性和原创性,未获得授权和允许,任何组织和个人不得以任何方式传播或复制或分享,如若转发,请标注原创链接。否则必将追究法律责任。

知识内容输出不易,请尊重他人劳动成果。严禁随意传播、复制和盗用他人成果或文章内容用以商业或盈利目的!

1、Kibana 的基本使用

1.1 基于 REST 风格的 API

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD

1.2 Kibana优缺点

优点

  • 官方支持,兼容性好
  • 图形化界面,众多操作均可基于图形化操作完成,而无序任何代码
  • 功能强大,不仅有有丰富的可视化报表,还提供了非常强大的开发者工具
  • 快捷键丰富
  • 开发者工具
  • 样例数据

缺点

  • 对于 Dev 环境,每次使用都需要启动
  • 重负载,占用系统资源较大
  • 基于 Java 环境,启动慢

1.3 Dev Tools

  • 图形化界面
  • 高可配置性
  • Auto Format
  • 代码智能提示

1.4 练习数据

  • kibana 样例数据
  • 开源社区索引库

1.5 汉化

在 kibana 的配置文件中,i18n.locale配置项默认为en,******************

i18n.locale: "zh-CN"

2、Search API

********

GET /<index_name>/_search 
GET /_search 

********

  • size:******************,****** 10
  • from:**************。************,******0
  • **timeout:******************************。****************************,********************。************。

3、Index API

3.1 Settings:********(**:****** 3.2 ****)

********

PUT <index_name>

3.1.1 **** Settings

****************** settings

PUT <index_name>
{
  "settings": {}
}

****

********test_setting,****************** 1,********************** 1。

PUT test_setting
{
  "settings": {
    "number_of_shards": 1,  //************ 1
    "number_of_replicas": 1 //**********************
  }
}

3.1.2 **** settings

**** _setting ****************************

****

PUT /<index_name>/_settings

****

********test_setting****,**************************** 0。

PUT test_setting/_settings
{
  "number_of_replicas": 0
}

3.1.3 ************

****************************************。

**************

  • index.number_of_shards:******************,****** 1,**************************

************************** 1024,****************,******************,****************************************。export ES_JAVA_OPTS=“-Des.index.max_number_of_shards=128” ****************************************************

3.1.4 ************

********** _setting API ******************。

**************

  • **index.number_of_replicas:********************。****** 1,********** 0。
  • **index.refresh_interval:********************,******1s. ******** -1 **********。
  • **index.max_result_window:**from + size********** ********。****** 10000. ************************ from + size,************。****** Scroll**Search After**********************。

************ max_result_window ******,ES**********10000********************

3.2 ********

3.2.1 ********

PUT <index_name>
  • index_name:********

3.2.2 ************

  • ************
  • **************************:\/*?"<>| ********、,#
  • 7.0 ****************************** : ,**** 7.x **********************。
  • ******************
  • ********-_****+********,************************。
  • ******.****..
  • ******** 255 **********
  • **************.****,************************(********************)****************

****:******************,**************

  • **********************
  • ******************************
  • **************************,******** + ****************:**test_index

3.2.3 ****

3.3 ********

3.3.1 ********

DELETE /<index_name>
  • index_name:********

3.4 ****************

HEAD <index_name>

3.5 **************

ES ****************,******************

  • ********
  • **********
  • ********

3.6 Reindex

4、Document API

4.1 **************

enum OpType {
   INDEX(0),
   CREATE(1)
}
  • index:****(****),**********,****************
  • create:************,**********

****:

  • ******************。
  • ES******************** Primary Shard
  • ********************,op_type ****** create

4.1.1 Create:****

****** PUT **************************,**************,****** PUT ****************** create,******************************,**************************** create,ES************** update ****。

****:**************** id ** 1 ******,************** goods ********** id ** 1 ******,**** id ** 1 ************,**********。************ id

********

PUT /<index_name>/_doc/<_id>?op_type=create

********,********:

# **********:PUT /<index_name>/_doc/<_id>/_create
PUT /<index_name>/_create/<_id>/		

****

******** _id ** 1 ******,********** name ** content ********

PUT goods/_doc/1?op_type=create
{
  "name":"********",
  "content":"******2060**********"
}

** ****

PUT goods/_create/1
{
  "name":"********",
  "content":"******2060**********"
}

******************,**** created

{
  "_index": "goods",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

******************

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, document already exists (current version [1])",
        "index_uuid": "p_2BvtuOTDeqikl4JXPmgQ",
        "shard": "0",
        "index": "goods"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[1]: version conflict, document already exists (current version [1])",
    "index_uuid": "p_2BvtuOTDeqikl4JXPmgQ",
    "shard": "0",
    "index": "goods"
  },
  "status": 409
}

4.1.2 Index:****(******************)

** ES **,************** Index,****Index******,************************ ES ********,******************“********”。**********,****************

********

PUT /<index_name>/_doc/<_id>?op_type=index
{
  ...
}

********,********:

PUT /<index_name>/_doc/<_id>	
{
  ...
}

****

** goods ************** _id ** 1 ******,********** name ** content ********

PUT goods/_doc/1?op_type=index
{
  "name":"********",
  "content":"******2060**********"
}

** ****

PUT goods/_doc/1
{
  "name":"********",
  "content":"******2060**********"
}

******************,**** created

{
  "_index": "goods",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

****************,**** updated

{
  "_index": "goods",
  "_id": "1",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}

4.1.2 ******** id

POST /<target>/_doc/

****:************,************** id

POST test_index/_doc
{
  "test_field":"test",
  "test_title":"title"
}

4.2 ****** CRUD

4.2.1 Document Index API:**** API

** JSON ******************************************。******************************,****************************。

********

PUT /<target>/_doc/<_id>

PUT /<target>/_create/<_id>

POST /<target>/_create/<_id>

************

****

PUT /<target>/_doc/<_id>
{
  ...
}

****

******** _id ** 1 ******,********** test_field ** test_title ********

PUT goods/_doc/1?op_type=index
{
  "name":"********",
  "content":"******2060**********"
}

******** created,****************

******** updated,******************

4.2.2 Get API

1:******** id ******

GET <index>/_doc/<_id>

2:******** id **************

HEAD  <index>/_doc/<_id>

********,**** 200

200 - OK

**********,**** 404

{
  "statusCode": 404,
  "error": "Not Found",
  "message": "404 - Not Found"
}

**** HEAD ********************************,**************** GET ******,************,** id ** 1 ******************,**** found:false

GET goods/_doc/1

************

{
  "_index": "goods",
  "_id": "1",
  "found": false
}

3:_source API

**** _source API **************************,true ******,false ******,****** true。

GET <index>/_doc/<_id>?_source=false

****:********_source=false****,**************************

**************** _source ****,************ mata data。
GET <index>/_source/<_id>?

****:

### 4.2.3 Delete API

************** id ******,Document Delete API ******** id

DELETE /<index>/_doc/<_id>

******** id ** 1 ******

DELETE goods/_doc/1

4.2.4 Update API

********************

POST /<index>/_update/<_id>
{
  "doc": {
    "<field_name>": "<field_value>"
  }
}

****:7.x ****************POST test/_doc/1/_update**********

4.3 Multi get (mget) API

****

GET /_mget
{
  "docs": [
    {
      "_index": "<index_name>",
      "_id": "<_id>"
    },
    {
      "_index": "<index_name>",
      "_id": "<_id>"
    }
  ]
}

************************ id,************************ id。

********

GET <index_name>/_mget
{
  "docs": [
    {
      "_id": "<_id>"
    },
    {
      "_id": "<_id>"
    }
  ]
}

**********

GET /<index_name>/_mget
{
  "ids" : ["<_id_1>", "<_id_2>"]
}

**************

************************************,******************,******************,****:

GET <>/_mget
{
  "docs": [
    {
      "_id": "1",
      "_source": false
    },
    {
      "_id": "2",
      "_source": true
    }
  ]
}

4.4 Bulk API

****

POST /_bulk
POST /<index>/_bulk
{"action": {"mata data"}}
{"data"}

4.4.1 Create Document

************ create ********

PUT goods/_create/1
{
  "name": "********",
  "content": "******2060**********",
  "price": 12999,
  "type": "****",
  "level": "********",
  "createtime": "2060-10-01T08:00:00Z",
  "tags": [
    "********",
    "********",
    "********"
  ]
}

******** bulk API ****,********

POST /_bulk 
{"create":{"_index":"goods","_id":"1"}}
{"name":"********","content":"******2060**********","price":12999,"type":"****","level":"********","createtime":"2060-10-01T08:00:00Z","tags":["********","********","********"]}

4.4.2 Index Document

************************

PUT goods/_doc/2
{
  "name": "********",
  "content": "**************************",
  "price": 8999,
  "type": "****",
  "level": "********",
  "createtime": "2060-10-01T08:00:00Z",
  "tags": [
    "********",
    "********",
    "********"
  ]
}

******** bulk API ****,********

POST /_bulk
{"index":{"_index":"goods","_id":"2"}}
{"name":"********","content":"**************************","price":8999,"type":"****","level":"********","createtime":"2060-10-01T08:00:00Z","tags":["********","********"]}

4.4.3 Update Document

id ** 2 **************** 8999 ****** 6999

POST goods/_update/2
{
  "doc": {
    "price": 6999
  }
}

**** bulk API ****,********

POST goods/_bulk
{ "update": { "_index": "goods",  "_id": "2"} }
{ "doc" : {"price" : 6999} }

4.4.4 Delete Ducoment

POST /_bulk
{ "delete": { "_index": "goods",  "_id": "1" }}

********

******************,********** MySQL ************ ES,**************** es **********,******************************。

****

  • bulk api ** json ******************,**** delete **,****************** json **(mata data** source field data),****** json ************,******** json **********,**********;
  • bulk ******,****************,**********************,****************,****************

******

  • ****:************Json**************,**********************,********,************************
  • ****:********,******************。

4.5 DeleteByQuery

POST /<index_name>/_delete_by_query
{
  "query": {
    ...
  }
}

4.6 UpdateByQuery ☆

update_by_query ****** Script **** Pipeline ********,****************。

5、****:****