Introduction to RediSearch
Redisearch is a powerful search engine library built on top of Redis. It enables advanced search functionality, full-text search, filtering, and aggregation within your Redis database. In this guide, we’ll explore Redisearch’s capabilities and dive into some of its most useful APIs with code snippets to get you started.
Getting Started with RediSearch
First, ensure that you have the RediSearch module installed on your Redis server. You can check the installation guide here.
Creating an Index
Indexes are essential to leverage RediSearch’s capabilities. Let’s create an index with fields for title, description, and tags.
FT.CREATE myIndex ON HASH PREFIX 1 "doc:" SCHEMA title TEXT description TEXT tags TAG
Adding Documents to Index
Once you have an index, you can start adding documents to it. Here’s how you can add a document using the HMSET command:
HMSET doc:1 title "Redisearch Beginner's Guide" description "A guide to getting started with RediSearch." tags "redis, search"
FT.ADD myIndex doc:1 1.0 FIELDS title "Redisearch Beginner's Guide" description "A guide to getting started with RediSearch." tags "redis, search"
Searching Documents
RediSearch allows you to perform full-text searches on your indexed data. Here’s an example of how to search for documents containing the word “guide”:
FT.SEARCH myIndex "guide"
Filtering Results
You can also filter your search results based on specific fields. This example demonstrates how to filter results to include only documents with the tag “redis”:
FT.SEARCH myIndex "@tags:{redis}"
Sorting Results
Sorting search results by a specific field can be very helpful. Here’s how you can sort the results by title in ascending order:
FT.SEARCH myIndex "guide" SORTBY title ASC
Aggregations
Aggregations allow you to perform complex queries and computations on your data. Here’s an example of how to group and count documents by tags:
FT.AGGREGATE myIndex "*" GROUPBY 1 @tags REDUCE COUNT 0 AS tagCount
Using RediSearch in an Application
Let’s create a simple Python application demonstrating how to use the RediSearch APIs discussed above. We’ll use the `redis-py` and `redisearch` Python packages.
from redis import Redis
from redisearch import Client, TextField, TagField, IndexDefinition, Query
# Connect to Redis
redis_client = Redis(host='localhost', port=6379)
# Create an index
client = Client('myIndex', conn=redis_client)
client.create_index([
TextField("title"),
TextField("description"),
TagField("tags")
])
# Add documents to the index
redis_client.hset("doc:1", mapping={
"title": "Redisearch Beginner's Guide",
"description": "A guide to getting started with RediSearch.",
"tags": "redis,search"
})
client.add_document(
"doc:1",
title="Redisearch Beginner's Guide",
description="A guide to getting started with RediSearch.",
tags="redis,search"
)
# Search for documents
query = Query("guide")
results = client.search(query)
print(f"Found {results.total} document(s):")
for doc in results.docs:
print(doc.title, doc.description, doc.tags)
# Filter results by tag
query = Query("@tags:{redis}")
results = client.search(query)
print(f"Filtered results: Found {results.total} document(s):")
for doc in results.docs:
print(doc.title, doc.description, doc.tags)
Conclusion
RediSearch is a robust search engine that can significantly enhance the capabilities of your Redis database by adding full-text search, filtering, and aggregation functionalities. By leveraging RediSearch, you can build more powerful and efficient applications.
Hash: 87447e5786e8022d2852713336b16a145918c906928001e4c920f80b7d930a47