Benjamin Habegger created OAK-12365:
---------------------------------------

             Summary: [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


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)

Reply via email to