(07)ElasticSearch 使用Kibana实现基础增删改查

  本篇的主要内容:认识ElasticSearch的客户端Kibana、添加索引、查询索引、添加文档、修改文档、查询文档、删除文档、删除索引

(1)Kibana的操作界面如下:

(2)添加索引

PUT /lib/
{
  "settings":{
    "index":{
      "number_of_shards":3,
      "number_of_replicas":0
    }
  }
}

  索引名是lib
  分片数number_of_shards是3,分片数一旦确定是不能修改
  备份数量number_of_replicas是0

(2_1)添加默认索引

PUT lib2

  分别执行 GET lib 和 GET lib2 查看区别:默认分片数是5 备份数是1

{
  "lib": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1569314525913",
        "number_of_shards": "3",
        "number_of_replicas": "0",
        "uuid": "UtdhpvQXRxibzTXkj9HPSA",
        "version": {
          "created": "6020499"
        },
        "provided_name": "lib"
      }
    }
  }
}
{
  "lib2": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1569314833757",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "5mwEdY0ZQu2ZP-uqrOFyGg",
        "version": {
          "created": "6020499"
        },
        "provided_name": "lib2"
      }
    }
  }
}

(3)查看索引

  下面的几种方法都可以查到所有的索引名称,只是展示的内容不同,_all 包含了aliases、mappings、settings

GET _all
GET _mappings
GET _mapping
GET _aliases
GET _alias
GET _settings
GET _all结果如下:
{
  "lib2": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1569314833757",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "5mwEdY0ZQu2ZP-uqrOFyGg",
        "version": {
          "created": "6020499"
        },
        "provided_name": "lib2"
      }
    }
  },
  "lib": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1569314525913",
        "number_of_shards": "3",
        "number_of_replicas": "0",
        "uuid": "UtdhpvQXRxibzTXkj9HPSA",
        "version": {
          "created": "6020499"
        },
        "provided_name": "lib"
      }
    }
  }
}

(3_1)查询某个索引

  以下的几个方法可以查询某个索引,展示的内容不同,GET lib 展示的内容包含了其他3项

GET lib
GET lib/_alias
GET lib/_mapping
GET lib/_settings

  GET lib

{
  "lib": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1569314525913",
        "number_of_shards": "3",
        "number_of_replicas": "0",
        "uuid": "UtdhpvQXRxibzTXkj9HPSA",
        "version": {
          "created": "6020499"
        },
        "provided_name": "lib"
      }
    }
  }
}

(4)添加文档

  添加文档类似于向数据库中添加记录,有两种方式put和post,一个索引下面只能添加 一种类型

  使用PUT添加文档,如下:

PUT /lib/user/1
{
    "first_name":"Jane",
    "last_name":"Smith",
    "age":32,
    "about":"I like to collect rock albums",
    "interests":[ "music" ]
}

  其中lib是创建的索引,user是类型名,1是添加的文档的id,使用PUT添加文档必须要有id。

  如果使用POST添加文档,可以不指定id,elasticsearch会自己生成id,如下:

POST /lib/user/
{
    "first_name":"Fir",
    "age":23,
    "about":"I like to build cabinets",
    "interests":[ "forestry" ]
}

(5)查询文档

  现在只介绍两种查询 文档的方式,根据id查询和查询出所有文档

  查询id为1的文档

GET lib/user/1

  结果如下:

{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "first_name": "Jane",
    "last_name": "Smith",
    "age": 32,
    "about": "I like to collect rock albums",
    "interests": [
      "music"
    ]
  }
}

  查询索引为lib、类型为user下的所有文档

GET /lib/user/_search
{
  "query":{
    "match_all":{}
  }
}

  结果如下:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "lib",
        "_type": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "first_name": "Jane",
          "last_name": "Smith",
          "age": 32,
          "about": "I like to collect rock albums",
          "interests": [
            "music"
          ]
        }
      },
      {
        "_index": "lib",
        "_type": "user",
        "_id": "y6mQYm0BJNrNYF2GcZz1",
        "_score": 1,
        "_source": {
          "first_name": "Fir",
          "age": 23,
          "about": "I like to build cabinets",
          "interests": [
            "forestry"
          ]
        }
      }
    ]
  }
}

  查询lib索引下类型是user,文档id是1的文档,只查询字段age和about

GET /lib/user/1?_source=age,about

  结果如下:

{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "about": "I like to collect rock albums",
    "age": 32
  }
}

(6)修改文档

  修改文档有两种方法,一个是用PUT直接覆盖掉原来的,另一个就是用POST只修改要改的部分。

  第一中方法,将id为1的age修改为36:

PUT /lib/user/1
{
    "first_name":"Jane",
    "last_name":"Smith",
    "age":36,
    "about":"I like to collect rock albums",
    "interests":[ "music" ]
}

  第二中方法,将id为1的age修改为33

POST /lib/user/1/_update
{
    "doc":{
       "age":33
    }
}

(7)删除文档

DELETE /lib/user/1

(8)删除索引

DELETE /lib

 

转载于:https://www.cnblogs.com/javasl/p/11405353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值