I am currently working through an Elasticsearch course on Udemy and will be adding command examples here to refer to.
Initially for myself, but if it helps others then that’s good too.
Indices
Indexes are the equivalent to tables in a MySQL database.
Create an index
An example for creating a “products” index.
PUT /products
Delete an index
An example for deleting the “products” index
DELETE /products
Documents
A document is the equivalent of a row in a MySQL database table.
Index a document
An example for indexing a “product” document.
POST /products/_doc
{
"name" : "Horizon Zero Dawn",
"price": 35,
"in_stock": true
}
Index a document with a specific ID / Replacing the document with the specific ID
An example of indexing a “product” document but with a specific ID (of 9999 in this example). Instead of letting Elasticsearch create the ID for us.
The same query is used to completely replace the document, which is what happens anyway behind the scenes as Elasticsearch documents are immutable.
PUT /products/_doc/9999
{
"name" : "Days Gone",
"price": 32,
"in_stock": true
}
Get document by ID
Retrieving a “product” document by a given ID. (ID of 9999 in this example).
GET /products/_doc/9999
Updating a document
POST /products/_update/9999
{
"doc": {
"price": 15
}
}
Upserts
An example of an upsert. That is, insert the document if not already present or updating it if it is.
If the document exists then the script is ran.
If the document does not already exist, then the key/value pairs in the “upsert” object are used to create the new document.
POST /products/_update/101
{
"script": {
"source": "ctx._source.in_stock = false"
},
"upsert": {
"name": "Death Stranding",
"price": 45,
"in_stock": true
}
}
Delete a document
An example of deleting a document by its given id — 9999 in this example.
DELETE /products/_doc/9999