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
}