Elasticsearch 7.0 Cookbook(Fourth Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

To index a document, several REST entry points can be used:

 

To index a document, we need to perform the following steps:

  1. If we consider the order type of the previous chapter, the call to index a document will be as follows:
POST /myindex/_doc/2qLrAfPVQvCRMe7Ku8r0Tw
{
"id": "1234",
"date": "2013-06-07T12:14:54",
"customer_id": "customer1",
"sent": true,
"in_stock_items": 0,
"items": [
{
"name": "item1",
"quantity": 3,
"vat": 20
},
{
"name": "item2",
"quantity": 2,
"vat": 20
},
{
"name": "item3",
"quantity": 1,
"vat": 10
}
]
}
  1. If the index operation was successful, the result returned by Elasticsearch should be as follows:
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "2qLrAfPVQvCRMe7Ku8r0Tw",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}

Some additional information is returned from the index operation, such as the following:

  • An auto-generated ID if it's not specified (in this example: 2qLrAfPVQvCRMe7Ku8r0Tw)
  • The version of the indexed document as per the optimistic concurrency control (the version is 1 because it was the document's first time of saving or updating)
  • Whether the record has been created ("result": "create" in this example)