Enterprise Search on multisite
After completing the steps to code-enable Enterprise Search for an environment, an Elasticsearch (ES) index must be created for each network site that will be using Enterprise Search for search queries.
When new sites are added to the multisite network, an index will not be automatically created for them.
VIP-CLI command examples
For demonstration purposes, the <app-name>
value example-app
and the <env>
value develop
are used in the VIP-CLI command examples below. Read more about how to target environments in VIP-CLI commands.
Create an index for a network site
Create an index for a network site with the wp vip-search index --setup
command. Target the specific network site by passing the value of the network site’s ID with the --blog-ids
option.
In this command example, an index is created for network site ID 2
:
vip @example-app.develop -- wp vip-search index --setup --blog-ids=2
Use Search Dev Tools or the wp vip-search health
commands to check that the new index is working as expected.
After an index has been successfully created for the network site, it can be targeted in wp vip-search
commands by passing the site address URL in the --url
argument. If the --url
argument is omitted, the CLI command will target the index of the main site (ID 1).
If a network site has been updated by a SQL database import, use versioning to re-index the site and bring the database and Elasticsearch index back into sync.
Search across network sites
WordPress multisite environments that have Enterprise Search enabled and indexes created for more than one network site can configure search queries to run against multiple indexes.
To configure search to run across more than one site index on a multisite network:
- Create a “network alias index” for all site indexes that currently exist on the multisite network with the command
wp vip-search recreate-network-alias
. This command must be repeated to update the network alias index each time a new index is created for a site on the network.
vip @example-app.develop -- wp vip-search recreate-network-alias
- Define the
EP_IS_NETWORK
constant astrue
invip-config.php
.
define( 'EP_IS_NETWORK', true );
- Using the
pre_get_posts
method, specify which site indexes on the network will handle search queries with thesites
parameter inWP_Query
. The accepted values for thesites
parameter arecurrent
(default), a site ID, an array of site IDs, orall
(all site indexes that currently exist in the “network alias index”).
In this code example, the custom function vip_search_across_sites()
modifies frontend search queries to run across network site ID 2
and network site ID 3
:
add_action( 'pre_get_posts', 'vip_search_across_sites' );
function vip_search_across_sites( $query ) {
if ( $query->is_search() && $query->is_main_query() ) {
$query->set( 'sites', array( 2, 3 ) );
}
}
Alternatively, customizations can be added to the cross-site search configuration via WP_Query
. In this code example, a query will perform a search for the keyword “carrot” across network site ID 2
and network site ID 3
:
$query = new WP_Query(
array(
's' => 'carrot',
'sites' => array( 2, 3 ), // Search only in site IDs 2 and 3
'ep_integrate' => true,
)
);
Create indexes for all sites on a network
Indexes for all network sites on a multisite environment can be created simultaneously by including the --network-wide
option when running the wp vip-search index --setup
command.
For example:
vip @example-app.develop -- wp vip-search index --setup --network-wide
This method should only be used if Enterprise Search is necessary and will be actively used by all sites on the network. Creating unused indexes can negatively impact the indexing and revalidation operations triggered by periodic health checks.
Caution
If a multisite has over 100 sites on the network, it is strongly recommend to create an index on a per-site basis. Rather than creating indexes for all sites, only create indexes for specific sites that will actively use them.
Last updated: August 08, 2024