# SearchContextTooLargeException: search context size exceeds limit

- **ID:** `elasticsearch/search-context-too-large`
- **Domain:** elasticsearch
- **Category:** resource_error
- **Verification:** ai_generated
- **Fix Rate:** 78%

## Root Cause

The search request tries to load too many documents or fields into memory, exceeding the search context memory limit.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| elasticsearch 8.11 | active | — | — |
| elasticsearch 8.12 | active | — | — |
| elasticsearch 7.17 | active | — | — |
| opensearch 2.10 | active | — | — |

## Workarounds

1. **Reduce the search request size by using smaller from/size or using search_after for pagination. Example: POST /my_index/_search { "size": 100, "search_after": [12345, "abc"] }** (85% success)
   ```
   Reduce the search request size by using smaller from/size or using search_after for pagination. Example: POST /my_index/_search { "size": 100, "search_after": [12345, "abc"] }
   ```
2. **Increase the search context size limit dynamically: PUT _cluster/settings { "persistent": { "search.max_search_context_size": "10mb" } }** (75% success)
   ```
   Increase the search context size limit dynamically: PUT _cluster/settings { "persistent": { "search.max_search_context_size": "10mb" } }
   ```
3. **Use source filtering to return only needed fields: POST /my_index/_search { "_source": ["field1", "field2"], "size": 1000 }** (80% success)
   ```
   Use source filtering to return only needed fields: POST /my_index/_search { "_source": ["field1", "field2"], "size": 1000 }
   ```

## Dead Ends

- **** — This setting controls aggregation buckets, not search context memory; no effect on this error. (85% fail)
- **** — Search context size limit is separate from heap; heap increase may help indirectly but is not the direct fix. (50% fail)
- **** — Scroll API also creates search contexts; large size worsens the problem. (70% fail)
