Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot remove deleted un-versioned DataObject from Elastic index #1

Open
wants to merge 6 commits into
base: pulls/v3-backport
Choose a base branch
from

Conversation

adunn49
Copy link

@adunn49 adunn49 commented Apr 19, 2024

Description

When a data object or page is deleted from the CMS, a job is created (via the onAfterDelete method defined in the SearchServiceExtension) that should remove this item from the search index.

However, in the case of un-unversioned DataObjects this results in a broken job. This is because the unserialize method expects the DataObject to still exist but by then it has been deleted and no traces of it remain.

With this fix, we remove the need for the data object to exist by setting the 'identifier' and the ClassName on the DataObjectDocument when it is initiated. We then use these as a fallback when no DataObject is available.

Screenshot with fix

Screenshot 2024-04-19 at 5 27 59 PM

@adunn49 adunn49 changed the title Added handling of case where the data object is no longer available -… Cannot remove deleted un-versioned DataObject from Elastic index Apr 21, 2024
@adunn49 adunn49 marked this pull request as ready for review April 21, 2024 20:43
Copy link

@blueo blueo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be a bit were we're bypassing the live check - was that intentional?

@@ -642,7 +693,7 @@ public function onAddToSearchIndexes(string $event): void

$currentDataObject = $this->getDataObject();

$liveDataObject = DataObject::get($currentDataObject->ClassName)->byID($currentDataObject->ID);
$liveDataObject = $currentDataObject ? DataObject::get($currentDataObject->ClassName)->byID($currentDataObject->ID) : null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm if your current object isn't LIVE - then this will put a DRAFT object into the $liveDataObject variable and allow non-live data objects to be added to the index

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are to do with the failing test on the backport branch (see https://github.com/silverstripe/silverstripe-search-service/actions/runs/8748084849/job/24007554595?pr=93) where a data object hasn't been set, and probably more to do with testEvents unit test not setting the data object on the class.

With regards to this change, if $currentDataObject is null, then this sets $liveDataObject to null and the error is thrown. This seems to be doing as intended (and not setting draft objects onto $liveDataObject) but i'll add some further tests so that we are testing this code for unversioned, draft and published and no dataobject set scenarios. Also, perhaps in the case of no dataobject (though this is probably unlikely when adding documents) the code should throw a different error to avoid confusion:

$currentDataObject = $this->getDataObject();

if (!$currentDataObject) {
    throw new IndexingServiceException('No data object set for indexing');
}

$liveDataObject = DataObject::get($currentDataObject->ClassName)->byID($currentDataObject->ID);

if (!$liveDataObject) {
    // unlikely case as indexer calls 'shouldIndex' immediately prior to this
    throw new IndexingServiceException('Only published DataObjects may be added to the index');
}

$this->setDataObject($liveDataObject);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yeah I didn't bring the tests over as it was a bit of effort to do so for the older branch. Ah yes looks like I read it too quickly and its not what I thought it was :)

Are you ok using this fork for now on your projects? I'll probably move these changes in to the latest v3 branch for future support

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in other words - will you be going to CMS5 soon?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we won't be going over to CMS5 just yet, and for now will be using this fork.

if (!$dataObject) {
throw new Exception(sprintf('DataObject %s : %s does not exist', $data['className'], $data['id']));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we keep the exception for things that don't have a fallback?

@ishannz ishannz force-pushed the bugfix/support-unversioned-dataobject-deletion branch from 92aaf2f to 0c3f9d5 Compare November 3, 2024 23:42
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.

3 participants