Redis is a key value storage system that can be configured to speed up websites and those running WordPress and WooCommerce. It is typically used as the fast storage system for WordPress and WooCommerce external object cache (using for example this plugin). But Redis has grown and become more versatile! You can actually build modules for Redis and one of those is called RedisSearch.
Thanks to Foad Yousefi, we have a plugin that can leverage RediSearch and it appears to be a fork of ElasticPress which uses Elasticsearch (installation guide) as the search engine for WordPress and WooCommerce whenever possible. The Redis equivalent plugin of ElasticPress is called RediSearch just like the module.
This tutorial does assume you have already installed Redis server. We will be using Ubuntu 18.04 for this tutorial to build the RediSearch extension and add it to our Redis configuration. You will generally need root or sudo access to accomplish this.
You should test this on a staging server first!
Getting Started with RediSearch
First we need to build the RediSearch extension and afterwards we need to configure the RediSearch WordPress plugin.
Build RediSearch Module Extension
First let’s update our package repostiory list and install the building tools and git
apt update
apt install cmake build-essential git -y
Enter your temporary folder and clone the repo, enter the folder and generate the build file
cd /tmp
git clone https://github.com/RedisLabsModules/RediSearch.git
cd RediSearch/cmake
cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
You should see a lot of output like this
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test HAVE_W_INCOMPATIBLE_POINTER_TYPES
-- Performing Test HAVE_W_INCOMPATIBLE_POINTER_TYPES - Success
-- Performing Test HAVE_W_DISCARDS_QUALIFIERS
-- Performing Test HAVE_W_DISCARDS_QUALIFIERS - Failed
CMake Deprecation Warning at CMakeLists.txt:6 (CMAKE_POLICY):
The OLD behavior for policy CMP0026 will be removed from a future version
of CMake.
The cmake-policies(7) manual explains that the OLD behaviors of all
policies are deprecated and that a policy should be set to OLD only under
specific short-term circumstances. Projects should be ported to the NEW
behavior and not rely on setting a policy to OLD.
v1.4.2-303-g90fd9d41
CMake Warning (dev) at CMakeLists.txt:113 (GET_TARGET_PROPERTY):
Policy CMP0045 is not set: Error on non-existent target in
get_target_property. Run "cmake --help-policy CMP0045" for policy details.
Use the cmake_policy command to set the policy and suppress this warning.
get_target_property() called with non-existent target "example_extension".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/RediSearch/cmake
Now you can build the RediSearch module with this command
make
You will see a long list of output which finishes like this
Scanning dependencies of target redisearch
[ 91%] Building C object CMakeFiles/redisearch.dir/src/module-init/module-init.c.o
[ 92%] Linking C shared library redisearch.so
[ 92%] Built target redisearch
Scanning dependencies of target sizes
[ 92%] Building CXX object CMakeFiles/sizes.dir/src/c_utils/sizes.cpp.o
[ 93%] Linking CXX executable sizes
[ 93%] Built target sizes
Scanning dependencies of target redisearchS
[ 93%] Linking C static library libredisearchS.a
[ 93%] Built target redisearchS
Scanning dependencies of target test_vector
[ 94%] Building C object src/rmutil/CMakeFiles/test_vector.dir/test_vector.c.o
[ 95%] Linking C executable test_vector
[ 95%] Built target test_vector
Scanning dependencies of target test_args
[ 96%] Building C object src/rmutil/CMakeFiles/test_args.dir/test_args.c.o
[ 96%] Linking C executable test_args
[ 96%] Built target test_args
Scanning dependencies of target test_cmdparse
[ 97%] Building C object src/rmutil/CMakeFiles/test_cmdparse.dir/test_cmdparse.c.o
[ 97%] Linking C executable test_cmdparse
[ 97%] Built target test_cmdparse
Scanning dependencies of target test_heap
[ 98%] Building C object src/rmutil/CMakeFiles/test_heap.dir/test_heap.c.o
[ 98%] Linking C executable test_heap
[ 98%] Built target test_heap
Scanning dependencies of target test_periodic
[ 99%] Building C object src/rmutil/CMakeFiles/test_periodic.dir/test_periodic.c.o
[ 99%] Linking C executable test_periodic
[ 99%] Built target test_periodic
Scanning dependencies of target test_priority_queue
[100%] Building C object src/rmutil/CMakeFiles/test_priority_queue.dir/test_priority_queue.c.o
[100%] Linking C executable test_priority_queue
[100%] Built target test_priority_queue
Now there is an extension file called redisearch.so
in the cmake
folder
Let’s copy that somewhere that makes sense and make a Redis modules folder, then copy over the extension file
mkdir -p /etc/redis/modules
cp /tmp/RediSearch/cmake/redisearch.so /etc/redis/modules/redisearch.so
Now reference this module in the redis.conf
file by adding this line
loadmodule /etc/redis/modules/redisearch.so
To do so you can use nano or your favorite Linux text editor
sudo nano /etc/redis/redis.conf
I added it at the bottom here, you may want to adjust how much RAM is available to Redis as well in the future.
# create a unix domain socket to listen on
#unixsocket /var/run/redis/redis.sock
# set permissions for the socket
#unixsocketperm 775
# requirepass passwordtouse
bind 127.0.0.1
daemonize yes
stop-writes-on-bgsave-error no
rdbcompression yes
# maximum memory allowed for redis
maxmemory 500M
# how redis will evict old objects - least recently used
maxmemory-policy allkeys-lru
loadmodule /etc/redis/modules/redisearch.so
Reload Redis server to activate the new configuration with RediSearch module enabled.
sudo service redis-server restart
Now it is time to move on to the application side of things and configure the WordPress plugin to work with RediSearch.
Configure the RediSearch WordPress Plugin
Time to install the RediSearch WordPress plugin. I prefer to do this via WP-CLI
wp plugin install redisearch --activate
Now we need to add the Redis server settings in the RediSearch plugin.
In the wp-admin sidebar find Redisearch and then go choose Redis server
For the Redis server set 127.0.0.1 or your address in redis.conf
.
For the Redis port set 6379 or whatever is set in your redis.conf
.
Give the index a name under Redisearch index name and click Save
Now navigate to Redisearch > Indexing options
Choose your options here, I recommend checking Write redis data to the disk so that you do not have to re-index if Redis restarts for one reason or another.
Remember to click Save changes.
Now you can start the indexing process by going to Redisearch > Redisearch
Click the icon on the left to begin the indexing from MySQL to Redis.
That should take care of it!
Verify RediSearch Index Data
If you want to verify that your WordPress or WooCommerce data was indexed using RediSearch you can log in to the Redis server via the Redis cli.
redis-cli
You should then see this prompt which shows you are in the Redis-CLI administration prompt.
127.0.0.1:6379>
Then you can type INFO keyspace
at the 127.0.0.1:6379>
prompt and select the database that shows up (here 0
).
INFO keyspace
# Keyspace
db0:keys=4149,expires=0,avg_ttl=0
Selecting the Redis database in the CLI is this simple
SELECT 0
If the database exists you will get this message showing success
OK
Then you can list all of the keys for that index
KEYS *
When you are done you can exit the redis-cli
exit
I hope you had fun with this 🙂
Sources
RediSearch Quick Start
Redis Modules Intro
Redis Get All Keys
Redis SELECT Command
List all Redis Databases