Skip to content

Full Text Search using Apache Solr


This guide covers integration with Solr 9

Solr

Installation

Download latest version from source

By default, the binary is installed to /opt/. Use the -i flag to specify a custom installation directory, and the -d flag to set a different data directory.

tar zxf solr-9.10.1.tgz -C /opt/
bash /opt/solr-9.10.1/bin/install_solr_service.sh -d /var/solr/

Configuration

After installation, create a Solr core (name it dovecot or anything you prefer):

sudo -u solr /opt/solr/bin/solr create -c dovecot

Stop the Solr service and remove the default configuration files, as they will be replaced with custom ones.

systemctl stop solr

rm /var/solr/data/dovecot/conf/solrconfig.xml
rm /var/solr/data/dovecot/conf/managed-schema.xml
rm /var/solr/data/dovecot/conf/schema.xml

Download the custom schema.xml and solrconfig.xml files and copy them to the core's conf directory:

Finally, change ownership for all newly created files in /var/solr/data/doveot directory and start service

chown -R solr:solr /var/solr/data/dovecot
systemctl start solr

Dovecot

The configuration outlined below does not index email bodies or excluded fields. Administrators should note that large mailboxes will generate substantial indexes, which may exceed the capacity of a single-node Solr cluster.

# conf.d/90-fts.conf

fts_autoindex = yes
fts_autoindex_max_recent_msgs = 999
fts_search_add_missing = yes
fts_search_timeout = 150s

## for solr
fts solr {
  #url = http://localhost:8983/solr/$SOLR_CORE_NAME/
   url = http://localhost:8983/solr/dovecot/
}

# Exclude headers from FTS
fts_header_excludes {
  Body = yes
  Received = yes
  DKIM-* = yes
  X-* = yes
  Authentication-Results = yes
  ARC-* = yes
}

language en {
  default = yes
}

Modify mail_plugins in 20-imap.conf

# conf.d/20-imap.conf
mail_plugins = $mail_plugins fts fts_solr

Run FTS rescan command for all users

doveadm fts rescan -A

In case the -A flag does not work, you should run the rescan command for each user individually. This step forces Dovecot to build the search index when mailboxes are queried.

In case -A command fails, run the rescan per user. This will ensure that dovecot wll index mails at search time.