Getting Started With Elasticsearch REST APIs
Posted By : Jatin Gupta | 28-Apr-2018
One of the many best things about Elasticsearch is it's REST API that allows to create, maintain and query the index data in multiple ways.
In this blog I will try to provide a summary of the some important API calls that you should get familiar with as you get started with Elasticsearch, and will add some examples with respective cURL commands.
The API examples detailed below are Document API, Search API, Indices API and cat API.
Document API
This API is used for handling documents in Elasticsearch.
Using these APIs, you can create documents in an index, update them, move them to different index, or even remove them.
Example :
1. Add a document : Add a document to an existing or a new index.
jatingupta@jatingupta:~$ curl -XPUT 'localhost:9200/movies/nolan/1?pretty' -H 'Content-Type: application/json' -d'{ "Name" : "Memento" }' jatingupta@jatingupta:~$ curl -XPUT 'localhost:9200/movies/nolan/2?pretty' -H 'Content-Type: application/json' -d'{ "Name" : "Inception" }'
2. Delete a document : Delete a document from an existing index.
jatingupta@jatingupta:~$ curl -XDELETE 'localhost:9200/movies/nolan/1?pretty'
3. Reindex : Change the index.
jatingupta@jatingupta:~$ curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'{ "source" :{ "index": "old_index" },"dest": { "index": "new_index" } } '
Search API
1. Search : Search for a document in an existing index.
jatingupta@jatingupta:~$ curl -XGET "localhost:9200/movies/nolan/_search?q=Name:Memento&pretty"
2. Count : Count the number of matches for the query.
jatingupta@jatingupta:~$ curl -XGET "localhost:9200/movies/nolan/_count?q=Name:Memento&pretty"
3. Validate : Validate a query without executing it.
jatingupta@jatingupta:~$ curl -XGET "localhost:9200/movies/nolan/_validate?q=Name:Memento&pretty"
Indices API
1. Create : Create a new index
jatingupta@jatingupta:~$ curl -XPUT "localhost:9200/books?pretty -H'Content-Type:application/json'"
2. Delete : Delete an index
jatingupta@jatingupta:~$ curl -XDELETE "localhost:9200/books?pretty"
3. Map : Add a new type to existing mapping
jatingupta@jatingupta:~$ curl -XPUT 'localhost:9200/employee/_mapping/user?pretty' -H 'Content-Type: application/json' -d' { "properties": { "name": { "type": "text" } } } '
CAT API
CAT api returns data in a more user friendly format as compared to the typical JSON response.
This api is very useful to get stats about the indices, space occupied and documents stored
1. Cat indices : Provides info and metrics regarding our indices.
jatingupta@jatingupta:~$ curl -XGET "localhost:9200/_cat/indices?v"
2. Cat health : Provides overview of indices health
jatingupta@jatingupta:~$ curl -XGET "localhost:9200/_cat/health?v&pretty"
3. Cat nodes : Provides info about your elasticsearch nodes
jatingupta@jatingupta:~$ curl -XGET "localhost:9200/_cat/nodes?v&pretty"
About Author
Jatin Gupta
Jatin is a DevOps trainee. He ha deep interest in python and cloud technologies. He likes to read about science/history and fiction, listening to music and explore new places.