Source filtering helps you get only the parts of data you want from a search result. This saves time and makes your results easier to use.
0
0
Source filtering in Elasticsearch
Introduction
You want to get only a few fields from a large document to reduce data size.
You want to hide sensitive information like passwords or personal details from search results.
You want to speed up your app by fetching only the needed data.
You want to simplify the data before sending it to another system or user.
You want to avoid transferring large documents over the network.
Syntax
Elasticsearch
{
"_source": ["field1", "field2", ...],
"query": {
... your query ...
}
}You can list fields to include only those in the results.
You can also exclude fields by using an object with includes and excludes arrays.
Examples
This example returns only the
title and author fields from each document.Elasticsearch
{
"_source": ["title", "author"]
}This example includes
title and author but excludes the comments field.Elasticsearch
{
"_source": {
"includes": ["title", "author"],
"excludes": ["comments"]
}
}This example disables source fetching completely, so no document fields are returned.
Elasticsearch
{
"_source": false,
"query": {
"match_all": {}
}
}Sample Program
This search finds documents where the city is London and returns only the name and age fields.
Elasticsearch
{
"_source": ["name", "age"],
"query": {
"match": {
"city": "London"
}
}
}OutputSuccess
Important Notes
Source filtering only affects what fields are returned, not what documents are found.
If you exclude all fields, you get no document data, just metadata like IDs.
Use source filtering to reduce network load and improve performance.
Summary
Source filtering controls which parts of documents you get back from a search.
You can include or exclude specific fields to get just what you need.
This helps make your app faster and your data easier to handle.