Restful是一种风格,ES推荐这种风格(现在很多接口都说是要按照restful这种形式)
表象一点说:
URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作
表现层转移啥这些说法的还是太晦涩了,非常的论文,实际看大致的实例就跟我上面的那句话描述一样。
然后操作一下基本的命令按照Restful
(1)创建一个索引
PUT /索引名/~类型名~/文档id
{请求体}
# PUT 创建命令 test1 索引 type1 类型 1 id
PUT zp/type1/1
{
"name": "zp",
"age": 29
}
# 返回结果
# 警告信息: 不支持在文档索引请求中的指定类型
# 而是使用无类型的断点(/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
"_index" : "zp", # 索引
"_type" : "type1", # 类型(已经废弃)
"_id" : "1", # id
"_version" : 1, # 版本
"result" : "created", # 操作类型
"_shards" : { # 分片信息
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
type类型的如下:
指定字段的类型(创建规则)
获取具体的索引规则
get index
{
"error" : {
"root_cause" : [
{
"type" : "resource_already_exists_exception",
"reason" : "index [zp/fE0TypKvS3u6jrP_hCNkgg] already exists",
"index_uuid" : "fE0TypKvS3u6jrP_hCNkgg",
"index" : "zp"
}
],
"type" : "resource_already_exists_exception",
"reason" : "index [zp/fE0TypKvS3u6jrP_hCNkgg] already exists",
"index_uuid" : "fE0TypKvS3u6jrP_hCNkgg",
"index" : "zp"
},
"status" : 400
}
# GET test2
{
"test2" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"birthday" : {
"type" : "date"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1599708623941",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "ANWnhwArSMSl8k8iipgH1Q",
"version" : {
"created" : "7080099"
},
"provided_name" : "test2"
}
}
}
}
# 查看默认的规则
PUT /test3/_doc/1
{
"name": "张三",
"age": 28,
"birthday": "1991-01-01"
}
# GET test3
{
"test3" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"birthday" : {
"type" : "date"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1599708906181",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "LzPLCDgeQn6tdKo3xBBpbw",
"version" : {
"created" : "7080099"
},
"provided_name" : "test3"
}
}
}
}
修改索引 POST
# 只会修改指定项,其他内容保证不变,我这里给他添加一个昵称
POST /test3/_doc/1/_update
{
"doc": {
"nickname":"法外狂徒"
}
}
# GET test/_doc/1
{
"_index" : "test3",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "张三",
"age" : 28,
"birthday" : "1991-01-01",
"nickname" : "法外狂徒"
}
}
删除索引
DELETE test