RediSearch vs Elasticsearch: Cheap Setup, Performance Benchmarks, Java Guide
RediSearch, a low‑memory Redis module, offers comparable full‑text search capabilities to Elasticsearch, with faster indexing and query performance on modest hardware; this article explains its features, compares benchmarks, shows Docker installation, and provides Java/Jedis code examples for creating, querying, and managing indexes.
RediSearch Overview
RediSearch is a Redis module that adds full‑text search, secondary indexing, and query capabilities to Redis. It uses compressed inverted indexes, resulting in low memory consumption and fast index construction. The latest version supports text tokenization, aggregation, stop‑words, synonyms, spell checking, sorting, tag queries, vector similarity, and Chinese tokenization.
Comparison with Elasticsearch
Hardware Requirements
Data Source
Configuration
Index Build Test
In the official index‑build benchmark, RediSearch completed the task in 221 seconds, beating Elasticsearch's 349 seconds—a 58% improvement.
Query Performance Test
After loading data into the index, a load‑generator with 32 clients issued two‑term search queries. RediSearch achieved 12.5K ops/sec, while Elasticsearch reached only 3.1K ops/sec—approximately four times faster. Latency was also slightly better (8 ms vs 10 ms).
RediSearch 2.0 further improves throughput and latency by about 2.4× compared with version 1.6.
Installation
For RediSearch 2.0+, the recommended deployment is the
redis-stack-serverDocker image.
<code>docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest</code>Set a password if needed:
<code>// Set login password
docker run -e REDIS_ARGS="--requirepass redis-stack" redis/redis-stack:latest</code>Verify the module is loaded via
redis-cli:
<code>redis-cli -h localhost
> MODULE list
...
1) "name"
2) "search"
3) "ver"
4) "20809"
...</code>Index Operations
Create Index (FT.CREATE)
<code>> FT.CREATE idx:goods ON hash PREFIX 1 "goods:" LANGUAGE chinese SCHEMA goodsName TEXT SORTABLE
"OK"
</code>FT.CREATE – creates the index.
idx:goods – index name.
ON hash – indexes hash‑type data.
PREFIX 1 "goods:" – target keys start with
goods:.
LANGUAGE chinese – enables Chinese tokenization.
SCHEMA goodsName TEXT SORTABLE – defines a searchable, sortable field.
Add source data:
<code>hset goods:1001 goodsName 小米手机
hset goods:1002 goodsName 华为手机
</code>Search Index (FT.SEARCH)
<code>> FT.SEARCH idx:goods1 "手机"
1) "2"
2) "goods:1001"
3) 1) "goodsName"
2) "\xe5\xb0\x8f\xe7\xb1\xb3\xe6\x89\x8b\xe6\x9c\xba"
4) "goods:1002"
5) 1) "goodsName"
2) "\xe5\x8d\x8e\xe4\xb8\xba\xe6\x89\x8b\xe6\x9c\xba"
</code>Get Index Info (FT.INFO)
<code>> FT.INFO idx:goods
1) "index_name"
2) "idx:goods1"
3) "index_options"
4) (empty list or set)
5) "index_definition"
6) 1) "key_type"
2) "HASH"
3) "prefixes"
4) 1) "goods:"
...</code>Delete Index (FT.DROPINDEX)
<code>> FT.DROPINDEX idx:goods1
"OK"
</code>To remove indexed data, delete the associated hash keys with
DEL.
Java Integration with Jedis
Use Jedis 4.0+ which includes RediSearch support.
Create Jedis Client Bean
<code>@Bean
public UnifiedJedis unifiedJedis(GenericObjectPoolConfig jedisPoolConfig) {
UnifiedJedis client;
if (StringUtils.isNotEmpty(password)) {
client = new JedisPooled(jedisPoolConfig, host, port, timeout, password, database);
} else {
client = new JedisPooled(jedisPoolConfig, host, port, timeout, null, database);
}
return client;
}
</code>Create Index in Java
<code>Schema schema = new Schema()
.addSortableTextField("goodsName", 1.0)
.addSortableTagField("tag", "|");
IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.HASH)
.setPrefixes("idx:goods")
.setLanguage("chinese");
client.ftCreate(idxName, IndexOptions.defaultOptions().setDefinition(rule), schema);
</code>Add Documents
<code>public boolean addGoodsIndex(String keyPrefix, Goods goods) {
Map<String, String> hash = MyBeanUtil.toMap(goods);
hash.put("_language", "chinese");
client.hset("idx:goods" + goods.getGoodsId(), hash);
return true;
}
</code>Search with Chinese Tokenization
<code>public SearchResult search(String goodsIdxName, SearchObjVO searchObjVO, Page<SearchPageGoodsVO> page) {
String keyword = searchObjVO.getKeyword();
String queryKey = String.format("@goodsName:(%s)", keyword);
Query q = new Query(queryKey);
if (StringUtils.isNotBlank(searchObjVO.getSidx())) {
q.setSortBy(searchObjVO.getSidx(), Constants.SORT_ASC.equals(searchObjVO.getOrder()));
}
q.setLanguage("chinese");
q.limit((int) page.offset(), (int) page.getSize());
return client.ftSearch(goodsIdxName, q);
}
</code>Conclusion
RediSearch provides a lightweight, fast full‑text search solution with low memory overhead and easy Docker deployment, making it attractive for small‑to‑medium projects. However, enterprise‑grade Redis clustering support is currently limited to the commercial Redis Enterprise edition, so large‑scale production use should be evaluated carefully.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.