Document the CRUD
Elasticsearch CRUD documents through REST apisCopy the code
operation | API sample | note |
---|---|---|
Index | PUT my_index _doc / {1 “user” : “mike”, “comment” : “comment”} | The Type name is _doc by convention Index: If the ID does not exist, create a new document. Otherwise, delete the existing document first and then create a new document. The version will increase |
Create | PUT my_index _create / {1 “user” : “mike”, “comment” : “comment”} PUT my_index / _doc (do not specify a ID, automatic generation of) {” user “:” mike “, “comment” : “comment”} |
Create If the ID already exists, it will fail |
Read | GET my_indx/_doc/1 | Get the data for the corresponding ID document |
Update | POST my_index _update / {1 “user” : “mike”, “comment” : “comment”} | Update: The document must already exist, and only incremental changes are made to the corresponding field |
Delete | DELETE my_index/_doc/1 | Delete the corresponding ID document |
Test the API
There is a development tool in the Kibana sidebar that allows you to test the Elasticsearch API. We imported a movie index data in the previous section, so let's use it as an example to test the related APICopy the code
The Index operation
PUT /movies/_doc/1
{
"genre" : [
"Adventure"."Animation"."Children"."Comedy"."Fantasy"]."year" : 1995."id" : "1"."@version" : "1"."title" : "Toy Story"} The response results are as follows: {"_index" : "movies"."_type" : "_doc"."_id" : "1"."_version" : 2."result" : "updated"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 9743."_primary_term" : 1
}
Copy the code
The Create operation
PUT /movies/_create/1# create document, error if document exists {"genre" : [
"Adventure"."Animation"."Children"."Comedy"."Fantasy"]."year" : 1995."id" : "1"."@version" : "1"."title" : "Toy Story"} The response results are as follows: {"error" : {
"root_cause": [{"type" : "version_conflict_engine_exception"."reason" : "[1]: version conflict, document already exists (current version [2])"."index_uuid" : "Km35tJDlTPGjIjnmNgPQ4A"."shard" : "0"."index" : "movies"}]."type" : "version_conflict_engine_exception"."reason" : "[1]: version conflict, document already exists (current version [2])"."index_uuid" : "Km35tJDlTPGjIjnmNgPQ4A"."shard" : "0"."index" : "movies"
},
"status" : 409
}
Copy the code
Read operation
GET movies/_doc/1Select * from movies where id = 01The document data response results are as follows: {"_index" : "movies"."_type" : "_doc"."_id" : "1"."_version" : 1."_seq_no" : 128."_primary_term" : 1."found" : true."_source" : {
"genre" : [
"Adventure"."Animation"."Children"."Comedy"."Fantasy"]."year" : 1995."id" : "1"."@version" : "1"."title" : "Toy Story"}}Copy the code
The Update operation
The # update operation can modify the values of old fields or add new fields, but overall requests need to POST movies/_update/ wrapped under doc1
{
"doc": {
"genre" : [
"Adventure1"."Animation1"."Children1"."Comedy1"."Fantasy1"]."year" : 1995."id" : "1"."@version" : "2"."title" : "Toy Story"}} The response results are as follows: {"_index" : "movies"."_type" : "_doc"."_id" : "1"."_version" : 3."result" : "updated"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 9744."_primary_term" : 1
}
Copy the code
The Delete operation
DELETE /movies/_doc/1The response results are as follows: {"_index" : "movies"."_type" : "_doc"."_id" : "1"."_version" : 4."result" : "deleted"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 9745."_primary_term" : 1
}
Copy the code
Bulk API
The Rest API needs to re-establish connections each time a request is generated, which can consume high performance. The Bulk API supports four types of operations: 1. Index 2. Create 3. Update 4. Delete
The Bulk API can know the Index in the URL or specify it in the request body. The failure of a single operation does not affect other operation results. 3. The returned result contains the result of each operationCopy the code
The sample
POST _bulk # Create an index named test with the document ID as1, the data is {"field1":"value1"} index {"index": {"_index":"test"."_id":"1"}}
{"field1":"value1"} # delete from test where id = 12File (does not exist at this time, will report an error, but does not affect the success of other execution) {"delete": {"_index":"test"."_id":2}} # create index test23, the data is {"field1":"value3"} index {"create": {"_index":"test2"."_id":"3"}}
{"field1":"value3"Alter table test2 alter table test2 alter table test2"update": {"_index":"test"."_id":"1"}}
{"doc": {"field1":"value2"}} The response result is as follows: For each operation, there is a response result {"took" : 522."errors" : false."items": [{"index" : {
"_index" : "test"."_type" : "_doc"."_id" : "1"."_version" : 3."result" : "updated"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 2."_primary_term" : 1."status" : 200}}, {"delete" : {
"_index" : "test"."_type" : "_doc"."_id" : "2"."_version" : 1."result" : "not_found"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 3."_primary_term" : 1."status" : 404}}, {"create" : {
"_index" : "test2"."_type" : "_doc"."_id" : "3"."_version" : 1."result" : "created"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 0."_primary_term" : 1."status" : 201}}, {"update" : {
"_index" : "test"."_type" : "_doc"."_id" : "1"."_version" : 4."result" : "updated"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 4."_primary_term" : 1."status" : 200}}}]Copy the code
Batch read MGET
# 1.Mysql > alter table test2; mysql > alter table test2; mysql > alter table test21Document GET _mget {"docs":[
{"_index":"test"."_id":"1"
},
{
"_index":"test2"."_id":"1"}]} the response is as follows: {"docs": [{"_index" : "test"."_type" : "_doc"."_id" : "1"."_version" : 4."_seq_no" : 4."_primary_term" : 1."found" : true."_source" : {
"field1" : "value2"}}, {"_index" : "test2"."_type" : "_doc"."_id" : "1"."found" : false}}]Copy the code
Search API
1.Queries are a big piece of content, which I'll cover in detail in the next section due to space constraintsCopy the code
Common error return
The problem | why |
---|---|
Unable to connect | The network is faulty or the cluster is down |
Connection cannot be closed | The network is faulty or the node is faulty |
429 | The Cluster is too Busy |
4xx | Request size error |
500 | Cluster internal error |
More content welcome to pay attention to my personal public number “Han Elder brother has words”, 100G artificial intelligence learning materials, a large number of back-end learning materials waiting for you to take. |