RediSearch
Redlite implements RediSearch-compatible full-text search using SQLite’s FTS5 (Full-Text Search) extension.
Commands
Section titled “Commands”| Command | Syntax | Description |
|---|---|---|
| FT.CREATE | FT.CREATE index [ON HASH|JSON] SCHEMA field [TEXT|TAG|NUMERIC] ... | Create search index |
| FT.SEARCH | FT.SEARCH index query [LIMIT offset count] | Search index |
| FT.INFO | FT.INFO index | Get index information |
| FT.ALTER | FT.ALTER index SCHEMA ADD field [TEXT|TAG|NUMERIC] | Add field to index |
| FT.DROPINDEX | FT.DROPINDEX index [DD] | Delete index |
| FT.EXPLAIN | FT.EXPLAIN index query | Explain query execution |
| FT.PROFILE | FT.PROFILE index SEARCH query | Profile query performance |
| FT.AGGREGATE | FT.AGGREGATE index query ... | Aggregate search results |
Creating Indexes
Section titled “Creating Indexes”Basic Index
Section titled “Basic Index”# Create index on hash keys matching pattern "doc:*"127.0.0.1:6379> FT.CREATE idx:docs ON HASH PREFIX 1 doc: SCHEMA title TEXT body TEXTOKIndex with Multiple Field Types
Section titled “Index with Multiple Field Types”127.0.0.1:6379> FT.CREATE idx:products ON HASH PREFIX 1 product: SCHEMA name TEXT WEIGHT 5.0 description TEXT category TAG price NUMERIC SORTABLEOKIndexing Documents
Section titled “Indexing Documents”# Add documents as hashes127.0.0.1:6379> HSET doc:1 title "Introduction to Databases" body "SQLite is a lightweight database..."(integer) 2
127.0.0.1:6379> HSET doc:2 title "Redis Guide" body "Redis is an in-memory data store..."(integer) 2
127.0.0.1:6379> HSET doc:3 title "Database Comparison" body "Comparing SQLite, Redis, and PostgreSQL..."(integer) 2Searching
Section titled “Searching”Basic Search
Section titled “Basic Search”# Search for "database"127.0.0.1:6379> FT.SEARCH idx:docs "database"1) (integer) 22) "doc:1"3) 1) "title" 2) "Introduction to Databases" 3) "body" 4) "SQLite is a lightweight database..."4) "doc:3"5) 1) "title" 2) "Database Comparison" 3) "body" 4) "Comparing SQLite, Redis, and PostgreSQL..."Field-Specific Search
Section titled “Field-Specific Search”# Search in title field only127.0.0.1:6379> FT.SEARCH idx:docs "@title:redis"1) (integer) 12) "doc:2"3) 1) "title" 2) "Redis Guide" 3) "body" 4) "Redis is an in-memory data store..."Boolean Queries
Section titled “Boolean Queries”# AND query127.0.0.1:6379> FT.SEARCH idx:docs "database redis"1) (integer) 12) "doc:3"
# OR query127.0.0.1:6379> FT.SEARCH idx:docs "sqlite | redis"1) (integer) 3
# NOT query127.0.0.1:6379> FT.SEARCH idx:docs "database -redis"1) (integer) 12) "doc:1"Phrase Search
Section titled “Phrase Search”# Exact phrase127.0.0.1:6379> FT.SEARCH idx:docs "\"in-memory data store\""1) (integer) 12) "doc:2"Prefix Search
Section titled “Prefix Search”# Prefix matching127.0.0.1:6379> FT.SEARCH idx:docs "data*"1) (integer) 2Pagination
Section titled “Pagination”# Get results 10-20127.0.0.1:6379> FT.SEARCH idx:docs "database" LIMIT 10 10TAG Fields
Section titled “TAG Fields”# Create index with tag field127.0.0.1:6379> FT.CREATE idx:articles ON HASH PREFIX 1 article: SCHEMA title TEXT tags TAGOK
# Add documents with tags127.0.0.1:6379> HSET article:1 title "Python Tutorial" tags "programming,python,tutorial"(integer) 2
# Search by tag127.0.0.1:6379> FT.SEARCH idx:articles "@tags:{python}"1) (integer) 12) "article:1"
# Multiple tags (OR)127.0.0.1:6379> FT.SEARCH idx:articles "@tags:{python | javascript}"
# Multiple tags (AND)127.0.0.1:6379> FT.SEARCH idx:articles "@tags:{python} @tags:{tutorial}"NUMERIC Fields
Section titled “NUMERIC Fields”# Create index with numeric field127.0.0.1:6379> FT.CREATE idx:products ON HASH PREFIX 1 product: SCHEMA name TEXT price NUMERICOK
# Add products127.0.0.1:6379> HSET product:1 name "Laptop" price 999.99(integer) 2127.0.0.1:6379> HSET product:2 name "Mouse" price 29.99(integer) 2
# Numeric range query127.0.0.1:6379> FT.SEARCH idx:products "@price:[0 100]"1) (integer) 12) "product:2"
# Greater than127.0.0.1:6379> FT.SEARCH idx:products "@price:[500 +inf]"1) (integer) 12) "product:1"Index Management
Section titled “Index Management”Get Index Info
Section titled “Get Index Info”127.0.0.1:6379> FT.INFO idx:docs 1) "index_name" 2) "idx:docs" 3) "index_definition" 4) 1) "key_type" 2) "HASH" 3) "prefixes" 4) 1) "doc:" 5) "attributes" 6) 1) 1) "identifier" 2) "title" 3) "type" 4) "TEXT" 2) 1) "identifier" 2) "body" 3) "type" 4) "TEXT" 7) "num_docs" 8) "3"Alter Index
Section titled “Alter Index”# Add new field to existing index127.0.0.1:6379> FT.ALTER idx:docs SCHEMA ADD author TEXTOKDrop Index
Section titled “Drop Index”# Drop index only (keep documents)127.0.0.1:6379> FT.DROPINDEX idx:docsOK
# Drop index and delete documents127.0.0.1:6379> FT.DROPINDEX idx:docs DDOKAggregation
Section titled “Aggregation”# Count documents by category127.0.0.1:6379> FT.AGGREGATE idx:products "*" GROUPBY 1 @category REDUCE COUNT 0 AS countQuery Explanation
Section titled “Query Explanation”# Explain query execution plan127.0.0.1:6379> FT.EXPLAIN idx:docs "database redis"1) "AND("2) " TERM(database)"3) " TERM(redis)"4) ")"Performance Profiling
Section titled “Performance Profiling”# Profile search query127.0.0.1:6379> FT.PROFILE idx:docs SEARCH QUERY "database"1) 1) "Parsing time" 2) "0.05ms"2) 1) "Search time" 2) "0.15ms"Library Mode (Rust)
Section titled “Library Mode (Rust)”use redlite::Db;
let db = Db::open("mydata.db")?;
// Create indexdb.ft_create("idx:docs", "HASH", &["doc:"], &[ ("title", "TEXT"), ("body", "TEXT"),])?;
// Add documentsdb.hset("doc:1", &[("title", "Database Guide"), ("body", "...")])?;
// Searchlet results = db.ft_search("idx:docs", "database", None, None)?;for (key, fields) in results { println!("{}: {:?}", key, fields);}
// Drop indexdb.ft_dropindex("idx:docs", false)?;Use Cases
Section titled “Use Cases”Documentation Search
Section titled “Documentation Search”FT.CREATE idx:docs ON HASH PREFIX 1 doc: SCHEMA title TEXT WEIGHT 5.0 content TEXT tags TAGFT.SEARCH idx:docs "@tags:{api} @title:authentication"E-commerce Product Search
Section titled “E-commerce Product Search”FT.CREATE idx:products ON HASH PREFIX 1 product: SCHEMA name TEXT description TEXT category TAG price NUMERICFT.SEARCH idx:products "@category:{electronics} @price:[100 500]"Blog/CMS Search
Section titled “Blog/CMS Search”FT.CREATE idx:posts ON HASH PREFIX 1 post: SCHEMA title TEXT author TEXT content TEXT published NUMERICFT.SEARCH idx:posts "@author:john @published:[1704067200 +inf]"Implementation
Section titled “Implementation”- Backend: SQLite FTS5 (Full-Text Search) extension
- Tokenization: Unicode61 tokenizer with case-folding
- Ranking: BM25 ranking algorithm
- Storage: Inverted index in SQLite tables
- Stemming: Porter stemmer support
- Languages: Unicode support for all languages