Extending¶
Events¶
Two events let another module change what the provider sends to Solr.
| Event | Dispatched | What can change |
|---|---|---|
Drupal\ai_vdb_provider_solr\Event\PreIndexEvent |
Before chunks' documents are sent to Solr. | $documents, one array of fields for each chunk. $collection is read-only. |
Drupal\ai_vdb_provider_solr\Event\PreSearchEvent |
Before each search request is sent to Solr. | $request, the body for Solr's JSON Request API. $collection is read-only, and $query is the Search API query, or NULL for a plain query. |
A grouped kNN search can send more than one request, each with a larger topK,
and dispatches PreSearchEvent for each.
A field that the core's schema lacks fails the request, because the provider turns off the guessing of new fields. Add the field to the schema first.
Example¶
Register a subscriber:
services:
example_module.solr_subscriber:
class: Drupal\example_module\EventSubscriber\SolrSubscriber
tags:
- { name: event_subscriber }
Add a filter query to every search:
namespace Drupal\example_module\EventSubscriber;
use Drupal\ai_vdb_provider_solr\Event\PreIndexEvent;
use Drupal\ai_vdb_provider_solr\Event\PreSearchEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class SolrSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
PreIndexEvent::class => 'onPreIndex',
PreSearchEvent::class => 'onPreSearch',
];
}
public function onPreIndex(PreIndexEvent $event): void {
foreach ($event->documents as &$document) {
$document['source_s'] = 'drupal';
}
}
public function onPreSearch(PreSearchEvent $event): void {
$event->request['filter'][] = 'source_s:drupal';
}
}
The Solr client¶
The Solr client comes from the ai_vdb_provider_solr.client_factory service.
It builds one client for each distinct connection.