基本操作
索引 <index>
创立
# 1. 创立索引
- PUT / 索引名 ====> PUT /products
- 留神:
1.ES 中索引衰弱转态 red(索引不可用)、yellwo(索引可用, 存在危险)、green(衰弱)
2. 默认 ES 在创立索引时回为索引创立 1 个备份索引和一个 primary 索引
# 2. 创立索引 进行索引分片配置
- PUT /products
{
"settings": {
"number_of_shards": 1, #指定主分片的数量
"number_of_replicas": 0 #指定正本分片的数量
}
}
)
查问
# 查问索引
- GET /_cat/indices?v
删除
# 3. 删除索引
- DELETE / 索引名 =====> DELETE /products
- DELETE /* `* 代表通配符, 代表所有索引 `
映射 <mapping>
创立
字符串类型: keyword 关键字 关键词、text 一段文本
数字类型:integer long
小数类型:float double
布尔类型:boolean
日期类型:date
# 1. 创立索引 & 映射
PUT /products
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"title":{"type": "keyword"},
"price":{"type": "double"},
"created_at":{"type": "date"},
"description":{"type": "text"}
}
}
}
阐明: ES 中反对字段类型十分丰盛,如:text、keyword、integer、long、ip 等。更多参见 https://www.elastic.co/guide/en/elasticsearch/reference/7.15/…
查问
# 1. 查看某个索引的映射
- GET / 索引名 /_mapping =====> GET /products/_mapping
文档 <document>
增加文档
POST /products/_doc/1 #指定文档 id
{
"title":"iphone13",
"price":8999.99,
"created_at":"2021-09-15",
"description":"iPhone 13 屏幕采纳 6.1 英寸 OLED 屏幕。"
}
POST /products/_doc/ #主动生成文档 id
{
"title":"iphone14",
"price":8999.99,
"created_at":"2021-09-15",
"description":"iPhone 13 屏幕采纳 6.8 英寸 OLED 屏幕"
}
{
"_index" : "products",
"_type" : "_doc",
"_id" : "sjfYnXwBVVbJgt24PlVU",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
查问文档
GET /products/_doc/1
{
"_index" : "products",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "iphone13",
"price" : 8999.99,
"created_at" : "2021-09-15",
"description" : "iPhone 13 屏幕采纳 6.1 英寸 OLED 屏幕"
}
}
删除文档
DELETE /products/_doc/1
{
"_index" : "products",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
更新文档
PUT /products/_doc/sjfYnXwBVVbJgt24PlVU
{"title":"iphon15"}
阐明: 这种更新形式是先删除原始文档, 在将更新文档以新的内容插入。
POST /products/_doc/sjfYnXwBVVbJgt24PlVU/_update
{
"doc" : {"title" : "iphon15"}
}
阐明: 这种形式能够将数据原始内容保留, 并在此基础上更新。
批量操作
POST /products/_doc/_bulk #批量索引两条文档
{"index":{"_id":"1"}}
{"title":"iphone14","price":8999.99,"created_at":"2021-09-15","description":"iPhone 13 屏幕采纳 6.8 英寸 OLED 屏幕"}
{"index":{"_id":"2"}}
{"title":"iphone15","price":8999.99,"created_at":"2021-09-15","description":"iPhone 15 屏幕采纳 10.8 英寸 OLED 屏幕"}
POST /products/_doc/_bulk #更新文档同时删除文档
{"update":{"_id":"1"}}
{"doc":{"title":"iphone17"}}
{"delete":{"_id":2}}
{"index":{}}
{"title":"iphone19","price":8999.99,"created_at":"2021-09-15","description":"iPhone 19 屏幕采纳 61.8 英寸 OLED 屏幕"}
阐明: 批量时不会因为一个失败而全副失败, 而是继续执行后续操作, 在返回时依照执行的状态返回!
本文由 mdnice 多平台公布