Skip to content

Instantly share code, notes, and snippets.

@roalcantara
Created January 29, 2021 13:16
Show Gist options
  • Save roalcantara/4e63d4ec259b155083f418f1a1ecf2ed to your computer and use it in GitHub Desktop.
Save roalcantara/4e63d4ec259b155083f418f1a1ecf2ed to your computer and use it in GitHub Desktop.
Elasticsearch CRUD Quickstart

# # ELASTICSEARCH CRUD QUICK START #

# ELASTICSEARCH VERSION (7.10.2) # DOCKER IMAGE: https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html # VSCODE REST EXTENSION: https://marketplace.visualstudio.com/items?itemName=crossjs.restio # API USED: https://pokeapi.co/

# # REST EXTENSION VARIABLES #

@hostname = localhost @port = 9200 @host = {{hostname}}:{{port}} @index = pokemons @id = 25 @contentType = application/json

# # CRUD QUICK START: INTRO #

### CHECK all indices
# To see all running indices # # curl --request GET # --url http://localhost:9200/_cat/indices # # As there is no indices, it should: # ✔ Return 200 OK # ✔ Return content-length: 0

GET http://{{host}}/_cat/indices

### GET all running nodes
# To see all running nodes # # curl --request GET # --url 'http://localhost:9200/_cat/nodes?v=true&pretty=' # # As there is just one running nodes, it should: # ✔ Return 200 OK # ✔ Return a response similar to # ` # ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name # 172.17.0.2 59 96 0 0.10 0.04 0.01 cdhilmrstw * 5160ddd7a72b # `

GET http://{{host}}/_cat/nodes?v=true&pretty

### GET an index's mapping
# To check index's mapping # # curl --request GET # --url http://localhost:9200/pokemons/_doc/_search # # As there are no indices, it should: # ✔ Return 404 Not Found # ✔ Return the error index_not_found_exception # { ... # "error": { # "type": "index_not_found_exception", # "index": "pokemons" # }, # "status": 404 # }

GET http://{{host}}/{{index}}/_mapping

### [R]EAD all documents
# To search all documents # # curl --request GET # --url http://localhost:9200/pokemons/_doc/_search # # As there are no indices, it should: # ✔ Return 404 Not Found # ✔ Return the error index_not_found_exception # { ... # "error": { # "type": "index_not_found_exception", # "index": "pokemons" # }, # "status": 404 # }

GET http://{{host}}/{{index}}/_doc/_search

### [C]REATE a document without ID
# Now, let's create a document without ID # # curl --request POST # --url http://localhost:9200/pokemons/_doc # --header 'content-type: application/json' # --data '{"code": 15,"name": "beedrill","weight": 295,"height": 10}' # # It should: # ✔ Create a index pokemons automatically # ✔ Autogenerate the document ID # ✔ Return 201 Created # ✔ Return the document creation response, similar to # { ... # "result": "created", # "_index": "pokemons", # "_version": 1, # "_id": "xB-NTXcBHav7iUM1gESC" # }

POST http://{{host}}/{{index}}/_doc HTTP/1.1 content-type: application/json

{
"code": 15, "name": "beedrill", "weight": 295, "height": 10

}

### [R]EAD all documents
# Next, let's see if the created document is listed # # curl --request GET # --url http://localhost:9200/pokemons/_doc/_search # # It should: # ✔ Return 200 OK # ✔ Return a list containing the created document similar to # `{ ... # "hits": { # "total": { "value": 1, "relation": "eq" }, # "hits": [ # { ... # "_index": "pokemons", # "_id": "xR-UTXcBHav7iUM1f0S-", # "_source": { "code": 15, "name": "beedrill", "weight": 295, "height": 10 } # } # ] # } # }

GET http://{{host}}/{{index}}/_doc/_search

### CHECK all INDICES
# This time, the created index should be found # # curl --request GET # --url http://localhost:9200/_cat/indices # # It should: # ✔ Return 200 OK # ✔ Contain the created index, similar to # ` # yellow open pokemons 2v8EYLEBQm2oGsCv8fr7Iw 1 1 1 0 4.6kb 4.6kb # `

GET http://{{host}}/_cat/indices

### CHECK the created index's mapping
# As the index has been created by the document creation # # curl --request GET # --url http://localhost:9200/pokemons/_mapping # # The mapping has been auto generated created and it should: # ✔ Return 200 OK # ✔ Return the index's auto generated mapping # { # "pokemons": { # "mappings": { # "properties": { # "code": { "type": "long" }, # "height": { "type": "long" }, # "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, # "weight": { "type": "long" } # } # } # } # }

GET http://{{host}}/{{index}}/_mapping

### [C]REATE a document with ID
# To create a document providing an ID # # curl --request POST # --url http://localhost:9200/pokemons/_create/25 # --header 'content-type: application/json' # --data '{"code": 25,"name": "pikachu","weight": 60,"height": 4}' # # It should: # ✔ Create a document with the given id # ✔ Return 201 Created # ✔ Return the document created response # { ... # "result": "created" # "_index": "pokemons", # "_version": 1, # "_id": "25", # ... # }

POST http://{{host}}/{{index}}/_create/{{id}} HTTP/1.1 content-type: application/json

{
"code": 25, "name": "pikachu", "weight": 60, "height": 4

}

### [R]EAD all documents
# Next, let's see if the created document is listed # # curl --request GET # --url http://localhost:9200/pokemons/_doc/_search # # It should: # ✔ Return 200 OK # ✔ Return a list containing the created document # { ... # "hits": { # "total": { "value": 2, "relation": "eq" }, # "hits": [ # { ... # "_index": "pokemons", # "_id": "xR-UTXcBHav7iUM1f0S-", # "_source": { "code": 15, "name": "beedrill", "weight": 295, "height": 10 } # }, # { ... # "_index": "pokemons", # "_id": "25", # "_source": { "code": 25, "name": "pikachu", "weight": 60, "height": 4 } # } # ] # } # }

GET http://{{host}}/{{index}}/_doc/_search

### [R]EAD document by ID
# And to get a specific document we use its ID # # curl --request GET # --url http://localhost:9200/pokemons/_doc/25 # # It should: # ✔ Return 200 OK # ✔ Return the found document # { ... # "_index": "pokemons", "_id": "25", "_version": 1, # "_source": { "code": 25, "name": "pikachu", "weight": 60, "height": 4 } # }

GET http://{{host}}/{{index}}/_doc/{{id}}

### [U]PDATE document by ID
# Let's say we need to update a given document # # curl --request POST # --url http://localhost:9200/pokemons/_doc/25/_update # --header 'content-type: application/json' # --data '{"doc" : {"weight" : 20,"views": 0}}' # # It should: # ✔ Increase the document version from '1' to '2' # ✔ Update the field weight from '60' to '20' # ✔ Append a new field: { "views": 0 } # ✔ Return 200 OK # ✔ Return the update response # { # ... # "result": "updated" # "_index": "pokemons", # "_version": 2, # "_id": "25", # ... # }

POST http://{{host}}/{{index}}/_doc/{{id}}/_update HTTP/1.1 content-type: application/json

{
"doc" : {
"weight" : 20, "views": 0

}

}

### [R]EAD updated document by ID
# And to check if the updated has been succedded # # curl --request GET # --url http://localhost:9200/pokemons/_doc/25 # # It should: # ✔ Return 200 OK # ✔ Return the updated document # { ... # "_index": "pokemons", "_id": "25", "_version": 2, # "_source": { "code": 25, "name": "pikachu", "weight": 20, "height": 4, "views": 0 } # }

GET http://{{host}}/{{index}}/_doc/{{id}}

### CHECK updated index's mapping
# As the index has been updated # # curl --request GET # --url http://localhost:9200/pokemons/_mapping # # It should: # ✔ Return 200 OK # ✔ Return the index's updated mapping # { # "pokemons": { # "mappings": { # "properties": { # "code": { "type": "long" }, # "height": { "type": "long" }, # "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, # "views": { "type": "long" }, # "weight": { "type": "long" } # } # } # } # }

GET http://{{host}}/{{index}}/_mapping

### [D]ELETE document by ID
# Finally, to delete a document by ID # # curl --request DELETE # --url http://localhost:9200/pokemons/_doc/25 # # It should: # ✔ Return 200 OK # ✔ Return the delete document response # { ... # "result": "deleted", # "_index": "pokemons", # "_version": 3, # "_id": "25" # }

DELETE http://{{host}}/{{index}}/_doc/{{id}}

### [R]EAD to confirm the document deletion
# To check if the document has been deleted # # curl --request GET # --url http://localhost:9200/pokemons/_doc/_search # # It should: # ✔ Return 200 OK # ✔ Not contain the deleted document

GET http://{{host}}/{{index}}/_doc/_search

### [R]EAD deleted document by ID
# And, if we try to get a deleted document by ID # # curl --request GET # --url http://localhost:9200/pokemons/_doc/25 # # It should: # ✔ Return 404 Not Found # ✔ Returns the response # { # "found": false, # "_index": "pokemons", # "_type": "_doc", # "_id": "25" # }

GET http://{{host}}/{{index}}/_doc/{{id}}

### [D]ELETE index
# To delete the entire index # # curl --request DELETE # --url http://localhost:9200/pokemons # # It should: # ✔ Return 200 OK # ✔ Return the updated document: # { "acknowledged": true }

DELETE http://{{host}}/{{index}}

### CHECK the mapping of a deleted index
# When trying to get the deleted index mapping # # curl --request GET # --url http://localhost:9200/pokemons/_mapping # # It should # ✔ Return 404 Not Found # ✔ Return the error # { ... # "error": { # "type": "index_not_found_exception", # "index": "pokemons" # }, # "status": 404 # }

GET http://{{host}}/{{index}}/_mapping

### GET all indices
# Which means that the index does not exist. So.. # # curl --request GET # --url http://localhost:9200/_cat/indices # # It should: # ✔ Return 200 OK # ✔ Not list the deleted index

GET http://{{host}}/_cat/indices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment