Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Read any request right to left in your head: first the body (what details?), then the path (on what?), then the verb (doing what?). Say it as one sentence.

Reading DELETE /books/_doc/4Step 1 of 4
  1. Body

    There is none. Nothing extra is needed to remove something.

The common trap is broken JSON: a missing quote around a key, a single-quoted string, or a trailing comma. Elasticsearch answers with a 400 and a parse error, and the fix is almost always in the punctuation.

Drills

Answer fast — trust the rule.

quick drill

Which of these is valid JSON?

quick drill

In GET /books/_doc/3, what does /books/_doc/3 tell Elasticsearch?

quick drill

Which verb would you use to read a document without changing anything?

quick drill

Elasticsearch answers a request with status 400 and a JSON parse error. What is the most likely cause?

Remember

  • Verb says what, path says where, JSON body says the details.
  • Read a request right to left and say it as one sentence.
  • Double quotes on every key and string, no trailing commas.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRead a request aloud

Take the request POST /books/_update/2 with the body {"doc": {"in_stock": true}}. Explain in plain words what each part — verb, path and body — tells Elasticsearch, and say what the request does as one sentence.