Quick links
Introduction
The Elasticsearch Get Source API is a crucial component of Elasticsearch’s robust suite of APIs. It allows users to retrieve the original _source field_ of a document directly, without the need for additional metadata. This API is particularly useful when you only need the source content of a document and not the entire document structure, which can significantly improve performance and efficiency.
How the Get Source API works
The Get Source API operates by directly accessing the _source field of a document. This field is a special one in Elasticsearch, as it contains the original JSON object that was indexed. Elasticsearch stores this JSON object exactly as it was provided, without any modifications or alterations.
To use the Get Source API, you need to specify the index and the ID of the document you want to retrieve. The basic syntax is as follows:
GET /<index>/_source/<id>
For example, if you have an index named ‘products’ and you want to retrieve the source of the document with an ID of 1, you would use the following command:
GET /products/_source/1
This will return the original JSON object that was indexed under the ‘products’ index with the ID of 1.
Partial retrieval with the Get Source API
One of the capabilities of the Get Source API is the ability to retrieve only specific parts of the source document. This is achieved using the `GET /<index>/_source/<id>?_source_includes=<field>` syntax.
For instance, if you only want to retrieve the ‘name’ and ‘price’ fields from the ‘products’ index for the document with an ID of 1, you would use the following command:
GET /products/_source/1?_source_includes=name,price
This will return a JSON object that only includes the ‘name’ and ‘price’ fields.
Disabling the _source field
While the _source field is enabled by default in Elasticsearch, it can be disabled. This is typically done to save space. However, it’s important to note that if the _source field is disabled, you cannot retrieve the original document, and certain features, such as the update and reindex APIs, will not be available.
To disable the _source field, you can use the following command when creating an index:
PUT /products { "mappings": { "_source": { "enabled": false } } }