Author: Ju4t
PUT http://localhost:9200/customer/external/1
{
"name": "John Doe"
}
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
GET http://localhost:9200/customer/external/1
{
"_index": "customer", // 在哪个索引
"_type": "external", // 在哪个类型
"_id": "1", // 记录的ID
"_version": 1, // 版本号
"_seq_no": 0, // 并发控制字段,每次更新就会+1,用来做乐观锁
"_primary_term": 1, // 同上,主分片重新分配,如重启,就会变化
"found": true,
"_source": { // 真正内容
"name": "John Doe"
}
}
PUT http://localhost:9200/customer/external/1
{
"name": "John Doe"
}
通过if_seq_no来校验数据是否是最新的
乐观锁修改
POST http://localhost:9200/customer/external/1?if_seq_no=0&if_primary_term=1
{
"name": "John Doe"
}
{
"_index": "customer", // 在哪个索引
"_type": "external", // 在哪个类型
"_id": "1", // 记录的ID
"_version": 7, // 版本号
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
提交的if_seq_no和查到if_seq_no不一样,则"status": 409
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [0], primary term [1]. current document has seqNo [1] and primary term [1]",
"index_uuid": "7wRZdFaVSuyNDalDvf-0aQ",
"shard": "0",
"index": "customer"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [0], primary term [1]. current document has seqNo [1] and primary term [1]",
"index_uuid": "7wRZdFaVSuyNDalDvf-0aQ",
"shard": "0",
"index": "customer"
},
"status": 409
}
DELETE http://localhost:9200/customer/external/1
DELETE http://localhost:9200/customer/
bulk批量API,不能用postman测了,上Kinaba
POST /customer/external/_bulk
{"index": {"_id":"1"}}
{"name": "John Doe"}
{"index": {"_id":"2"}}
{"name": "John Doe"}
{
"took" : 13,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 200
}
},
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1,
"status" : 201
}
}
]
}
# https://www.elastic.co/guide/en/elasticsearch/reference/7.16/docs-bulk.html
POST _bulk
{ "index" : { "_index" : "website", "_type": "blog", "_id" : "1" } }
{ "title" : "My first blog post" }
{ "delete" : { "_index" : "website", "_type": "blog", "_id" : "2" } }
{ "create" : { "_index" : "website", "_type": "blog", "_id" : "3" } }
{ "title" : "My second blog post!" }
{ "update" : {"_id" : "1", "_index" : "website", "_type": "blog" } }
{ "doc" : {"title" : "My updated blog post!"} }
POST /bank/_bulk
测试数据见:https://gitee.com/xlh_blog/common_content/blob/master/es%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE.json
# 查询方式一:
GET bank/_search?q=*&sort=account_number:asc
# DSL方式查询方式(常用):
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{"account_number": "asc"},
{"balance": "desc"}
],
"from": 10,
"size": 10
}
# 排序
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
]
}
# 设置返回字段
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{"balance": "desc"}
],
"from": 0,
"size": 20,
"_source": ["firstname","balance"]
}
# 查询指定id内容
GET /bank/_search
{
"query": {
"match": {
"account_number": "20"
}
}
}
# 全文检索(分词),按照评分进行排序 "_score" : 5.990829,
GET /bank/_search
{
"query": {
"match": {
"address": "Kings"
}
}
}
# 短语匹配(不分词)
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "mill road"
}
}
}
# 多字段匹配(分词)
GET /bank/_search
{
"query": {
"multi_match": {
"query": "mill",
"fields": ["address", "city"]
}
}
}
# 复合查询
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "F"
}
}
],
"must": [
{}
]
}
}
}