fix(elasticsearch): clamp page size to max_result_window

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-26 22:07:34 +08:00
1 parent aac09d3cb7
commit ff88516235
3 files changed
+39 -7

No files matched your search

@@ -41,9 +41,24 @@ export function clampDocumentPage(page: number, pageSize: number, paginationTota
return Math.min(normalizedPage, lastPage);
}
// Elasticsearch rejects any from+size combination above this by default (index.max_result_window).
// DBX can't know a target index's actual configured value up front, so it clamps requests to the
// out-of-the-box default rather than letting an oversized "rows per page" preference reach the cluster as-is.
export const ELASTICSEARCH_DEFAULT_MAX_RESULT_WINDOW = 10_000;
// Clamps a requested page size against the from+size window still available at a given skip offset,
// regardless of where the requested limit came from (a settings default or an explicit override).
export function clampElasticsearchRequestLimit(skip: number, limit: number): number {
const normalizedSkip = Math.max(0, Math.floor(skip));
const normalizedLimit = Math.max(1, Math.floor(limit));
return Math.min(normalizedLimit, Math.max(0, ELASTICSEARCH_DEFAULT_MAX_RESULT_WINDOW - normalizedSkip));
}
export function documentPageRequestLimit(page: number, pageSize: number, paginationTotal?: number): number {
const normalizedPageSize = Math.max(1, Math.floor(pageSize));
if (paginationTotal === undefined) return normalizedPageSize;
const remaining = paginationTotal - Math.max(0, Math.floor(page)) * normalizedPageSize;
return remaining > 0 ? Math.min(normalizedPageSize, remaining) : normalizedPageSize;
const skip = Math.max(0, Math.floor(page)) * normalizedPageSize;
const cappedPageSize = clampElasticsearchRequestLimit(skip, normalizedPageSize);
if (paginationTotal === undefined) return cappedPageSize;
const remaining = paginationTotal - skip;
return remaining > 0 ? Math.min(cappedPageSize, remaining) : cappedPageSize;
}