Databases 9 min read

Mastering RedisSearch: Build Fast Full-Text Search on Redis with PHP

This guide introduces RedisSearch, walks through installation, explains its rich feature set, details core concepts like data models and indexing, shows command‑line operations for creating indexes and querying, and provides a complete PHP example for integrating RedisSearch into applications.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Mastering RedisSearch: Build Fast Full-Text Search on Redis with PHP

Introduction

RedisSearch is a Redis module that adds full‑text search, indexing and aggregation capabilities. It leverages Redis’s high‑performance in‑memory engine to provide real‑time search, autocomplete, faceting, sorting and more, making it valuable for micro‑service architectures.

Installation

Install the module as described in the Redis Stack guide and verify the installation with redis-cli module list. The screenshot shows both modules loaded successfully.

Key Features

Multi‑field joint search

High‑performance incremental indexing

Pre‑defined sortable fields

Complex boolean queries

Pipeline‑based query clauses

Prefix search

Field weight support

Autocomplete suggestions

Exact phrase search

Stemming‑based query expansion

Custom scoring functions

Field‑specific search limits

Numeric filters and ranges

Geospatial filtering using Redis geo commands

Unicode support (UTF‑8)

Document‑only or ID‑only retrieval

Document deletion, updates and index garbage collection

Partial and conditional updates

Spell correction

Highlighting

Aggregation analysis

Stop‑word and synonym configuration

Vector storage and KNN retrieval

Core Concepts

Data Model and Index

RedisSearch indexes JSON documents. Define a schema that maps field names to types, for example:

{
  "title": "string",
  "author": "string",
  "price": "float",
  "stock": "float"
}

When creating the index, RedisSearch builds an inverted index for each field based on the declared type.

Documents and Attributes

A document is a JSON object that conforms to the data model. Example document:

{
  "title": "Open‑Source Tech Stack RedisSearch Tutorial",
  "author": "Tinywan",
  "price": 9.99,
  "stock": 2024,
  "description": "RedisSearch is a Redis‑based search engine module offering full‑text search, indexing and aggregation."
}

Each key (title, author, price, stock, description) has a type (text or numeric) that determines how it is indexed.

Index and Search Mechanics

The index is an inverted table that maps terms to document IDs, enabling fast lookup. A search query generates a forward index of matching IDs, which is merged with the inverted index to compute scores and return results.

Command‑Line Operations

Create an Index

FT.CREATE TinywanIdx ON HASH PREFIX 1 doc: SCHEMA title TEXT WEIGHT 5.0 body TEXT url TEXT

This creates an index for hash keys with the prefix doc:. The fields and their weights are defined in the schema.

Add Data

HSET doc:1 title "Hello Tinywan" body "search and query" url "https://redis.io/docs/interact/search-and-query"

Any hash that matches the prefix is automatically added to the index.

Search the Index

FT.SEARCH TinywanIdx "Hello Tinywan" LIMIT 0 10

Adding multiple records and querying yields similar results.

PHP Integration

Redisearch‑PHP is a client library that enables PHP applications to use RedisSearch.

GitHub: https://github.com/ethanhann/redisearch-php

Install the Library

composer require ethanhann/redisearch-php

Create a Redis Client

$redis = (new \Ehann\RedisRaw\PhpRedisAdapter())->connect('192.168.13.168', 6379);

Define the Index

$bookIndex = new \Ehann\RediSearch\Index($redis);
$bookIndex->addTextField('title')
          ->addTextField('author')
          ->addNumericField('price')
          ->addNumericField('stock')
          ->addTextField('description')
          ->create();

Add Documents

$bookIndex->add([
    new \Ehann\RediSearch\Fields\TextField('title', 'Open‑Source Tech Stack RedisSearch Tutorial'),
    new \Ehann\RediSearch\Fields\TextField('author', 'Tinywan'),
    new \Ehann\RediSearch\Fields\NumericField('price', 9.99),
    new \Ehann\RediSearch\Fields\NumericField('stock', 2024),
    new \Ehann\RediSearch\Fields\TextField('description', 'RedisSearch is a Redis‑based search engine module offering full‑text search, indexing and aggregation.')
]);

Search from PHP

$result = $bookIndex->search('Open‑Source Tech Stack RedisSearch Tutorial');
var_dump($result);
Result can also be viewed with RedisInsight.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redisPHPdatabasesFull‑Text SearchRedisSearch
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.