Quick Links
Introduction
Stored fields in Elasticsearch are fields that are stored directly in the index. These fields are not returned by default when executing a search query. However, it’s important to note that the _source field, even though it is stored by default, is not indexed and thus cannot be searched. Stored fields, on the other hand, are searchable because they are indexed.
The role of stored fields
Stored fields play a crucial role in Elasticsearch. They are used to return the original values of a field in the response of a search query. This is particularly useful when you want to retrieve the original value of a field, not just its analyzed tokens.
For instance, if you have a document with a ‘title’ field, and you want to retrieve the original title when you search for documents, you would use a stored field. The ‘title’ field would be stored and returned in the search results, allowing you to see the original title of each document that matches your query.
Configuring stored fields
By default, fields are not stored in Elasticsearch. This means that if you want to use stored fields, you need to explicitly set the “store” parameter to true in your mapping (it is false by default). Here’s an example:
json { "mappings": { "properties": { "title": { "type": "text", "store": true }, "content": { "type": "text" } } } }
In this example, the ‘title’ field is set to be stored, in contrast to the ‘content’ field which can contain a much larger value and is thus not stored. This means that when you execute a search query, you can retrieve the original value of the ‘title’ field in the search results without having to also retrieve the large content value.
Retrieving stored fields
To retrieve stored fields in the search results, you can use the ‘stored_fields’ parameter in your search query. Here’s an example:
json { "query": { "match_all": {} }, "stored_fields": ["title"] }
In this example, the ‘title’ field is specified in the ‘stored_fields’ parameter. This means that the original value of the ‘title’ field will be returned in the search results, but not the value of the ‘content’ field. It is also worth noting that stored field values will always be returned as arrays.
Limitations of stored fields
While stored fields are useful, they do come with some limitations. One of the main limitations is that they increase the size of the index. This is because the original value of the field is stored in addition to the analyzed tokens. This can lead to increased storage costs and slower search performance.
Another limitation is that stored fields are not as flexible as the _source field. The _source field returns the entire original document, which allows you to retrieve any field in the document. Stored fields, on the other hand, only allow you to retrieve the fields that you have explicitly set to be stored.
Despite these limitations, stored fields are a useful tool in Elasticsearch. They allow you to retrieve the original value of a field in your search results, which can be incredibly useful in many scenarios. However, like any tool, they should be used judiciously and with an understanding of their trade-offs.