JSON and REST — How You Talk to Elasticsearch
Elasticsearch has no special client language you must learn first. Every action — storing a book, searching for it, deleting a whole index — is an ordinary HTTP request carrying a JSON body. Once you can read one request, you can read them all.
The rule
Verb says what, path says where, JSON body says the details.
Throughout this course we run a small online bookshop. Its books live in an index called books, and one book looks like this:
{
"title": "The Quiet Garden",
"author": "Mira Das",
"genre": "fiction",
"price": 12.5,
"pages": 320,
"in_stock": true,
"published": "2019-04-02"
}That is JSON: curly braces hold an object of "key": value pairs. Values can be strings (in double quotes), numbers, true/false, null, arrays in [ ], or nested objects. Keys are always double-quoted strings, and there is no trailing comma after the last pair.
A request to Elasticsearch has three parts:
- The verb —
GETreads,PUTcreates or replaces at a path you name,POSTsends something for the server to act on (often with a server-chosen id),DELETEremoves. - The path — which resource:
/booksis the index,/books/_doc/1is the document with id 1,/books/_searchis the search endpoint of that index. Paths starting with_are Elasticsearch's own endpoints. - The body — JSON with the details, such as the document or the query.
GET /books/_search
{
"query": { "match": { "title": "garden" } }
}The reply is JSON too: a status code (200 OK, 201 Created, 404 Not Found, 400 Bad Request) plus a body you can read.