-
Notifications
You must be signed in to change notification settings - Fork 0
feat(slb-531): add Search API index update functionality #518
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
base: release
Are you sure you want to change the base?
feat(slb-531): add Search API index update functionality #518
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @dspachos, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request adds functionality to automatically update the Search API index for entities that reference other entities when those referenced entities are changed or deleted. This is particularly useful for ensuring that content like nodes, which might embed or reference media or other entities, stays up-to-date in the search index when the referenced content is modified. The implementation uses the entity_usage module to find referencing entities and a new service to trigger the Search API updates.
Highlights
- Search API Index Updates: Introduced a new service (
search.api.index.update) that leverages theentity_usagemodule to find all entities that reference a given entity. When a referenced entity is saved or deleted, this service triggers a Search API index update for all its source entities. - Media Entity Integration: Implemented
hook_media_presaveandhook_media_deleteto automatically call the newsearch.api.index.updateservice whenever a Media entity is saved or deleted. This ensures that content referencing media (like nodes with media in their body fields) is re-indexed when the media changes. - Kernel Test Coverage: Added a new kernel test (
SearchApiIndexUpdateTest) to verify that updating a media entity correctly queues both the media entity itself and any referencing nodes (specifically tested with media embedded in a Gutenberg block) for Search API indexing. - Minor Code Style Fixes: Included minor code style adjustments (spacing around
if/elseif) and removed an unusedusestatement in thecustommodule.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces functionality to update the Search API index for entities when entities they reference are changed or deleted. It adds a new service SearchApiIndexUpdate that uses the entity_usage module to find dependent entities and queues them for re-indexing. The changes include implementing hooks for media entities, defining the new service, adding the service class, and providing a kernel test. The core logic appears sound, but there is temporary debugging code that needs removal, and some improvements can be made to documentation, test namespace, and potentially making the hooks more generic.
| dpm(__METHOD__); | ||
| $node = \Drupal\node\Entity\Node::load(25); | ||
| $serializer = new \Drupal\silverback_gutenberg\BlockSerializer(); | ||
|
|
||
| $parser = new \Drupal\gutenberg\Parser\BlockParser(); | ||
|
|
||
| $content = $node->body->getValue()[0]['value']; | ||
| //dpm($content); | ||
| $blocks = $parser->parse($content); | ||
| // dpm($blocks); | ||
|
|
||
| $blocks = [ | ||
| [ | ||
| 'blockName' => 'core/paragraph', | ||
| 'innerContent' => ['<p>A test paragraph</p>'], | ||
| 'attrs' => [], | ||
| 'innerBlocks' => [], | ||
| 'innerHTML' => [], | ||
| ], | ||
| [ | ||
| 'blockName' => 'drupalmedia/drupal-media-entity', | ||
| 'attrs' => [ | ||
| 'caption' => 'This is the caption', | ||
| 'mediaEntityIds' => [1], | ||
| ], | ||
| 'innerContent' => [], | ||
| 'innerBlocks' => [], | ||
| 'innerHTML' => [], | ||
| ] | ||
| ]; | ||
|
|
||
| $html = $serializer->serialize_blocks($blocks); | ||
| dpm($html); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| function silverback_search_media_presave(EntityInterface $entity) { | ||
| \Drupal::service('search.api.index.update')->searchApiIndexUpdate($entity); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hooks silverback_search_media_presave and silverback_search_media_delete are specifically implemented for the media entity type. If the intention is to trigger Search API index updates for usages of other entity types as well (e.g., nodes, taxonomy terms), consider implementing generic hook_entity_presave and hook_entity_delete and adding logic inside to check the entity type, or using event subscribers for a more modern approach.
| function silverback_search_media_delete(EntityInterface $entity) { | ||
| \Drupal::service('search.api.index.update')->searchApiIndexUpdate($entity); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hooks silverback_search_media_presave and silverback_search_media_delete are specifically implemented for the media entity type. If the intention is to trigger Search API index updates for usages of other entity types as well (e.g., nodes, taxonomy terms), consider implementing generic hook_entity_presave and hook_entity_delete and adding logic inside to check the entity type, or using event subscribers for a more modern approach.
| * @todo Add class description. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // $this->index->indexItems(); | ||
| // $tracker->trackAllItemsUpdated(['1']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $this->assertEquals( | ||
| [], | ||
| $tracker->getRemainingItems(), | ||
| '⚠️ Initial index matching error (1)', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $this->assertEquals( | ||
| $expected, | ||
| $tracker->getRemainingItems(), | ||
| '⚠️ Media update index matching error (2)', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.