Source filtering in Elasticsearch - Time & Space Complexity
When using source filtering in Elasticsearch, we want to know how the time to get results changes as we ask for more or fewer fields.
We ask: How does filtering the source affect the work Elasticsearch does?
Analyze the time complexity of the following code snippet.
GET /my_index/_search
{
"_source": ["title", "author"],
"query": {
"match_all": {}
}
}
This code asks Elasticsearch to return only the "title" and "author" fields from all documents.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Elasticsearch reads each matching document and extracts requested fields.
- How many times: Once per matching document in the search results.
As the number of matching documents grows, Elasticsearch extracts fields from each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Extract fields from 10 documents |
| 100 | Extract fields from 100 documents |
| 1000 | Extract fields from 1000 documents |
Pattern observation: The work grows directly with the number of documents returned.
Time Complexity: O(n)
This means the time to filter source fields grows linearly with the number of documents returned.
[X] Wrong: "Filtering source fields makes the search run faster regardless of result size."
[OK] Correct: Filtering reduces data sent back but Elasticsearch still reads each matching document, so time depends on how many documents are returned.
Understanding how source filtering affects performance helps you explain trade-offs in real projects where you want less data but still need fast responses.
What if we changed the source filter to include all fields instead of just a few? How would the time complexity change?
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
