[
https://issues.apache.org/jira/browse/OAK-12365?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18105296#comment-18105296
]
Benjamin Habegger commented on OAK-12365:
-----------------------------------------
h3. Fix implemented
In {{addOrUpdate()}}, when {{makeDocument()}} returns {{null}} and {{isUpdate}}
is true (the node existed prior to this commit), call
{{context.getWriter().deleteDocument(path)}} to remove any stale document.
Guarded by feature toggle {{FT_OAK-12365}} (enabled by default, kill switch
{{FT_OAK_12365_DISABLE}}).
Covers both the OAK-12244 type-tracking-enabled path and the legacy
(toggle-disabled) path, since both funnel through {{addOrUpdate()}}.
h3. Notable finding during verification
A *direct* (non-relative) property removal on the same node was already handled
correctly before this fix: {{FulltextDocumentMaker.removeProperties()}} forces
{{dirty = true}} whenever a previously-indexed property is removed, so a
rebuilt (property-less) document is written instead of {{null}}. The actual gap
only manifests when the last indexable content comes from a
*relative/aggregated* property (e.g. via {{aggregateRule}} or a relative
property definition): the root node's own {{propertiesModified}} list stays
empty in that case, so the forced-dirty rebuild never triggers, and
{{makeDocument()}} genuinely returns {{null}}.
Also worth noting for anyone relying on plain equality/range queries as a
correctness check: Oak's query engine (SelectorImpl.currentOakProperty)
re-fetches property values from the live tree to evaluate simple WHERE
conditions, so a stale index document does not by itself cause wrong results
for {{WHERE prop = 'x'}} style queries. The observable impact is on
fulltext/facet/suggest/excerpt results (which read straight from the index row)
and on index hygiene (stale documents never purged).
h3. Test Evidence
Before fix: {{nodeLosesLastAggregatedPropertyTriggersDocumentDeletion}},
{{nodeLosesLastAggregatedPropertyTriggersDocumentDeletionWhenMixinToggleDisabled}}
(unit), {{nodeLosesLastAggregatedPropertyDisappearsFromFulltextIndex}}
(integration, fulltext CONTAINS query) all fail.
After fix: all pass. Full {{oak-search}} + {{oak-lucene}} suites: 1250 tests, 0
failures, 0 errors.
h3. Affected Files
* {{oak-search}}: {{FulltextIndexEditor}} (core fix)
* {{oak-search}}: {{PropertyIndexCommonTest}} (new integration test, all
backends)
* {{oak-lucene}}: {{LuceneIndexEditor2Test}} (new unit tests verifying writer
interactions directly)
> [oak-search] FulltextIndexEditor does not delete a document when a node's
> last indexed property is removed
> ----------------------------------------------------------------------------------------------------------
>
> Key: OAK-12365
> URL: https://issues.apache.org/jira/browse/OAK-12365
> Project: Jackrabbit Oak
> Issue Type: Bug
> Components: oak-search
> Reporter: Benjamin Habegger
> Priority: Major
>
> h3. Summary
> When a node has an existing index document and a later commit removes the
> last property that was actually being indexed on it (its indexing rule/type
> is unchanged), the stale document is never deleted — it stays in the index
> and keeps matching queries on the old value.
> h3. Root Cause
> {{FulltextIndexEditor.addOrUpdate()}} calls {{makeDocument()}} to (re)build
> the document for a node on every commit that touches it. If the node has no
> indexable content left, {{makeDocument()}} returns {{null}} (see
> {{FulltextDocumentMaker.makeDocument()}}: the {{isUpdate \&\& !dirty}}
> branch, and, for property indexes, the {{!indexesAllNodesOfMatchingType()
> \&\& !dirty}} branch) — and {{addOrUpdate()}} just returns {{false}}. There
> is no branch anywhere in that method, or its callers, that calls
> {{deleteDocument()}} for this case.
> This is a *content* transition — the indexing rule still matches the node,
> but the document's indexable content disappeared. It is distinct from the
> *rule* transition fixed in OAK-12244 ({{wasIndexable}}/{{isIndexable()}}
> tracking for mixin add/remove): that fix does not cover this case, since
> {{indexingRule}} never becomes null here.
> This is only a bug on the *update* path. A brand-new node that never had an
> indexed property is fine as-is — {{makeDocument()}} returning {{null}} there
> is correct, since no document was ever created for it.
> Affects every backend sharing this class: {{oak-lucene}} and
> {{oak-search-elastic}}.
> h3. Example
> # Index rule for {{nt:base}} indexes property {{status}}.
> # Commit 1: create {{/test/a}} with {{status=published}}. Document is
> created. {{WHERE status = 'published'}} returns {{/test/a}}.
> # Commit 2: remove the {{status}} property from {{/test/a}} (type unchanged).
> The rule still matches, so {{indexingRule}} is non-null; {{makeDocument()}}
> now returns {{null}} since there is nothing left to index, and
> {{addOrUpdate()}} does nothing further.
> # {{WHERE status = 'published'}} still returns {{/test/a}} — the node no
> longer has that property at all, but its stale document remains.
> h3. Fix
> In {{addOrUpdate()}}, when {{makeDocument()}} returns {{null}} and
> {{isUpdate}} is true (the node existed prior to this commit), call
> {{context.getWriter().deleteDocument(path)}} to remove any stale document for
> it. {{deleteDocument()}} is a safe no-op when no document currently exists
> for the path (confirmed for both the Lucene term-delete and the Elastic
> bulk-delete-by-id writers), so this is correct even when the node was never
> actually indexed. Guarded by a feature toggle, enabled by default, per this
> codebase's convention for bug-fix toggles.
> h3. Affected Files
> * {{oak-search}}: {{FulltextIndexEditor}} (core fix)
> * {{oak-search}}: {{PropertyIndexCommonTest}} (new integration test, all
> backends)
> * {{oak-lucene}}: {{LuceneIndexEditor2Test}} (new unit tests verifying writer
> interactions directly)
--
This message was sent by Atlassian Jira
(v8.20.10#820010)