Elasticsearch Document

Elasticsearch Document

Last Update: March 2020

Before you start reading this page, try the new Elasticsearch Check-Up - Get actionable recommendations that can improve your cluster search and indexing speed (no installation required).

Document in Elasticsearch

Overview

A document is simply a json document that is stored in Elasticsearch index. It consists of one or more fields; where each field has its own data type. This field type defines the type of data that can be stored in the field such as integer, string, object. Document is schema-free, which means we do not require to specify schema before indexing document, when a field is indexed for the first time, its type is decided and set.

Examples:

Creating A document : to create a document in the users index.

POST  /users/_doc 
{
    "name" : "Petey",
    "lastname" : "Cruiser",
    "email" : "petey@gmail.com"
}

In the above request, we haven’t mentioned id for the document so index operation generates a unique ID for the document. Here _doc is the type of document. We can provide this type to user-defined type also where user index may store user type document.

POST  /users/_doc/1
{
    "name" : "Petey",
    "lastname" : "Cruiser",
    "email" : "petey@gmail.com"
}

In the above query, the document will be created with id 1.

You can use the below ‘GET’ query to get a document from the index using id

GET  /users/_doc/1

Bellow is the result containing the document (in _source field) with metadata:-

{
    "_index": "users",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,    "_seq_no": 1,    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "Petey",
        "lastname": "Cruiser",
        "email": "petey@gmail.com"
    }
}
Notes

Starting version 7.0 types are deprecated, so for backward compatibility on version 7.x all docs are under type ‘_doc’, starting 8.x type will be completely removed from ES APIs


About Opster

Opster is redefining Elasticsearch management - pro-actively troubleshooting, optimizing performance, operating on clusters and assisting with all things needed to successfully run ES in production


Click below to learn how to fix common problems related to these concepts
« Back to Index