Saved searches and filters in Elasticsearch - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using saved searches and filters in Elasticsearch, it's important to understand how the time to get results changes as your data grows.
We want to know how the search and filter operations scale with more data and saved queries.
Analyze the time complexity of the following Elasticsearch query using a saved filter.
GET /products/_search
{
"query": {
"bool": {
"filter": [
{ "term": { "category": "electronics" } },
{ "range": { "price": { "lte": 1000 } } }
]
}
}
}
This query searches the products index, applying filters for category and price range.
Let's find the repeated steps in this search with filters.
- Primary operation: Elasticsearch scans matching documents using the filters.
- How many times: It checks each document in the filtered index segments.
As the number of documents grows, the search engine must check more entries to apply filters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows roughly in direct proportion to the number of documents.
Time Complexity: O(n)
This means the time to run the saved search with filters grows linearly as the number of documents increases.
[X] Wrong: "Using saved filters makes the search time constant no matter how many documents there are."
[OK] Correct: Saved filters help reuse queries but the search still needs to check documents, so time grows with data size.
Understanding how saved searches and filters affect performance shows you can think about real data growth and system behavior, a valuable skill in many projects.
What if we added an index on the category field? How would that change the time complexity of the filtered search?
Practice
Solution
Step 1: Understand what saved searches do
Saved searches store queries so you can run them again without rewriting.Step 2: Compare options to this purpose
Only To reuse a query easily without rewriting it every time describes reusing queries easily, which matches saved searches.Final Answer:
To reuse a query easily without rewriting it every time -> Option CQuick Check:
Saved searches = reuse queries [OK]
- Confusing saved searches with data storage
- Thinking saved searches create charts
- Assuming saved searches delete data
Solution
Step 1: Recall filter syntax in Elasticsearch saved searches
Filters are applied inside a filtered query using the "filtered" key.Step 2: Check each option's structure
{"query": {"filtered": {"filter": {"term": {"status": "active"}}}}} correctly uses "query": {"filtered": {"filter": {...}}} which is the right way to apply filters.Final Answer:
{"query": {"filtered": {"filter": {"term": {"status": "active"}}}}} -> Option BQuick Check:
Filter inside filtered query = {"query": {"filtered": {"filter": {"term": {"status": "active"}}}}} [OK]
- Putting filter outside query block
- Using match instead of term for exact filter
- Missing filtered wrapper for filters
{"query": {"filtered": {"query": {"match": {"title": "book"}}, "filter": {"term": {"status": "published"}}}}}Solution
Step 1: Analyze the query and filter parts
The query matches documents where title contains 'book'. The filter restricts to status 'published'.Step 2: Understand filtered query behavior
Filtered query returns documents matching both query and filter conditions (AND logic).Final Answer:
Documents with title containing 'book' and status 'published' -> Option AQuick Check:
Filtered query = query AND filter [OK]
- Thinking query and filter use OR logic
- Ignoring the filter part
- Confusing match and term filters
{"query": {"filtered": {"query": {"match": {"content": "test"}}, "filter": {"term": {"category": "news"}}}}What is wrong with this JSON?
Solution
Step 1: Check JSON structure carefully
The JSON snippet ends without closing all opened braces, causing syntax error.Step 2: Verify other parts are valid
Using 'term' filter and 'match' query inside filtered is correct syntax.Final Answer:
Missing closing braces at the end -> Option DQuick Check:
JSON must be properly closed [OK]
- Ignoring missing braces causing syntax errors
- Thinking 'term' filter is wrong here
- Assuming filters can't be inside queries
Solution
Step 1: Understand the filter requirements
Status must be 'active' AND priority must be 'high' OR 'medium'.Step 2: Identify correct bool filter usage
Use 'must' for AND conditions and 'terms' for multiple values in one field.Step 3: Check each option
{"query": {"filtered": {"filter": {"bool": {"must": [{"term": {"status": "active"}}, {"terms": {"priority": ["high", "medium"]}]}}}}}} uses 'bool' with 'must' array containing 'term' for status and 'terms' for priority, correctly matching requirements.Final Answer:
{"query": {"filtered": {"filter": {"bool": {"must": [{"term": {"status": "active"}}, {"terms": {"priority": ["high", "medium"]}]}}}}}} -> Option AQuick Check:
Bool must + terms array = correct filter [OK]
- Using 'or' instead of 'must' for AND logic
- Putting multiple filters without bool wrapper
- Using multiple term filters for same field instead of terms
