0%

Redis-Redisearch

RediSearch 是一个高性能的全文搜索引擎,它可以作为一个 Redis Module(扩展模块)运行在 Redis 服务器上。

RediSearch

  • Redis 哈希中多个字段的全文索引
  • 无性能损失的增量索引
  • 文档排名(使用tf-idf,具有可选的用户提供的权重)
  • 场加权
  • 使用 AND、OR 和 NOT 运算符的复杂布尔查询
  • 前缀匹配、模糊匹配和精确短语查询
  • 支持双变音拼音匹配
  • 自动完成建议(带有模糊前缀建议)
  • 多种语言中基于词干的查询扩展(使用Snowball
  • 支持中文标记化和查询(使用Friso
  • 数字过滤器和范围
  • 使用Redis 地理空间索引进行地理空间搜索
  • 强大的聚合引擎
  • 支持所有 utf-8 编码文本
  • 检索完整文档、选定字段或仅检索文档 ID
  • 排序结果(例如,按创建日期)

官方文档

运行环境

  • docker
1
2
mkdir -p redisearch/data
docker run -p 6379:6379 -v $PWD/redisearch/data:/data -d redislabs/redisearch:latest
  • 检查安装
1
2
3
4
5
6
7
➜  docker-run redis-cli 
127.0.0.1:6379> MODULE LIST
1) 1) "name"
2) "search"
3) "ver"
4) (integer) 20015
127.0.0.1:6379> exit
  • 测试下
1
2
3
4
5
6
7
127.0.0.1:6379> FT.ADD idx docCn 1.0 LANGUAGE chinese FIELDS txt "Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。从盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。[8]"
OK
127.0.0.1:6379> FT.SEARCH idx "数据" LANGUAGE chinese HIGHLIGHT SUMMARIZE
1) (integer) 1
2) "docCn"
3) 1) "txt"
2) "<b>\xe6\x95\xb0\xe6\x8d\xae</b>\xe5... <b>\xe6\x95\xb0\xe6\x8d\xae</b>\xe8\xbf\x9b\xe8\xa1\x8c\xe5\x86\x99\xe6\x93\x8d\xe4\xbd\x9c\xe3\x80\x82\xe7\x94\xb1\xe4\xba\x8e\xe5\xae\x8c\xe5\x85\xa8\xe5\xae\x9e\xe7\x8e\xb0\xe4\xba\x86\xe5\x8f\x91\xe5\xb8\x83... <b>\xe6\x95\xb0\xe6\x8d\xae</b>\xe5\x86\x97\xe4\xbd\x99\xe5\xbe\x88\xe6\x9c\x89\xe5\xb8\xae\xe5\x8a\xa9\xe3\x80\x82[8... "
  • 对中文支持不够好

代码演示

  • Spring boot 2.6.0

  • spring-lettucemod 1.8.1

  • 注意这里需要排除commons-pool2

项目依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>com.redis</groupId>
<artifactId>spring-lettucemod</artifactId>
<version>1.8.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
  • 单元测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

@Autowired
StatefulRedisModulesConnection<String, String> connection;

@Autowired
RedisTemplate redisTemplate;

@Test
public void search() {
Set<String> keys = redisTemplate.keys("*");
keys.forEach(item -> {
redisTemplate.delete(item);
log.info(" keys - > {} ", item);
}
);
String searchIndexName = "doc-idx";
Gson gson = new GsonBuilder().setPrettyPrinting().create();
RediSearchCommands<String, String> commands = connection.sync();
RedisCommands redisCommands = connection.sync();
List<String> list = commands.list();
for (String s : list) {
commands.dropindex(s);
}
CreateOptions<String, String> options = CreateOptions.<String, String>builder()
.on(CreateOptions.DataType.HASH)
.languageField(Language.CHINESE.getId())
.prefix(String.format("%s:", DocumentDto.class.getName()))
.build();
log.info("{}",gson.toJson(options));
Field title = Field.text("title").build();
Field subtitle = Field.text("subtitle").build();
Field description = Field.text("description").build();
Field author = Field.text("authors").build();
commands.create(
searchIndexName, //
options, //
title,
subtitle,
description, //
author
);
String key = String.format("%s:%s", DocumentDto.class.getName(), "1");
Map<String,String> map = new HashMap<>();
map.put("title","标题");
map.put("subtitle","副标题");
map.put("description","描述");
map.put("authors","作者");
Boolean result = redisHash.hMSet(key,map);
log.info(" \n key {} \n data {} result {} ", key,gson.toJson(map),result);
map = new HashMap<>();
map.put("title","标题1");
map.put("subtitle","副标题2");
map.put("description","描述3");
map.put("authors","作者4");
key = String.format("%s:%s", DocumentDto.class.getName(), "2");
result = redisHash.hMSet(key,map);
log.info(" \n key {} \n data {} result {} ", key,gson.toJson(map),result);
List<String> fields = new ArrayList<>();
fields.add("title");
fields.add("subtitle");
fields.add("description");
fields.add("authors");
SearchOptions.Summarize summarize = new SearchOptions.Summarize();
summarize.setFields(fields);
SearchOptions searchOptions = SearchOptions.builder()
.language(Language.CHINESE)
.limit(SearchOptions.Limit.of(0,10))
.summarize(summarize)
.build();
log.info(" searchOptions {}",gson.toJson(searchOptions));
SearchResults<String, String> results = commands.search(searchIndexName, "标", searchOptions);
log.info("{}", gson.toJson(results));
}
  • Console
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
key cn.z201.learning.redis.operating.DocumentDto:1 
data {
"subtitle": "副标题",
"description": "描述",
"title": "标题",
"authors": "作者"
} result true

key cn.z201.learning.redis.operating.DocumentDto:2
data {
"subtitle": "副标题2",
"description": "描述3",
"title": "标题1",
"authors": "作者4"
} result true

// 结果
[
{
"title": "标题1... ",
"subtitle": "�",
"description": "�",
"authors": "�"
},
{
"title": "标题... ",
"subtitle": "�",
"description": "�",
"authors": "�"
}
]
  • 结果很不友好,对中文支持一般般。