Skip to content

RediSearch

Redlite implements RediSearch-compatible full-text search using SQLite’s FTS5 (Full-Text Search) extension.

CommandSyntaxDescription
FT.CREATEFT.CREATE index [ON HASH|JSON] SCHEMA field [TEXT|TAG|NUMERIC] ...Create search index
FT.SEARCHFT.SEARCH index query [LIMIT offset count]Search index
FT.INFOFT.INFO indexGet index information
FT.ALTERFT.ALTER index SCHEMA ADD field [TEXT|TAG|NUMERIC]Add field to index
FT.DROPINDEXFT.DROPINDEX index [DD]Delete index
FT.EXPLAINFT.EXPLAIN index queryExplain query execution
FT.PROFILEFT.PROFILE index SEARCH queryProfile query performance
FT.AGGREGATEFT.AGGREGATE index query ...Aggregate search results
Terminal window
# 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 TEXT
OK
Terminal window
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 SORTABLE
OK
Terminal window
# Add documents as hashes
127.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) 2
Terminal window
# Search for "database"
127.0.0.1:6379> FT.SEARCH idx:docs "database"
1) (integer) 2
2) "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..."
Terminal window
# Search in title field only
127.0.0.1:6379> FT.SEARCH idx:docs "@title:redis"
1) (integer) 1
2) "doc:2"
3) 1) "title"
2) "Redis Guide"
3) "body"
4) "Redis is an in-memory data store..."
Terminal window
# AND query
127.0.0.1:6379> FT.SEARCH idx:docs "database redis"
1) (integer) 1
2) "doc:3"
# OR query
127.0.0.1:6379> FT.SEARCH idx:docs "sqlite | redis"
1) (integer) 3
# NOT query
127.0.0.1:6379> FT.SEARCH idx:docs "database -redis"
1) (integer) 1
2) "doc:1"
Terminal window
# Exact phrase
127.0.0.1:6379> FT.SEARCH idx:docs "\"in-memory data store\""
1) (integer) 1
2) "doc:2"
Terminal window
# Prefix matching
127.0.0.1:6379> FT.SEARCH idx:docs "data*"
1) (integer) 2
Terminal window
# Get results 10-20
127.0.0.1:6379> FT.SEARCH idx:docs "database" LIMIT 10 10
Terminal window
# Create index with tag field
127.0.0.1:6379> FT.CREATE idx:articles ON HASH PREFIX 1 article: SCHEMA title TEXT tags TAG
OK
# Add documents with tags
127.0.0.1:6379> HSET article:1 title "Python Tutorial" tags "programming,python,tutorial"
(integer) 2
# Search by tag
127.0.0.1:6379> FT.SEARCH idx:articles "@tags:{python}"
1) (integer) 1
2) "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}"
Terminal window
# Create index with numeric field
127.0.0.1:6379> FT.CREATE idx:products ON HASH PREFIX 1 product: SCHEMA name TEXT price NUMERIC
OK
# Add products
127.0.0.1:6379> HSET product:1 name "Laptop" price 999.99
(integer) 2
127.0.0.1:6379> HSET product:2 name "Mouse" price 29.99
(integer) 2
# Numeric range query
127.0.0.1:6379> FT.SEARCH idx:products "@price:[0 100]"
1) (integer) 1
2) "product:2"
# Greater than
127.0.0.1:6379> FT.SEARCH idx:products "@price:[500 +inf]"
1) (integer) 1
2) "product:1"
Terminal window
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"
Terminal window
# Add new field to existing index
127.0.0.1:6379> FT.ALTER idx:docs SCHEMA ADD author TEXT
OK
Terminal window
# Drop index only (keep documents)
127.0.0.1:6379> FT.DROPINDEX idx:docs
OK
# Drop index and delete documents
127.0.0.1:6379> FT.DROPINDEX idx:docs DD
OK
Terminal window
# Count documents by category
127.0.0.1:6379> FT.AGGREGATE idx:products "*" GROUPBY 1 @category REDUCE COUNT 0 AS count
Terminal window
# Explain query execution plan
127.0.0.1:6379> FT.EXPLAIN idx:docs "database redis"
1) "AND("
2) " TERM(database)"
3) " TERM(redis)"
4) ")"
Terminal window
# Profile search query
127.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"
use redlite::Db;
let db = Db::open("mydata.db")?;
// Create index
db.ft_create("idx:docs", "HASH", &["doc:"], &[
("title", "TEXT"),
("body", "TEXT"),
])?;
// Add documents
db.hset("doc:1", &[("title", "Database Guide"), ("body", "...")])?;
// Search
let results = db.ft_search("idx:docs", "database", None, None)?;
for (key, fields) in results {
println!("{}: {:?}", key, fields);
}
// Drop index
db.ft_dropindex("idx:docs", false)?;
Terminal window
FT.CREATE idx:docs ON HASH PREFIX 1 doc: SCHEMA title TEXT WEIGHT 5.0 content TEXT tags TAG
FT.SEARCH idx:docs "@tags:{api} @title:authentication"
Terminal window
FT.CREATE idx:products ON HASH PREFIX 1 product: SCHEMA name TEXT description TEXT category TAG price NUMERIC
FT.SEARCH idx:products "@category:{electronics} @price:[100 500]"
Terminal window
FT.CREATE idx:posts ON HASH PREFIX 1 post: SCHEMA title TEXT author TEXT content TEXT published NUMERIC
FT.SEARCH idx:posts "@author:john @published:[1704067200 +inf]"
  • 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