A WordPress plugin to connect to Pantheon's Apache Solr search infrastructure, or your own!
Contributors: getpantheon, Outlandish Josh, 10up, collinsinternet, andrew.taylor, danielbachhuber, mattleff, mikengarrett, jazzsequence, jspellman, pwtyler
Tags: search
Requires at least: 4.6
Requires PHP: 7.1
Tested up to: 6.7.1
Stable tag: 2.6.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Improve your user experience with the Apache Solr search engine for your WordPress website.
Search is critical for your site, but the default search for WordPress leaves a lot to be desired. Improve your user experience with the Apache Solr search engine for your WordPress website.
solr_integrate
parameter set to true.The Solr Power plugin can be installed just like you’d install any other WordPress plugin.
Because Solr Power is intended to be a bridge between WordPress and the Apache Solr search engine, you’ll need access to a functioning Solr 3.6 instance for the plugin to work as expected. This plugin does not support other versions of Solr. The plugin also requires PHP 7.1 or higher.
If you’re using the Solr Power plugin on Pantheon, setting up Apache Solr is as easy as enabling the Apache Solr add-on in your Pantheon dashboard. Once you’ve done so:
If you’re using the Solr Power plugin elsewhere, you’ll need to install and configure Apache Solr. On a Linux environment, this involves four steps:
./bin/install-solr.sh
to install and run Apache Solr on port 8983.PANTHEON_INDEX_HOST
and PANTHEON_INDEX_PORT
environment variables.schema.xml
to the Solr configuration directory (a path similar to solr/conf/schema.xml
).Alternatively, there are a couple of community-maintained Docker containers you may be able to use: kalabox/pantheon-solr, kshaner/solr.
In a local development environment, you can point Solr Power to a custom Solr instance by creating a MU plugin with:
<?php
/**
* Define Solr host IP, port, scheme and path
* Update these as necessary if your configuration differs
*/
putenv( 'PANTHEON_INDEX_HOST=192.168.50.4' );
putenv( 'PANTHEON_INDEX_PORT=8983' );
add_filter( 'solr_scheme', function(){ return 'http'; });
define( 'SOLR_PATH', '/solr/wordpress/' );
** Note for Lando users **
If you are using lando for development, the MU plugin is not needed. Lando auto configures everything for your local environment to connect to the docker index it maintains and if you overrite the ENV variables it will mess with that configuration.
This plugin is under active development on GitHub:
https://github.com/pantheon-systems/solr-power
Please feel free to file issues there. Pull requests are also welcome! See CONTRIBUTING.md for information on contributing.
For further documentation, such as available filters and working with the SolrPower_Api
class directly, please see the project wiki:
https://github.com/pantheon-systems/solr-power/wiki
This plugin has WP-CLI support.
All Solr Power related commands are grouped into the wp solr
command, see an example:
$ wp solr
usage: wp solr check-server-settings
or: wp solr delete [<id>...] [--all]
or: wp solr index [--batch=<batch>] [--batch_size=<size>] [--post_type=<post-type>]
or: wp solr info [--field=<field>] [--format=<format>]
or: wp solr optimize-index
or: wp solr repost-schema
or: wp solr stats [--field=<field>] [--format=<format>]
See 'wp help solr <command>' for more information on a specific command.
You can see more details about the commands using wp help solr
:
**NAME**
wp solr
**DESCRIPTION**
Perform a variety of actions against your Solr instance.
**SYNOPSIS**
wp solr <command>
**SUBCOMMANDS**
check-server-settings Check server settings.
delete Remove one or more posts from the index.
index Index all posts for a site.
info Report information about Solr Power configuration.
optimize-index Optimize the Solr index.
repost-schema Repost schema.xml to Solr.
stats Report stats about indexed content.
Use Solr in a custom WP_Query instead of querying a database. Add 'solr_integrate' => true
to the query arguments.
NOTE: Currently, only basic queries, tax_query, meta_query and date_query are supported. See examples/example.custom_WP_Query.php
for an example.
A meta_query can use the following compare operators:
'='
'!='
'>'
'>='
'<'
'<='
'LIKE'
'NOT LIKE'
'IN'
'NOT IN'
'BETWEEN'
'NOT BETWEEN'
'EXISTS'
'NOT EXISTS'
('REGEXP'
, 'NOT REGEXP'
, and 'RLIKE'
are not supported.)
To support searching by author name (e.g. where “Pantheon” would return posts authored by the “Pantheon” user), add the following to your custom schema.xml
:
<copyField source="post_author" dest="text"/>
The following guidance can be used to extend the Solr index and modify boosts beyond just this example.
To support math functions on dates, you must add a custom schema.xml
to Solr and reindex with the new schema.
Add the following to schema.xml
:
<!-- Add to <types> -->
<!-- See: https://lucene.apache.org/solr/6_2_0/solr-core/org/apache/solr/schema/TrieDateField.html -->
<fieldType name="tdate" class="solr.TrieDateField" omitNorms="true" precisionStep="6" positionIncrementGap="0"/>
<!-- Add to <fields> -->
<field name="post_date_iso" type="tdate" indexed="true" stored="true" required="true" />
Add the following to your functions.php
file.
<?php
/**
* Hooks into the document build process to add post date field in proper format.
*/
function my_solr_build_document( $doc, $post_info ) {
$post_time = strtotime( $post_info->post_date );
// Matches format required for TrieDateField
$doc->setField( 'post_date_iso', gmdate( 'c\Z', $post_time ) );
return $doc;
}
add_filter( 'solr_build_document', 'my_solr_build_document', 10, 2 );
/**
* Hooks into query processor, Dismax, to add publish date boost.
* See: https://www.metaltoad.com/blog/date-boosting-solr-drupal-search-results
*/
function my_solr_dismax_query( $dismax ) {
$dismax->setQueryParser( 'edismax' );
$dismax->setBoostQuery( 'recip(abs(ms(NOW/HOUR,post_date_iso),3.16e-11,1,1))' );
return $dismax;
}
add_filter( 'solr_dismax_query', 'my_solr_dismax_query' );
is_solr_query
FilterThe is_solr_query
filter controls whether Solr should be used for a specific query.
Parameters:
Return:
add_filter( 'is_solr_query', function( $enabled, $query ) {
if ( $query->is_category( 'news' ) ) {
return false;
}
return $enabled;
}, 10, 2 );
By default, custom schema is sourced from wp-content/uploads/solr-for-wordpress-on-pantheon/schema.xml
. This can be overridden with an absolute path using the the solr_power_customer_schema_file_path
filter.
Parameters:
Return:
add_filter('solr_power_customer_schema_file_path', function($custom_schema_file_path) {
return '/absolute/path/to/schema.xml';
});
post_date_iso
field.”solr_build_document
filter, you will get a similar error.post_date_iso
field is missing from the index, Solr will ignore this boost and return regular results.Once solr has sent the data to the solr server, solr must COMMIT the data to the index and adjust the index and relevancy ratings accordingly before that data can appear in search results.
By default, Solr Search for WordPress has auto-commit disabled. The index is committed when the uncommitted item is two minutes old, or the cron runs. By default, the cron runs on the Pantheon platform every hour.
When autocommit is enabled, Solr Search for WordPress commits data when it sends every post. When running on Pantheon, we recommend leaving autocommit disabled to aid overall site performance.
To enable autocommit, add the following to wp-config.php
or an mu-plugin.
define( 'SOLRPOWER_DISABLE_AUTOCOMMIT', false );
To force-commit data outside of a normal cron run, from the command line, you can run the command below or simply force a cron-run.
wp solr commit
Please report security bugs found in the Solr Power plugin’s source code through the Patchstack Vulnerability Disclosure Program. The Patchstack team will assist you with verification, CVE assignment, and notify the developers of this plugin.