Skip to content

Fix stale nested object array indexes on patch update - #2920

Merged
kishorenc merged 7 commits into
typesense:v31from
krunal1313:stale_nested_object_array
Oct 28, 2026
Merged

kishorenc merged 7 commits into
typesense:v31from
krunal1313:stale_nested_object_array

Conversation

@krunal1313

@krunal1313 krunal1313 commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Change Summary

Replacing or deleting a nested object[] field could leave its flattened child values in the in-memory index.
For example, after patching {"reviewQueue":[]}, a filter such as reviewQueue.reviewerId:=9001 could still match the document even though the stored array was empty.

This change removes stale child indexes when an object array is replaced or deleted, including arrays inside nested objects such as wrapper.reviewQueue.

It also preserves unchanged child indexes: changing taskIds while retaining reviewerId keeps the document searchable by that reviewer under both UPDATE and UPSERT.

Implementation

  • Carry the full parent path through recursive update diffing.
  • Use root .flat metadata and root-level deletion output to remove flattened children missing from the update.
  • Leave supplied children to the existing diff logic, preserving unchanged values and reindexing changed values.
  • Restrict child scans to object parents and object arrays, and avoid copying flattened field names.

Validation

The two added regressions failed before the fix and passed afterward. Coverage includes:

  • Unchanged siblings under UPDATE and UPSERT.
  • Empty-array and null replacements inside nested objects.
  • Stored document values and preservation of unrelated nested fields.
  • The original top-level empty-array regression.

PR Checklist

@krunal1313

Copy link
Copy Markdown
Contributor Author

Fixes #2918

@kishorenc

Copy link
Copy Markdown
Member

@krunal1313

A few more changes needed.

  1. The object check only looks at the first element. An array like [null, {"userId": 31}] is accepted on create, and after replacing it with [], userId:=31 still matches. Please check whether any element is an object, and add this case to the test.

  2. Instead of scanning all of .flat for every replaced parent, we can use search_schema.equal_prefix_range() on the full path, the way the null branch already does. That also makes the existing top-level block in the null branch redundant, so it can be removed. In a debug build, an update that nulls 4,000 nested objects took about 25% longer with the scan, and with the prefix lookup it is back to the current cost.

  3. When we remove a child, we should also erase it from new_doc. On an update, new_doc starts as a copy of the old document, and the missing-values index is computed from it, so a track_missing_values child still reads as present (a _missing filter on it doesn't match) until a restart.

@kishorenc
kishorenc merged commit f66a7c1 into typesense:v31 Oct 28, 2026
3 of 4 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.

2 participants