elasticsearch resource_error ai_generated true

SearchContextTooLargeException: search context size exceeds limit

ID: elasticsearch/search-context-too-large

Also available as: JSON · Markdown · 中文
78%Fix Rate
82%Confidence
1Evidence
2024-03-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
elasticsearch 8.11 active — — —
elasticsearch 8.12 active — — —
elasticsearch 7.17 active — — —
opensearch 2.10 active — — —

Root Cause

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

generic

中文

搜索请求尝试加载过多文档或字段到内存,超出搜索上下文内存限制。

Official Documentation

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-settings.html#search-settings-max-search-context-size

Workarounds

  1. 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"] }
    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. 75% success Increase the search context size limit dynamically: PUT _cluster/settings { "persistent": { "search.max_search_context_size": "10mb" } }
    Increase the search context size limit dynamically: PUT _cluster/settings { "persistent": { "search.max_search_context_size": "10mb" } }
  3. 80% success Use source filtering to return only needed fields: POST /my_index/_search { "_source": ["field1", "field2"], "size": 1000 }
    Use source filtering to return only needed fields: POST /my_index/_search { "_source": ["field1", "field2"], "size": 1000 }

中文步骤

  1. 通过使用较小的from/size或search_after分页来减少搜索请求大小。
  2. 动态增加搜索上下文大小限制。
  3. 使用source filtering只返回必要字段。

Dead Ends

Common approaches that don't work:

  1. 85% fail

    This setting controls aggregation buckets, not search context memory; no effect on this error.

  2. 50% fail

    Search context size limit is separate from heap; heap increase may help indirectly but is not the direct fix.

  3. 70% fail

    Scroll API also creates search contexts; large size worsens the problem.