Skip to content

Setting up

Requirements

  • Drupal 10.3 or 11.
  • The AI module 1.3 or later, the standalone AI Search module 1.3 or later, and Key. The AI module's own AI Search submodule is deprecated and is removed in AI 1.6.0 (change record), so the provider requires the standalone module.
  • Solr 9.1 or later. Solr 9.0 applies filter queries after the kNN search instead of before, so filtered searches come back short there. Solr 10 adds quantized vectors (scalar and binary), which hold several times as many vectors in the same memory. Tested on Solr 9.1.1, 9.10 and 10.0.0.
  • A core for the vectors, made from Solr's _default configset. For example, in Docker:

    docker run -d --name solr -p 8983:8983 solr:10.0.0 solr-precreate vectors
    

    The provider adds its vector fields and their types through the Schema API, and turns off the configset's guessing of new fields.

To run Solr on a server of its own, see Solr on DigitalOcean.

Installing

Until AI Search 1.3 has a stable release, allow its alpha in the same command:

composer require 'drupal/ai_search:^1.3@alpha' 'drupal/ai_vdb_provider_solr:^1.0@alpha'
drush pm:install ai_vdb_provider_solr

Connecting to Solr

At Configuration » AI » Vector Database Providers » Solr Configuration (/admin/config/ai/vdb_providers/solr):

Setting What to give
Scheme http, or https behind TLS.
Host The Solr server's host name, without the scheme.
Port 8983 for Solr itself, 443 behind an HTTPS proxy.
Path to Solr The path before /solr/, usually /.
Solr context The path segment before the core: solr on Solr itself. Empty where a gateway serves the core without it (see Pantheon Search).
Core or collection The core made for the vectors, such as vectors.
User name For basic authentication; leave empty without it.
Password A Key that holds the basic authentication password.
Verify the TLS certificate On, unless Solr answers https with a certificate no authority has signed (see below).
Vector quantization For new collections: none, scalar or binary. Scalar and binary need Solr 10; the form refuses them on an older Solr.
Commit within (ms) How soon indexed chunks become searchable.
Timeout (s) How long each request to Solr may take. Connecting gives up after at most 5 seconds. A ping, which AI Search sends to check that the server is available, waits at most 2 seconds.

The form pings the core before it saves. The core's address is the scheme, host and port, then the path, the context and the core: with a path of / and the context solr, https://solr.example.com:443/solr/vectors. With an empty context there is nothing between the path and the core.

The form warns when basic authentication is used over plain http, which sends the password in clear text. The settings are validated when they are saved.

The same settings can be set with Drush:

drush config:set ai_vdb_provider_solr.settings host solr.example.com -y

TLS with a private CA

The module has no setting for this. Drupal's http_client takes Guzzle options from settings.php:

$settings['http_client_config']['verify'] = '/path/to/ca.pem';

This applies to every request the site makes through http_client.

A certificate no authority has signed

Some hosts put Solr behind https with a self-signed certificate on their own private network. Turning Verify the TLS certificate off stops the provider checking Solr's certificate, for its own requests only; every other request the site makes is still verified. Without the check the site cannot tell Solr from a server that pretends to be it, so use it only on a network you trust, and prefer a private CA (above) where the host gives you one.

Pantheon Search on Solr 9 (search: version: 9 in pantheon.yml) serves each environment's core at an address with no /solr/ in it, over https with a self-signed certificate. Pantheon gives the address in environment variables, so set the connection in settings.php, where it follows the environment:

if (getenv('PANTHEON_INDEX_HOST')) {
  $config['ai_vdb_provider_solr.settings'] = [
    'scheme' => getenv('PANTHEON_INDEX_SCHEME') ?: 'https',
    'host' => getenv('PANTHEON_INDEX_HOST'),
    'port' => (int) getenv('PANTHEON_INDEX_PORT'),
    'path' => '/' . trim(getenv('PANTHEON_INDEX_PATH') . '/' . dirname(trim(getenv('PANTHEON_INDEX_CORE'), '/')), '/'),
    'context' => '',
    'core' => basename(getenv('PANTHEON_INDEX_CORE')),
    'verify' => FALSE,
  ] + ($config['ai_vdb_provider_solr.settings'] ?? []);
}

A Pantheon core starts with Search API Solr's configset, whose schema cannot be changed through the Schema API. The provider needs a core made from Solr's _default configset, so post that configset to the environment (Pantheon's PANTHEON_INDEX_SCHEMA endpoint, which Search API Pantheon's drush search-api-pantheon:postSchema uses) and reload the core before saving a server. Each environment has one core, so it holds either this configset or Search API Solr's, not both.

The ai_vdb_provider_solr_pantheon submodule does that step without Search API Pantheon, whose command needs a Search API server on its Pantheon connector. It ships Solr 9.10.1's _default configset and does nothing unless PANTHEON_ENVIRONMENT is set:

drush pm:install ai_vdb_provider_solr_pantheon
terminus drush <site>.<env> -- ai-vdb-solr-pantheon:post-configset -y

The command posts the configset to the environment's core, reloads it, and waits until the core answers with the _default schema, default-config, printing the schema it reads every five seconds (--timeout, 120 seconds by default). It names any Search API server on Pantheon's connector first, since replacing the configset stops it working. Run it in each environment (dev, test, live and each multidev), and again after the search version in pantheon.yml changes, which gives the environment a fresh core. drush ai-vdb-solr-pantheon:status, the status report and the provider's settings form say when an environment needs it.

Posting the configset keeps existing collections working: the command adds back the vector field of each collection the core holds, which the new configset lacks. Pantheon's reads lag a reload by several seconds, so the command first notes the vector fields the schema has, waits until they are gone from the reloaded schema, adds them back, and waits until they read back before it reports success. If the old fields are still listed at the timeout, it adds nothing and fails: run it again. In a new environment, post the configset before the first indexing.

Creating a collection

  1. At Configuration » Search and metadata » Search API, add a server.
  2. Choose the AI Search backend and an Embeddings Engine.
  3. Choose Apache Solr as the Vector Database.
  4. Under Vector Database Configuration, name a Collection and choose the Similarity Metric.
  5. Leave Include raw embedding vector in results off: the provider indexes vectors without storing them, so it cannot return them.
  6. Save the server.

The collection is created in the core when the server is saved, or, for a server imported from configuration, when it first indexes. A collection's dimension, similarity and quantization are fixed when it is created.