新增:
POST /test_index/_doc
{
"doc": {
"name":"aaa",
"price": 100009.00,
"desc":"very good"
}
}
修改:
POST /{indexName}/_update/{id}
{
"doc": {
"fieldName" : "fieldValue"
}
}
举例:
POST /test_index/_update/AVYrxH8BeM2aFHTN9y9g
{
"doc": {
"name" : "test_index1_shanghai"
}
}
这种格式已经不再适用,会报错:
POST /{indexName}/_doc/{id}/_update
或者
POST /indexName/{id}/_update
返回错误信息
{
"error" : "no handler found for uri [/test_index/_doc/AVYrxH8BeM2aFHTN9y9g/_update?pretty=true] and method [POST]"
}
同样,下面的命令会导致全量更新,如果操作不当,可能会丢失旧版本的数据
POST /test_index/_doc/ClaHxn8BeM2aFHTN-i_e
{
"doc": {
"name":"tea cake",
"price" :39.00,
"production date":"2022-03-26"
}
}
并发修改:
以往ES的并发修改使用的是乐观锁机制,使用的时候需要指定version,但是目前version已经不再适用。以下是指定version时ES给出的提示信息:
{
"error" : {
"root_cause" : [
{
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
}
],
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
},
"status" : 400
}
现在,在并发修改的场景下,需要使用另外两个属性。
if_seq_no和if_primary_term。这两个属性在新增和根据id查询成功后,ES会返回当前数据的seq_no和_primary_term。
如果把这两个属性放在请求体中,会提示使用方式不对
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "Field [_seq_no] is a metadata field and cannot be added inside a document. Use the index API request parameters."
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [_seq_no] of type [_seq_no] in document with id 'ClaHxn8BeM2aFHTN-i_e'. Preview of field's value: '20'",
"caused_by" : {
"type" : "mapper_parsing_exception",
"reason" : "Field [_seq_no] is a metadata field and cannot be added inside a document. Use the index API request parameters."
}
},
"status" : 400
}
正确的方式:
POST /{indexName}/_doc/{id}?if_seq_no={预期的seq_no}&if_primary_term={预期的primary_term}
{
"doc": {
"name":"tea cake",
"price" :138.00
}
}
例子:
POST /test_index/_doc/ClaHxn8BeM2aFHTN-i_e?if_seq_no=20&if_primary_term=1
{
"doc": {
"name":"tea cake",
"price" :138.00
}
}
查询:
条件查询:
GET /test_index/_search
{
"query": {
"match" : {
"_id" : "AVYrxH8BeM2aFHTN9y9g"
}
}
}
查询索引下所有的数据,和以前版本保持一致
GET /{indexName}/_search
{
"query": {
"match_all": {}
}
}
举例:
GET /test_index/_search
{
"query": {
"match_all": {}
}
}
根据_id查询
GET /test_index/_doc/B1ZaxX8BeM2aFHTNly-A
批量查询
如果根据多个id查询多条数据,需要用到_mget。目前_mget必须要指定index,否则会报错:
{
"error" : {
"root_cause" : [
{
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: index is missing for doc 0;2: index is missing for doc 1;"
}
],
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: index is missing for doc 0;2: index is missing for doc 1;"
},
"status" : 400
}
正确的方式
GET /{indexName}/_mget
{
"docs":[
{
"_id":"xxx"
},
{
"_id":"xxx"
}
]
}
举例:
GET /test_index/_mget
{
"docs":[
{
"_id":"A1YsxH8BeM2aFHTNES_u"
},
{
"_id":"BFYsxH8BeM2aFHTNFy-m"
}
]
}
那不指定索引,可不可以查询所有数据呢?
GET _search
{
"query": {
"match_all": {}
}
}
草稿,内容正在逐渐完善。。
转载标题:ElasticSearch8.1入门,增删改查 转载地址:https://www.123yun.com/article/1415.html
剑三连接服务器失败 彩虹六号服务器连接错误 局域网文件共享服务器 服务器超出最大连接数