内容简介:es中的索引对应mysql的数据库、类型对应mysql的表、文档对应mysql的记录、映射对应mysql的索引索引:index类型:type
es中的索引对应 mysql 的数据库、类型对应mysql的表、文档对应mysql的记录、映射对应mysql的索引
索引:index
类型:type
映射:mappings
1、创建索引
在kibana的Dev Tools中输入如下
PUT /lib/ { "settings":{ "index":{ "number_of_shards":3, "number_of_replicas":0 } } }
索引的名称为lib
number_of_shards:分片的数量为3,分片的数量一旦确定了就不能修改
number_of_replicas: 备份的数量为1,由于就1台服务器因此备份数量为0
创建成果后在右边显示如下
{ "acknowledged": true, "shards_acknowledged": true, "index": "lib" }
直接创建一个索引,会使用默认配置
PUT lib2
查看已经创建好的索引的配置
GET /lib/_settings
GET /lib2/_settings
查看所有索引的配置
GET _all/_settings
2、在索引下添加文档
# user 是类型
# 1 是文档的id
PUT /lib/user/1 { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": ["music"] }
右边显示结果如下
{ "_index": "lib", "_type": "user", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
如果添加文档时没有指定id,那么这个id会由es自动生成,那么我们要使用POST方式添加文档
POST /lib/user/ { "first_name": "Douglas", "last_name": "Fir", "age": 23, "about": "I like to build cabinets", "interests": ["forestry"] }
结果如下
{ "_index": "lib", "_type": "user", "_id": "3z0vM2kBpR5Gle8qjwsu", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
3、查询文档
# 根据文档id进行查询
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" ] } }
GET /lib/user/3z0vM2kBpR5Gle8qjwsu
# 查看文档的部分信息
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 } }
4、修改文档
# 使用一个新的文档覆盖之前的文档,会覆盖文档所有字段的值
PUT /lib/user/1 { "first_name": "Jane", "last_name": "Smith", "age": 36, "about": "I like to collect rock albums", "interests": ["music"] }
结果如下,会提示结果为updated
{ "_index": "lib", "_type": "user", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
# 修改文档中的指定字段的值
POST /lib/user/1/_update { "doc":{ "age":33 } }
结果如下
{ "_index": "lib", "_type": "user", "_id": "1", "_version": 3, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
5、删除文档
DELETE /lib/user/1
结果如下
{ "_index": "lib", "_type": "user", "_id": "1", "_version": 4, "result": "deleted", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
6、删除一个索引
DELETE lib2
结果如下
{
“acknowledged”: true
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Golang操作MySQL增删改查
- 使用 mongoose 操作 mongodb 增删改查
- 每日一博 | Elasticsearch 与 Thinkphp 增删改查操作
- 每日一博 | Elasticsearch 与 Thinkphp 增删改查操作
- Go中map的操作与使用(增删改查)
- 使用HDFS dfs命令对文件进行增删改查操作
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
C++ How to Program (5th Edition) (How to Program)
Harvey & Paul) Deitel & Associates / Prentice Hall / 2005-01-05 / USD 98.00
With over 250,000 sold, Harvey and Paul Deitel's C++ How to Program is the world's best-selling introduction to C++ programming. Now, this classic has been thoroughly updated! The Deitels' groundbreak......一起来看看 《C++ How to Program (5th Edition) (How to Program)》 这本书的介绍吧!