## 创建索引 put csdn
返回结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "csdn"
}
1.2 查询索引信息
## 查询索引 get csdn
返回结果
{
"csdn" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1645948029364",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "tl3tRSlZQCqfM_PH8s7KaQ",
"version" : {
"created" : "7080099"
},
"provided_name" : "csdn"
}
}
}
}
1.3 删除索引
DELETE csdn
返回结果
{
"acknowledged" : true
}
1.4修改索引状态
## 修改索引状态 ## 关闭 索引 无法进行读写操作 post csdn/_close ## 重新打开索引 post csdn/_open
返回结果
{
"acknowledged" : true,
"shards_acknowledged" : true
}
1.5 判断索引是否存在
## 判断索引是否存在 head csdn
返回结果
-----索引存在---
200 - OK
-----索引不存在---
{"statusCode":404,"error":"Not Found","message":"404 - Not Found"}
2.映射操作
2.0 映射相关参数说明
index 决定字段是否可以被用户搜索
store 决定字段是否被存储
type 字段的类型 注意5.0后版本 没有String类型 使用 text 和 keyword 代替
text类型 需要分词的设置为text,不支持聚合
keyword类型 不需要分词的设置为 keyword,支持聚合
post csdn/_mapping
{
"properties":{
"id":{
"index":true,
"store":true,
"type":"integer"
},
"name":{
"index":true,
"store":false,
"type":"text",
"analyzer":"ik_max_word"
},
"age":{
"index":true,
"store":false,
"type":"integer"
}
}
}
返回结果
{
"acknowledged" : true
}
2.2 创建索引时建立映射
put csdn
{
"mappings":{
"properties":{
"id":{
"index":true,
"store":true,
"type":"integer"
},
"name":{
"index":true,
"store":false,
"type":"text",
"analyzer":"ik_max_word"
},
"age":{
"index":true,
"store":false,
"type":"integer"
}
}
}
}
返回结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "csdn"
}
2.3 映射查询
## 根据索引查询映射信息 get csdn/_mapping
返回结果
{
"csdn" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"id" : {
"type" : "integer",
"store" : true
},
"name" : {
"type" : "text",
"analyzer" : "ik_max_word"
}
}
}
}
}
3.文档操作
3.1 添加文档
post csdn/_doc
{
"id":20,
"name":"赵六",
"age":25
}
返回结果
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
3.2查询文档
3.2.1根据文档唯一ID查询文档
get csdn/_doc/gPc9On8BcPj1STPlFokb
返回结果
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id" : 20,
"name" : "赵六",
"age" : 25
}
}
3.2.2 查询所有文档
## 查询所有文档 get csdn/_doc/_search
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵六",
"age" : 25
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
}
]
}
}
3.3 修改文档
## 根据文档id修改文档
put csdn/_doc/gPc9On8BcPj1STPlFokb
{
"id":20,
"name":"赵牛修改",
"age":11
}
返回结果
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
注意 : ES中文档创建后不能被修改, 进行修改操作只是将原文档删除,重新添加一个文档,并将版本号加一
ID 存在就是修改操作 不存在就是添加操作
3.3.1 查询文档版本号get csdn/_doc/_search?version=true
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "MfdEOn8BcPj1STPlTYra",
"_version" : 1,
"_score" : 1.0,
"_source" : {
"query" : {
"match_all" : { }
}
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_version" : 3,
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 11
}
}
]
}
}
3.4 删除文档
delete csdn/_doc/gPc9On8BcPj1STPlFokb
返回结果
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_version" : 4,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1
}



