From 480691fe3e3991dc0c3b2901ed668466ee973b07 Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 17 Nov 2026 10:12:50 +0100 Subject: [PATCH] materio_ai: write stored vectors into Solr documents at indexing (I2) SolrDocumentSubscriber listens to PostCreateIndexDocumentsEvent and sets knn1024_vector on each node document from the materio_ai_embedding table (EmbeddingStorage::loadVectors, one query per batch, active model only). No call to the model: a full reindex does not depend on Ollama. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM --- .../custom/materio_ai/materio_ai.services.yml | 6 ++ .../SolrDocumentSubscriber.php | 89 +++++++++++++++++++ .../src/Storage/EmbeddingStorage.php | 27 ++++++ 3 files changed, 122 insertions(+) create mode 100644 web/modules/custom/materio_ai/src/EventSubscriber/SolrDocumentSubscriber.php diff --git a/web/modules/custom/materio_ai/materio_ai.services.yml b/web/modules/custom/materio_ai/materio_ai.services.yml index 13e3672f..1e90d0a1 100644 --- a/web/modules/custom/materio_ai/materio_ai.services.yml +++ b/web/modules/custom/materio_ai/materio_ai.services.yml @@ -28,3 +28,9 @@ services: arguments: ['@config.factory'] tags: - { name: event_subscriber } + + materio_ai.solr_document_subscriber: + class: Drupal\materio_ai\EventSubscriber\SolrDocumentSubscriber + arguments: ['@config.factory', '@materio_ai.embedding_storage'] + tags: + - { name: event_subscriber } diff --git a/web/modules/custom/materio_ai/src/EventSubscriber/SolrDocumentSubscriber.php b/web/modules/custom/materio_ai/src/EventSubscriber/SolrDocumentSubscriber.php new file mode 100644 index 00000000..6fdaefbd --- /dev/null +++ b/web/modules/custom/materio_ai/src/EventSubscriber/SolrDocumentSubscriber.php @@ -0,0 +1,89 @@ + 'addVectors']; + } + + /** + * Adds the vectors to the documents of node items. + */ + public function addVectors(PostCreateIndexDocumentsEvent $event): void { + $config = $this->configFactory->get('materio_ai.settings'); + $dimension = (int) $config->get('dimension'); + $model = (string) $config->get('model'); + if ($dimension <= 0 || $model === '') { + return; + } + + // Item ID "entity:node/123:fr" => nid 123, langcode fr. + $targets = []; + foreach ($event->getSearchApiItems() as $item_id => $item) { + [$datasource_id, $raw_id] = Utility::splitCombinedId($item_id); + if ($datasource_id !== 'entity:node') { + continue; + } + [$nid] = explode(':', $raw_id, 2); + $targets[$item_id] = [(int) $nid, $item->getLanguage()]; + } + if (!$targets) { + return; + } + + $vectors = $this->storage->loadVectors(array_column($targets, 0), $model); + if (!$vectors) { + return; + } + + $field = VectorField::fieldName($dimension); + $documents = $event->getSolariumDocuments(); + foreach ($documents as $document) { + $item_id = $document->getFields()['ss_search_api_id'] ?? NULL; + if (!isset($targets[$item_id])) { + continue; + } + [$nid, $langcode] = $targets[$item_id]; + $vector = $vectors[$nid][$langcode] ?? NULL; + if ($vector !== NULL && count($vector) === $dimension) { + $document->setField($field, $vector); + } + } + $event->setSolariumDocuments($documents); + } + +} diff --git a/web/modules/custom/materio_ai/src/Storage/EmbeddingStorage.php b/web/modules/custom/materio_ai/src/Storage/EmbeddingStorage.php index de2cd310..6a12c1ca 100644 --- a/web/modules/custom/materio_ai/src/Storage/EmbeddingStorage.php +++ b/web/modules/custom/materio_ai/src/Storage/EmbeddingStorage.php @@ -55,6 +55,33 @@ final class EmbeddingStorage { return $row ? self::row($row) : NULL; } + /** + * Returns the vectors of several nodes computed by one model. + * + * One query for a whole indexing batch. + * + * @param int[] $nids + * Node IDs. + * + * @return array> + * Vectors keyed by nid, then langcode. + */ + public function loadVectors(array $nids, string $model): array { + if (!$nids) { + return []; + } + $result = $this->database->select(self::TABLE, 'e') + ->fields('e', ['nid', 'langcode', 'vector']) + ->condition('nid', array_values(array_unique($nids)), 'IN') + ->condition('model', $model) + ->execute(); + $vectors = []; + foreach ($result as $row) { + $vectors[(int) $row->nid][$row->langcode] = self::unpack($row->vector); + } + return $vectors; + } + /** * Returns the text hashes of a node, keyed by langcode. *