SearchWP

Version 4 Documentation

How to Integrate with Content Imports/Migrations

Note: This documentation is for SearchWP 4
SearchWP 3.x: How to Best Work with Content Imports

SearchWP reacts to certain operations that happen within WordPress. When content is edited, WordPress tells SearchWP about the edit, and SearchWP in turn re-indexes that edited content. This keeps your index up to date over time automatically, without you having to manage anything.

By nature, content import routines (e.g. with WP All Import) make many (sometimes concurrent) edits in a very short timeframe. This can have adverse effects when it comes to SearchWP reacting to all of the signals being sent by WordPress that many edits are taking place.

If the import is big enough and runs fast enough, SearchWP’s indexer can become flooded with edit notifications and eventually the index will be out of sync. This is undesirable as SearchWP can begin cycling through its own index repeatedly or even worse returning inaccurate search results.

Properly handling content imports

NOTE: As of version 4.1.16 of SearchWP WP All Import processes are handled automatically!

Read more about SearchWP’s automatic integration,
or continue reading to implement your own

It is recommended to at the very least disable the SearchWP indexer when running an import script. This can be done by modifying your own import script, or utilizing the hooks available in your chosen plugin. The pseudocode for what we’ll do is as follows:

Pause SearchWP indexer
Tell SearchWP to mark each edited entry for re-indexing
Run import in full
Enable SearchWP indexer
Trigger build of SearchWP index

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Disable SearchWP's automatic integration with WP All Import.
add_filter( 'searchwp\integration\wp-all-import', '__return_false' );
add_action( 'pmxi_before_xml_import', function( $import_id ) {
\SearchWP::$indexer->pause();
}, 10 );
add_action( 'pmxi_saved_post', function( $post_id ) {
$source_name = \SearchWP\Utils::get_post_type_source_name( get_post_type( $post_id ) );
$source = \SearchWP::$index->get_source_by_name( $source_name );
// Mark this to be dropped after the import has finished.
\SearchWP::$index->drop( $source, $post_id );
}, 10 );
add_action( 'pmxi_after_xml_import', function( $import_id ) {
\SearchWP::$indexer->unpause();
// Process all entries marked to be dropped.
\SearchWP::$index->unpause();
\SearchWP::$index->trigger();
}, 10 );

With this snippet in place SearchWP will better react to your content imports, with an accurate index having been built after the import script has completed. This will also allow for your import to run a bit faster since SearchWP will not be competing for resources as the import is processing.