Skip to content

fix(index): rebuild cleanup safety + search request-shape and pagination fixes - #6

Merged
ykan821 merged 10 commits into
masterfrom
dev
Nov 2, 2026
Merged

ykan821 merged 10 commits into
masterfrom
dev

Conversation

@ykan821

@ykan821 ykan821 commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Summary

Six fixes from an external review of 8.0.0, plus shared unit-test infrastructure.

  • fix(index): Rebuild's failure cleanup deleted the new index unconditionally — if updateAliases was applied but its response was lost, that destroyed the index the alias had just switched to (dangling alias, lost writes). The swap catch now verifies the alias first and keeps the new index when the swap may have landed or the state cannot be checked; verified orphans are still removed, and cleanup failures no longer mask the original exception.
  • fix(index): count() sent the whole search body to _count, which accepts a query only — any chained param (size, sort, aggs, ...) got a 400. The body is now projected down to the query (omitted when empty) and routing() is carried.
  • fix(index): doSearch() wrote the index's track_total_hits default back into the shared Query, so get() then scroll() on one Search sent a disabled total into a scroll context (ES rejects it, 400) and an explicit track_total_hits: true in a raw body was silently overridden. Defaults now merge into the request only. The same blind spot made scroll()'s batch-size default override a raw-body size — fixed alike.
  • fix(index): with an int trackTotalHits cap, hasMorePages() treated the lower-bound total (relation: gte) as exact and stopped pagination at the cap boundary. A gte total now only proves pages below it exist; at or past the bound the full-page heuristic applies. lastPage() keeps the bound-derived estimate, matching the earlier capped-total decision.
  • fix(index): cursor() kept each scroll batch's 0-based keys, so iterator_to_array($search->cursor()) dropped all but the last batch. Hits now yield with continuous keys.
  • docs(readme): the clause-append example used two consecutive bool() calls — sibling clauses under one query key, which throw on serialize. Now appends inside a single bool; the throwing behavior is pinned by a test.
  • refactor(test): new unit coverage for Search/Rebuild request shape and failure paths, backed by shared tests/Support/FakeClient doubles (recorded calls, scripted responses); test files follow the per-class taxonomy.

Behavior note: search.query.before/after events with action=count now carry the query-only body in dsl instead of the full (rejected) search body.

🤖 Generated with Claude Code

ykan821 and others added 10 commits July 27, 2026 20:27
Register a callable via ClientManager::setResolver() / Index::setClientResolver() that builds the ES client on first get(), memoized per connection. Defers construction for runtime-context clients and long-lived workers without changing the static registration API; set()/setResolver() are mutually exclusive per connection (last write wins).

Update README and docs examples to register via resolver.

Co-Authored-By: Claude <noreply@anthropic.com>
…es trait

Agg and Node had byte-identical resolveProperties logic. Move it to a
shared trait so Agg stays independent of Node and Query/Intervals/
FunctionScore still inherit it via Node.
…ailure

A lost response can leave the swap outcome ambiguous: updateAliases may
have been applied server-side while the client still sees an exception.
The old cleanup deleted the new index unconditionally, which in that
window destroys the index the alias just switched to (dangling alias,
lost writes, rollback() unusable).

Now the swap catch checks getAlias first: keep the new index when it is
already aliased or the state cannot be verified (operator resolves via
rollback()/clean()), delete only when the alias verifiably does not point
to it. Cleanup deletes no longer mask the original exception.

Co-Authored-By: Claude <noreply@anthropic.com>
_count only accepts a query in its body, so any chained search param
(size, sort, aggs, ...) made ES reject the request with 400. Extract the
query from the built body instead of sending it whole, omit the body when
there are no clauses, and include urlParams (routing) like doSearch does.

Co-Authored-By: Claude <noreply@anthropic.com>
Two fixes had each grown its own ClientInterface double. Consolidate into
tests/Support (FakeClient + FakeIndicesNamespace + FakeResult): call
recording, per-endpoint outcome queues, and Throwable scripting. Unit
tests may now cover client interaction (request shape, unreproducible
failure paths) without re-rolling this infrastructure.

Co-Authored-By: Claude <noreply@anthropic.com>
SearchCountTest -> SearchTest, RebuildCleanupTest -> RebuildTest,
matching the one-class-per-file taxonomy of the unit suite. Upcoming
Search fixes (doSearch pollution, cursor keys) land in the same files.

Co-Authored-By: Claude <noreply@anthropic.com>
…query

doSearch wrote the index's track_total_hits default back into the shared
Query, so reusing a Search leaked it into later calls: scroll() after
get() sent track_total_hits=false and ES rejected the scroll (400), and
an explicit track_total_hits=true in a raw body was silently overridden
because hasParam() only sees chained params.

Merge the default into the request body only, checking the final body
(covers both chained params and raw bodies). The same hasParam blind spot
made scroll()'s size default override a raw-body size with 1000 — check
the final body there too. hasParam() keeps its chained-only semantics.

Co-Authored-By: Claude <noreply@anthropic.com>
With an int track_total_hits cap, ES reports hits.total as a lower bound
(relation "gte"). hasMorePages() compared page against lastPage()
computed from that bound, so the page at the cap boundary reported no
more pages even when data continued — violating its contract as a
next-page signal.

Trust a gte total only to prove pages below the bound exist; at or past
it, fall back to the full-page heuristic already used when the total is
absent. lastPage() keeps returning the bound-derived estimate, matching
the earlier decision that capped totals feed pagination as-is.

Co-Authored-By: Claude <noreply@anthropic.com>
yield from preserved each scroll batch's 0-based keys, so flattening
with iterator_to_array() — which preserves keys by default — silently
dropped all but the last batch. Yield each hit individually to assign
continuous keys across the whole generator.

Co-Authored-By: Claude <noreply@anthropic.com>
The example showed two consecutive $q->bool() calls, which are sibling
clauses under one query key and throw on serialization. Append within a
single bool instead, and state the sibling-clause rule explicitly. Pin
the throwing behavior with a test so the docs cannot drift again.

Co-Authored-By: Claude <noreply@anthropic.com>
@ykan821
ykan821 merged commit 8b74edf into master Nov 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant