From ce6124b70749091402ddd5f7fe1c2661e6aaf7f0 Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 17 Nov 2026 09:41:33 +0100 Subject: [PATCH] materio_ai: dense vector field in the generated Solr configset (I1) SolrConfigSubscriber listens to PostConfigFilesGenerationEvent (search_api_solr 4.x no longer calls hook_search_api_solr_config_files_alter) and appends the knn_vector_1024 DenseVectorField (cosine, HNSW) and the knn1024_* dynamic field (not stored). Solr 9.1 at least. Dependency on search_api_solr added. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM --- .../custom/materio_ai/materio_ai.info.yml | 1 + .../custom/materio_ai/materio_ai.services.yml | 6 ++ .../EventSubscriber/SolrConfigSubscriber.php | 59 +++++++++++++++++++ .../materio_ai/src/Solr/VectorField.php | 55 +++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 web/modules/custom/materio_ai/src/EventSubscriber/SolrConfigSubscriber.php create mode 100644 web/modules/custom/materio_ai/src/Solr/VectorField.php diff --git a/web/modules/custom/materio_ai/materio_ai.info.yml b/web/modules/custom/materio_ai/materio_ai.info.yml index c826da5f..f9e1e16a 100644 --- a/web/modules/custom/materio_ai/materio_ai.info.yml +++ b/web/modules/custom/materio_ai/materio_ai.info.yml @@ -8,4 +8,5 @@ dependencies: - address:address - drupal:node - drupal:taxonomy + - search_api_solr:search_api_solr - workflow:workflow diff --git a/web/modules/custom/materio_ai/materio_ai.services.yml b/web/modules/custom/materio_ai/materio_ai.services.yml index ef364471..13e3672f 100644 --- a/web/modules/custom/materio_ai/materio_ai.services.yml +++ b/web/modules/custom/materio_ai/materio_ai.services.yml @@ -22,3 +22,9 @@ services: class: Drupal\materio_ai\Vectorizer arguments: ['@entity_type.manager', '@config.factory', '@queue', '@materio_ai.corpus_builder', '@materio_ai.embedding_client', '@materio_ai.embedding_storage'] Drupal\materio_ai\Vectorizer: '@materio_ai.vectorizer' + + materio_ai.solr_config_subscriber: + class: Drupal\materio_ai\EventSubscriber\SolrConfigSubscriber + arguments: ['@config.factory'] + tags: + - { name: event_subscriber } diff --git a/web/modules/custom/materio_ai/src/EventSubscriber/SolrConfigSubscriber.php b/web/modules/custom/materio_ai/src/EventSubscriber/SolrConfigSubscriber.php new file mode 100644 index 00000000..ac7dd25f --- /dev/null +++ b/web/modules/custom/materio_ai/src/EventSubscriber/SolrConfigSubscriber.php @@ -0,0 +1,59 @@ + 'addVectorField']; + } + + /** + * Appends the field type and the dynamic field to the schema_extra files. + */ + public function addVectorField(PostConfigFilesGenerationEvent $event): void { + if (version_compare($event->getLuceneMatchVersion(), '9.1', '<')) { + return; + } + $dimension = (int) $this->configFactory->get('materio_ai.settings')->get('dimension'); + if ($dimension <= 0) { + return; + } + $files = $event->getConfigFiles(); + foreach ([ + 'schema_extra_types.xml' => VectorField::typeXml($dimension), + 'schema_extra_fields.xml' => VectorField::dynamicFieldXml($dimension), + ] as $file => $xml) { + if (isset($files[$file]) && !str_contains($files[$file], $xml)) { + $files[$file] .= $xml; + } + } + $event->setConfigFiles($files); + } + +} diff --git a/web/modules/custom/materio_ai/src/Solr/VectorField.php b/web/modules/custom/materio_ai/src/Solr/VectorField.php new file mode 100644 index 00000000..23ba9cc3 --- /dev/null +++ b/web/modules/custom/materio_ai/src/Solr/VectorField.php @@ -0,0 +1,55 @@ + for schema_extra_types.xml. + * + * Cosine: no assumption on the norm of the vectors sent by the model. + */ + public static function typeXml(int $dimension): string { + return sprintf( + "\n", + self::typeName($dimension), + $dimension, + ); + } + + /** + * for schema_extra_fields.xml. + * + * Not stored: the vectors live in the materio_ai_embedding table. + */ + public static function dynamicFieldXml(int $dimension): string { + return sprintf( + "\n", + $dimension, + self::typeName($dimension), + ); + } + +}