Skip to the workspace
Pebellslearn by building
Introduction to Elasticsearch  ›  Lesson

JSON and REST — How You Talk to Elasticsearch

REST API Basics 10 minEvery Elasticsearch action is an HTTP request where the verb says what to do, the path says which resource, and a JSON body carries the details.

elasticsearch / JSON and REST — How You Talk to Elasticsearch

Explore each scene at your own pace.

01 · The rule

The rule

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 — GET reads, PUT creates or replaces at a path you name, POST sends something for the server to act on (often with a server-chosen id), DELETE removes.
  • The path — which resource: /books is the index, /books/_doc/1 is the document with id 1, /books/_search is 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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.