Events/Hooks to Fire Actions on Index Activity

Created on 16 June 2023, about 1 year ago
Updated 18 June 2023, about 1 year ago

Respect the submission guidelines above! Drupal.org issue forks cause additional work for the project maintainer!

Setup

  • Solr version:8.4.0
  • Drupal Core version:9.5.9
  • Search API version:8.x-1.28
  • Search API Solr version: 4.2.10
  • Configured Solr Connector:Standard

Issue

I asked this question in Slack but no responses, so I figured maybe I should ask it differently.

I am trying to sync my Solr index with a vector store. What I need is to be able to react any time a Solr index object is created, updated, deleted or (re)indexed.

My question is simple: Are there events or hooks which will allow me fire an action when any of these events occur? I mean, I know there are, but Google Search and ChatGPT just aren't helping to figure this out, so where is the code/documentation to help me get this process started? Thanks!

p.s. This is where my search began, but I don't see any event here which will help do what I am trying to do: https://git.drupalcode.org/project/search_api_solr/-/blob/4.x/src/Event/...

πŸ’¬ Support request
Status

Fixed

Version

4.2

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States SomebodySysop

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @SomebodySysop
  • πŸ‡ΊπŸ‡ΈUnited States SomebodySysop

    Thank you. It looks like this is what I could use to react to any event on any Solr index object. Correct?

    /**
    * The postExecute event is thrown just after all execution is done.
    *
    * The event listener receives a Query instance and a Result instance.
    *
    * @var string
    */
    public const POST_EXECUTE = PostExecute::class;

    So,

    use Drupal\search_api\Query\QueryInterface;
    use Drupal\search_api\Query\ResultSetInterface;
    use Drupal\search_api_solr\Event\PostExecute;
    
    class MyModule {
    
      /**
       * The postExecute event is thrown just after all execution is done.
       *
       * The event listener receives a Query instance and a Result instance.
       *
       * @var string
       */
      public const POST_EXECUTE = PostExecute::class;
    
      /**
       * Event subscriber callback for the postExecute event.
       *
       * @param \Drupal\search_api\Query\QueryInterface $query
       *   The query object.
       * @param \Drupal\search_api\Query\ResultSetInterface $result_set
       *   The result set object.
       */
      public function onPostExecute(QueryInterface $query, ResultSetInterface $result_set) {
        // My code here.
      }
    
    }

    I look at the $query to find the function (update, create, delete, index) and the $result to get the item(s) affected.

    On at least the right track here?

  • Status changed to Fixed about 1 year ago
  • πŸ‡©πŸ‡ͺGermany mkalkbrenner πŸ‡©πŸ‡ͺ

    It depends on your requirements. If you need informations about a Drupal entity, it might be better to leverage Search API's events.

    But with these events of all three layers, you can track the entire action. Now it is up to you.

  • πŸ‡ΊπŸ‡ΈUnited States SomebodySysop

    No, I am only trying to track when a solr index object is affected: created, updated, indexed or deleted. Period.

    Thanks for the info. I'll see what I can do.

  • πŸ‡ΊπŸ‡ΈUnited States SomebodySysop

    Actually, I was able to accomplish my goals using the entity hooks. I wanted to use event subscriber instead, but just couldn't figure out the correct events to use. At any rate, for what I wanted to do (sync Solr index with vector store), I didn't need to monitor Search API (or Search API Solr) events. As Search API Solr automatically updates the Solr index on CRUD events, I only needed to monitor the Drupal entities for changes.

    Again, for anyone else who bumps up against something like this, here is how I handled it:

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024