Get APIedit

Retrieves the specified JSON document from an index.

Requestedit

GET <index>/_doc/<_id>

HEAD <index>/_doc/<_id>

GET <index>/_source/<_id>

HEAD <index>/_source/<_id>

Descriptionedit

You use GET to retrieve a document and its source or stored fields from a particular index. Use HEAD to verify that a document exists. You can use the _source resource retrieve just the document source or verify that it exists.

Realtimeedit

By default, the get API is realtime, and is not affected by the refresh rate of the index (when data will become visible for search). If a document has been updated but is not yet refreshed, the get API will issue a refresh call in-place to make the document visible. This will also make other documents changed since the last refresh visible. In order to disable realtime GET, one can set the realtime parameter to false.

Source filteringedit

By default, the get operation returns the contents of the _source field unless you have used the stored_fields parameter or if the _source field is disabled. You can turn off _source retrieval by using the _source parameter:

$params = [
    'index'   => 'twitter',
    'id'      => '0',
    '_source' => false,
];
$response = $client->get($params);
var getResponse = client.Get<Tweet>(0, g => g
    .Index("twitter")
    .SourceEnabled(false)
);
response = es.get(
    index='twitter',
    id=0,
    _source=False,
)
response = client.get(index: 'twitter', id: 0, _source: false)
GET twitter/_doc/0?_source=false

If you only need one or two fields from the _source, use the _source_includes or _source_excludes parameters to include or filter out particular fields. This can be especially helpful with large documents where partial retrieval can save on network overhead. Both parameters take a comma separated list of fields or wildcard expressions. Example:

$params = [
    'index'            => 'twitter',
    'id'               => '0',
    '_source_includes' => '*.id',
    '_source_excludes' => 'entities',
];
$response = $client->get($params);
var getResponse = client.Get<Tweet>(0, g => g
    .Index("twitter")
    .SourceIncludes("*.id")
    .SourceExcludes("entities")
);
response = es.get(
    index='twitter',
    id=0,
    _source_includes='*.id',
    _source_excludes='entities',
)
response = client.get(index: 'twitter', id: 0, _source_includes: '*.id', _source_excludes: 'entities')
GET twitter/_doc/0?_source_includes=*.id&_source_excludes=entities

If you only want to specify includes, you can use a shorter notation:

$params = [
    'index'   => 'twitter',
    'id'      => '0',
    '_source' => '*.id,retweeted',
];
$response = $client->get($params);
var getResponse = client.Get<Tweet>(0, g => g
    .Index("twitter")
    .SourceIncludes("*.id,retweeted")
);
response = es.get(
    index='twitter',
    id=0,
    _source='*.id,retweeted',
)
response = client.get(index: 'twitter', id: 0, _source_includes: '*.id,retweeted')
GET twitter/_doc/0?_source=*.id,retweeted
Routingedit

If routing is used during indexing, the routing value also needs to be specified to retrieve a document. For example:

$params = [
    'index'   => 'twitter',
    'id'      => '1',
    'routing' => 'user1',
];
$response = $client->get($params);
var getResponse = client.Get<Tweet>(2, g => g
    .Index("twitter")
    .Routing("user1")
);
response = es.get(
    index='twitter',
    id=1,
    routing='user1',
)
response = client.get(index: 'twitter', id: 1, routing: 'user1')
GET twitter/_doc/2?routing=user1

This request gets the tweet with id 2, but it is routed based on the user. The document is not fetched if the correct routing is not specified.

Preferenceedit

Controls a preference of which shard replicas to execute the get request on. By default, the operation is randomized between the shard replicas.

The preference can be set to:

_local
The operation will prefer to be executed on a local allocated shard if possible.
Custom (string) value
A custom value will be used to guarantee that the same shards will be used for the same custom value. This can help with "jumping values" when hitting different shards in different refresh states. A sample value can be something like the web session id, or the user name.
Refreshedit

The refresh parameter can be set to true in order to refresh the relevant shard before the get operation and make it searchable. Setting it to true should be done after careful thought and verification that this does not cause a heavy load on the system (and slows down indexing).

Distributededit

The get operation gets hashed into a specific shard id. It then gets redirected to one of the replicas within that shard id and returns the result. The replicas are the primary shard and its replicas within that shard id group. This means that the more replicas we have, the better GET scaling we will have.

Versioning supportedit

You can use the version parameter to retrieve the document only if its current version is equal to the specified one. This behavior is the same for all version types with the exception of version type FORCE which always retrieves the document. Note that FORCE version type is deprecated.

Internally, Elasticsearch has marked the old document as deleted and added an entirely new document. The old version of the document doesn’t disappear immediately, although you won’t be able to access it. Elasticsearch cleans up deleted documents in the background as you continue to index more data.

Path parametersedit

<index>
(Required, string) Name of the index that contains the document.
<_id>
(Required, string) Unique identifier of the document.

Query parametersedit

preference
(Optional, string) Specify the node or shard the operation should be performed on (default: random).
realtime
(Optional, boolean) Set to false to disable real time GET (default: true). See Realtime.
refresh
(Optional, enum) If true, Elasticsearch refreshes the affected shards to make this operation visible to search, if wait_for then wait for a refresh to make this operation visible to search, if false do nothing with refreshes. Valid values: true, false, wait_for. Default: false.
routing
(Optional, string) Target the specified primary shard.
stored_fields
(Optional, boolean) Set to true to retrieve the document fields stored in the index rather than the document _source (default: false).
_source
(Optional, list) Set to false to disable source retrieval (default: true). You can also specify a comma-separated list of the fields you want to retrieve.
_source_excludes
(Optional, list) Specify the source fields you want to exclude.
_source_includes
(Optional, list) Specify the source fields you want to retrieve.
version
(Optional, integer) Explicit version number for concurrency control. The specified version must match the current version of the document for the request to succeed.
version_type
(Optional, enum) Specific version type: internal, external, external_gte, force.

Response bodyedit

_index
The name of the index the document belongs to.
_type
The document type. Elasticsearch indices now support a single document type, _doc.
_id
The unique identifier for the document.
_version
The document version. Incremented each time the document is updated.
_seq_no
The sequence number assigned to the document for the indexing operation. Sequence numbers are used to ensure an older version of a document doesn’t overwrite a newer version. See Optimistic concurrency control.
_primary_term
The primary term assigned to the document for the indexing operation. See Optimistic concurrency control.
found
Indicates whether the document exists: true or false.
_routing
The explicit routing, if set.
_source
If found is true, contains the document data formatted in JSON. Excluded if the _source parameter is set to false or the stored_fields paramter is set to true.
_fields
If the stored_fields parameter is set to true and found is true, contains the document fields stored in the index.

Examplesedit

Retrieve the JSON document with the _id 0 from the twitter index:

$params = [
    'index' => 'twitter',
    'id'    => '0',
];
$response = $client->get($params);
var getResponse = client.Get<Tweet>(0, g => g.Index("twitter"));
response = es.get(
    index='twitter',
    id=0,
)
response = client.get(index: 'twitter', id: 0)
GET twitter/_doc/0

The API returns the following result:

{
    "_index" : "twitter",
    "_type" : "_doc",
    "_id" : "0",
    "_version" : 1,
    "_seq_no" : 10,
    "_primary_term" : 1,
    "found": true,
    "_source" : {
        "user" : "kimchy",
        "date" : "2009-11-15T14:12:12",
        "likes": 0,
        "message" : "trying out Elasticsearch"
    }
}

Check to see if a document with the _id 0 exists:

$params = [
    'index' => 'twitter',
    'id'    => '0',
];
$response = $client->exists($params);
var existsResponse = client.DocumentExists<Tweet>(0, g => g.Index("twitter"));
response = es.exists(
    index='twitter',
    id=0,
)
response = client.exists(index: 'twitter', id: 0)
HEAD twitter/_doc/0

Elasticsearch returns a status code of 200 - OK if the document exists, or 404 - Not Found if it doesn’t.

Get the source field onlyedit

Use the <index>/_source/<id> resource to get just the _source field of a document. For example:

$params = [
    'index' => 'twitter',
    'id'    => '1',
];
$response = $client->getSource($params);
var sourceResponse = client.Source<Tweet>(1, s => s.Index("twitter"));
response = es.get_source(
    index='twitter',
    id=1,
)
response = client.get_source(index: 'twitter', id: 1)
GET twitter/_source/1

You can use the source filtering parameters to control which parts of the _source are returned:

var sourceFilteringResponse = client.Source<Tweet>(1, s => s
    .Index("twitter")
    .SourceIncludes("*.id")
    .SourceExcludes("entities")
);
response = es.get_source(
    index='twitter',
    id=1,
    _source_includes='*.id',
    _source_excludes='entities',
)
response = client.get_source(index: 'twitter', id: 1, _source_includes: '*.id', _source_excludes: 'entities')
GET twitter/_source/1/?_source_includes=*.id&_source_excludes=entities

You can use HEAD with the _source endpoint to efficiently test whether or not the document _source exists. A document’s source is not available if it is disabled in the mapping.

var sourceExistsResponse = client.SourceExists<Tweet>(1, s => s.Index("twitter"));
response = client.exists_source(index: 'twitter', id: 1, type: '_doc')
HEAD twitter/_source/1
Get stored fieldsedit

Use the stored_fields parameter to specify the set of stored fields you want to retrieve. Any requested fields that are not stored are ignored. Consider for instance the following mapping:

$params = [
    'index' => 'twitter',
    'body'  => [
        'mappings' => [
            'properties' => [
                'counter' => [
                    'type'  => 'integer',
                    'store' => false,
                ],
                'tags' => [
                    'type'  => 'keyword',
                    'store' => true,
                ],
            ],
        ],
    ],
];
$response = $client->indices()->putMapping($params);
var createIndexResponse = client.Indices.Create("twitter", c => c
    .Map<Tweet>(m => m
        .Properties(p => p
            .Number(n => n
                .Name(f => f.Counter)
                .Type(NumberType.Integer)
                .Store(false)
            )
            .Keyword(k => k
                .Name(f => f.Tags)
                .Store(true)
            )
        )
    )
);
response = client.indices.create(index: 'twitter', body: {
    mappings: {properties: {
        counter: {type: 'integer',
                  store: false},
        tags: {type: 'keyword',
               store: true}
    }
    }
})
response = client.index(index: 'twitter', id: 1, body: {counter: 1, tags: ['red']})
PUT twitter
{
   "mappings": {
       "properties": {
          "counter": {
             "type": "integer",
             "store": false
          },
          "tags": {
             "type": "keyword",
             "store": true
          }
       }
   }
}

Now we can add a document:

$params = [
    'index' => 'twitter',
    'id'    => '1',
    'body'  => [
        'counter' => 1,
        'tags'    => ['red'],
    ],
];
$response = $client->index($params);
var indexResponse = client.Index(new Tweet
{
    Counter = 1,
    Tags = new[] { "red" }
}, i => i.Index("twitter").Id(1));
response = es.index(
    index='twitter',
    id=1,
    body={
        'counter': 1,
        'tags': ['red'],
    },
)
PUT twitter/_doc/1
{
    "counter" : 1,
    "tags" : ["red"]
}

And then try to retrieve it:

$params = [
    'index'  => 'twitter',
    'id'     => '1',
    'fields' => 'tags,counter',
];
$response = $client->get($params);
var getResponse = client.Get<Tweet>(1, g => g
    .Index("twitter")
    .StoredFields(
        f => f.Tags,
        f => f.Counter)
);
response = es.get(
    index='twitter',
    id=1,
    stored_fields='tags,counter',
)
response = client.get(index: 'twitter', id: 1, stored_fields: 'tags,counter')
GET twitter/_doc/1?stored_fields=tags,counter

The API returns the following result:

{
   "_index": "twitter",
   "_type": "_doc",
   "_id": "1",
   "_version": 1,
   "_seq_no" : 22,
   "_primary_term" : 1,
   "found": true,
   "fields": {
      "tags": [
         "red"
      ]
   }
}

Field values fetched from the document itself are always returned as an array. Since the counter field is not stored, the get request ignores it.

You can also retrieve metadata fields like the _routing field:

$params = [
    'index' => 'twitter',
    'id'    => '2',
    'body'  => [
        'counter' => 1,
        'tags'    => ['white'],
    ],
    'routing' => 'user1',
];
$response = $client->index($params);
var indexResponse = client.Index(new Tweet
{
    Counter = 1,
    Tags = new[] { "white" }
}, i => i
.Index("twitter")
.Id(2)
.Routing("user1")
);
response = es.index(
    index='twitter',
    id=1,
    body={
        'counter': 1,
        'tags': ['white'],
    },
    routing='user1',
)
response = client.index(index: 'twitter', id: 2, routing: 'user1', body: {counter: 1, tags: ['white']})
PUT twitter/_doc/2?routing=user1
{
    "counter" : 1,
    "tags" : ["white"]
}
$params = [
    'index'   => 'twitter',
    'id'      => '2',
    'routing' => 'user1',
    'fields'  => 'tags,counter',
];
$response = $client->get($params);
var getResponse = client.Get<Tweet>(2, g => g
    .Index("twitter")
    .Routing("user1")
    .StoredFields(
        f => f.Tags,
        f => f.Counter)
);
response = es.get(
    index='twitter',
    id=2,
    routing='user1',
    stored_fields='tags,counter',
)
response = client.get(index: 'twitter', id: 2, routing: 'user1', stored_fields: 'tags,counter')
GET twitter/_doc/2?routing=user1&stored_fields=tags,counter

The API returns the following result:

{
   "_index": "twitter",
   "_type": "_doc",
   "_id": "2",
   "_version": 1,
   "_seq_no" : 13,
   "_primary_term" : 1,
   "_routing": "user1",
   "found": true,
   "fields": {
      "tags": [
         "white"
      ]
   }
}

Only leaf fields can be retrieved with the stored_field option. Object fields can’t be returned—​if specified, the request fails.