目录
0. 数据格式说明
1. ES的基本操作
1.1 索引操作
1.1.1 建立索引
1.1.2 删除索引
1.1.3 查询索引
1.2 映射操作
1.2.1 建立映射
1.2.2 查询映射
1.3 基本操作-CRUD
1.3.1 新增和替换文档
1.3.2 查询文档
在实战开始之前,为了便于书写和沟通,本文先来约定一下如何在文章中表达请求和响应的信息:
1. 假设通过Postman工具或者Kibana向服务器发送一个PUT类型的请求,地址是:http://{IP}:9200/test001/article/1
2. 请求的内容是JSON格式的,内容如下:
{
"settings": {
"number_of_shards": 5 //设置5个片区
, "number_of_replicas": 1 //设置1个备份
}
}
对于上面的请求,本文中就以如下格式描述:
PUT /user
{
"settings": {
"number_of_shards": 5 //设置5个片区
, "number_of_replicas": 1 //设置1个备份
}
}
语法:PUT /索引名
# 发送请求
# 在没有特殊设置的情况下,默认有5个分片,1个备份,也可以通过请求参数的方式来指定.。
PUT test_index
{}
# 返回值
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test_index"
}
结果返回创建成果,同时kinbana显示警告,从ES7开始默认的分片数将从5修改为1,如果需要继续指定分片数,则需要采用index参数模板传入参数。
语法:DELETE /索引名
# 发送请求
DELETE test_03
{}
# 返回值
{
"acknowledged" : true
}
删除成功:
语法:GET /索引名
# 发送请求
GET test_index
# 返回值
{
"test_index" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1664806581608",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "qcVTOE2VRpieJi3sZOjZwg",
"version" : {
"created" : "6050499"
},
"provided_name" : "test_index"
}
}
}
}
查询结果中包括了别名(aliases)、映射(mappings)以及配置参数(mappings)等详细信息。
语法:POST/索引/映射
# 发送请求
POST /test_index/student
{
"mappings": {
"info": {
"properties": {
"name": {
"type": "text"
},
"clazz":{
"type":"string"
},
"id":{
"type":"double"
}
}
}
}
}
# 返回值
{
"_index" : "test_index",
"_type" : "student",
"_id" : "bB1InoMBWJmEB7k-HcSI",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
字段类型 type:double / long / integer / text / keyword / date / binary
注意:text和keyword都是字符串类型,但是只有text类型的数据才能分词,字段的配置一旦确定就不能更改 映射的配置项有很多,我们可以根据需要只配置用得上的属性.
创建成果,返回值中除了执行结果(result)之外,还有当前映射的详细信息。
语法:GET /索引名/_mapping
# 发送请求
GET /test_index/_mapping
#返回值
{
"test_index" : {
"mappings" : {
"student" : {
"properties" : {
"mappings" : {
"properties" : {
"info" : {
"properties" : {
"properties" : {
"properties" : {
"clazz" : {
"properties" : {
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"id" : {
"properties" : {
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"name" : {
"properties" : {
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
返回值包括type和field详细的JSON信息。
语法:PUT /索引名/类型名/文档ID
# 发送请求
PUT /test_index/student/1
{
"id":1,
"name":"战三",
"clazz":12
}
# 返回值
{
"_index" : "test_index",
"_type" : "student",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
注意:
语法:1. 查询所有(基本查询语句): GET /索引名/类型名/_search
# 发送请求
GET /test_index/student/_search
# 返回值
{
"took" : 151,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_index",
"_type" : "student",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "战三",
"clazz" : 12
}
},
{
"_index" : "test_index",
"_type" : "student",
"_id" : "bB1InoMBWJmEB7k-HcSI",
"_score" : 1.0,
"_source" : {
"mappings" : {
"info" : {
"properties" : {
"name" : {
"type" : "text"
},
"clazz" : {
"type" : "string"
},
"id" : {
"type" : "double"
}
}
}
}
}
}
]
}
}
查询所有结果中包含以下字段:
语法:2. 根据ID查询: GET /索引名/类型名/文档ID
# 发送请求
GET /test_index/student/1
# 返回值
{
"_index" : "test_index",
"_type" : "student",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 1,
"name" : "战三",
"clazz" : 12
}
}