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.
Source filtering in 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.
title and author fields from each document.{
"_source": ["title", "author"]
}title and author but excludes the comments field.{
"_source": {
"includes": ["title", "author"],
"excludes": ["comments"]
}
}{
"_source": false,
"query": {
"match_all": {}
}
}This search finds documents where the city is London and returns only the name and age fields.
{
"_source": ["name", "age"],
"query": {
"match": {
"city": "London"
}
}
}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.
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.
Practice
_source filtering in Elasticsearch?Solution
Step 1: Understand
_sourcefiltering role_sourcefiltering controls which fields of a document are included in the search response.Step 2: Differentiate from other features
It does not affect indexing or sorting, only what data is returned after a search.Final Answer:
To control which parts of the document are returned in search results -> Option AQuick Check:
Source filtering = control returned fields [OK]
- Confusing source filtering with indexing filters
- Thinking it changes document content
- Assuming it sorts results
password from the search results using source filtering?Solution
Step 1: Recall source filtering syntax
To exclude fields, use"excludes"inside_sourcewith a list of field names.Step 2: Match correct syntax
"_source": { "excludes": ["password"] } uses"excludes": ["password"], which is the correct way to exclude thepasswordfield.Final Answer:
"_source": { "excludes": ["password"] } -> Option CQuick Check:
Exclude fields = use "excludes" key [OK]
- Using "include" to exclude fields
- Using non-existent keys like "filter" or "remove"
- Wrong JSON structure
{
"_source": { "includes": ["title", "author"] },
"query": { "match_all": {} }
}Solution
Step 1: Understand
Theincludesin_sourceincludeskey specifies which fields to return from the document.Step 2: Apply to given query
Since onlytitleandauthorare included, only these fields will appear in the results.Final Answer:
Only the fieldstitleandauthorwill be returned -> Option DQuick Check:
includes fields = return only those fields [OK]
- Thinking includes excludes fields
- Assuming all fields return by default
- Confusing includes with excludes
{
"_source": {
"includes": "title,author"
},
"query": { "match_all": {} }
}Solution
Step 1: Check
Theincludesvalue typeincludesfield expects an array of strings, not a single comma-separated string.Step 2: Identify correct format
It should be["title", "author"]instead of"title,author".Final Answer:
The includes field should be a list, not a comma-separated string -> Option BQuick Check:
includes must be an array, not a string [OK]
- Using comma-separated strings instead of arrays
- Misspelling _source key
- Confusing query errors with source filtering
password and ssn from documents, but also include only the name and email fields explicitly. Which source filtering configuration achieves this correctly?Solution
Step 1: Understand combined includes and excludes
Using bothincludesandexcludeslets you specify fields to return and fields to omit.Step 2: Analyze options
"_source": { "includes": ["name", "email"], "excludes": ["password", "ssn"] } includesnameandemailbut excludespasswordandssn, matching the requirement.Final Answer:
"_source": { "includes": ["name", "email"], "excludes": ["password", "ssn"] } -> Option AQuick Check:
includes + excludes together filters fields precisely [OK]
- Using only includes or excludes, missing fields
- Including fields meant to be excluded
- Misunderstanding combined filtering behavior
Ready to prove it?
Pass the test and this pattern moves unknown
