码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-08-25 12:11:57      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:rds   技术   shard   res   另一个   failed   ati   date   str   

ElasticSearch是基于索引的,所以在操作文档之前先创建索引。下面是Kibana的操作界面:

技术图片

1、添加索引:PUT

  PUT /lib/
  {
    "settings":{
      "index":{
        "number_of_shards":3,
        "number_of_replicas":0
      }
    }
  }
  索引名是lib
  分片数number_of_shards是3,分片数一旦确定是不能修改
  备份数量number_of_replicas是0

2、使用默认配置创建索引:

  创建一个索引名是lib2的索引

  PUT lib2

3、查看索引信息:GET

  GET lib/_settings

 1 {
 2   "lib": {
 3     "settings": {
 4       "index": {
 5         "creation_date": "1566687468270",
 6         "number_of_shards": "3",
 7         "number_of_replicas": "0",
 8         "uuid": "UVAIaBtTTOu4OlGirLs_hg",
 9         "version": {
10           "created": "6020499"
11         },
12         "provided_name": "lib"
13       }
14     }
15   }
16 }

  与创建时一样分片数是3,备份数是0。

  查看一下默认创建的索引,分片数默认是5,备份数默认是1,如下:

  GET lib2/_settings

 1 {
 2   "lib2": {
 3     "settings": {
 4       "index": {
 5         "creation_date": "1566688286573",
 6         "number_of_shards": "5",
 7         "number_of_replicas": "1",
 8         "uuid": "XEDTNi2STmC-gjYWCGo0Fw",
 9         "version": {
10           "created": "6020499"
11         },
12         "provided_name": "lib2"
13       }
14     }
15   }
16 }

4、查看所有索引:GET _all/_settings,结果如下,上面共建立了2个索引。

 1 {
 2   "lib": {
 3     "settings": {
 4       "index": {
 5         "creation_date": "1566687468270",
 6         "number_of_shards": "3",
 7         "number_of_replicas": "0",
 8         "uuid": "UVAIaBtTTOu4OlGirLs_hg",
 9         "version": {
10           "created": "6020499"
11         },
12         "provided_name": "lib"
13       }
14     }
15   },
16   "lib2": {
17     "settings": {
18       "index": {
19         "creation_date": "1566688286573",
20         "number_of_shards": "5",
21         "number_of_replicas": "1",
22         "uuid": "XEDTNi2STmC-gjYWCGo0Fw",
23         "version": {
24           "created": "6020499"
25         },
26         "provided_name": "lib2"
27       }
28     }
29   }
30 }

5、添加文档

  添加文档类似于向数据库中添加记录

  使用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" ]
}

  生成结果如下,自动生成了id:RQn7xWwBihLrhcI-a2H7

{
  "_index": "lib",
  "_type": "user",
  "_id": "RQn7xWwBihLrhcI-a2H7",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

6、查询文档

  GET /类型/id,如查询lib索引下类型是user,文档id是1的文档:GET /lib/user/1,显示所有字段

  只查询需要的字段  GET /lib/user/1?_source=age,about

7、修改文档

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

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

1 PUT /lib/user/1
2 {
3     "first_name":"Jane",
4     "last_name":"Smith",
5     "age":36,
6     "about":"I like to collect rock albums",
7     "interests":[ "music" ]
8 }

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

1 POST /lib/user/1/_update
2 {
3     "doc":{
4         "age":33
5     }
6 }

8、删除文档

1 DELETE /lib/user/1

9、删除索引

1 DELETE /lib2

 

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

标签:rds   技术   shard   res   另一个   failed   ati   date   str   

原文地址:https://www.cnblogs.com/javasl/p/11405353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!