Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6291848

Browse files
committed
feat: add redis-sorted-set-auto-complete.md
利用 Redis Sorted Set 实现关键词自动补全功能
1 parent 50df946 commit 6291848

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
### [Sorted Sets 有序集合](/docs/redis-sorted-set-introduction.md)
4040
- [社交网站通常会有粉丝关注的功能,用 Redis 怎么实现?](/docs/redis-sorted-set-sns-follow.md)
4141
- [每日、每周、每月积分排行榜功能该怎么实现?](/docs/redis-sorted-set-ranking-or-trending-list.md)
42+
- [关键词搜索,如何用 Redis 实现自动补全?](/docs/redis-sorted-set-auto-complete.md)
4243

4344
### [Hash 哈希](/docs/redis-hash-introduction.md)
4445
- [登录会话,用 Redis 该怎么做?](/docs/redis-hash-session-token.md)

‎docs/redis-sorted-set-auto-complete.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 利用 Redis Sorted Set 实现自动补全
2+
3+
## 代码实现
4+
| [Python](#Python-版本) | [Java](#Java-版本) |
5+
|---|---|
6+
7+
### Python 版本
8+
```python
9+
from redis import Redis
10+
11+
12+
class AutoComplete:
13+
def __init__(self, client: Redis):
14+
self.client = client
15+
16+
def feed(self, keyword: str, weight):
17+
for i in range(1, len(keyword)):
18+
key = 'auto_complete:' + keyword[:i]
19+
self.client.zincrby(key, weight, keyword)
20+
21+
def hint(self, prefix: str, count=10):
22+
key = 'auto_complete:' + prefix
23+
return self.client.zrevrange(key, 0, count - 1)
24+
25+
26+
if __name__ == '__main__':
27+
redis = Redis(decode_responses=True)
28+
redis.flushall()
29+
ac = AutoComplete(redis)
30+
31+
ac.feed('张艺兴', 5000)
32+
ac.feed('张艺谋', 3000)
33+
ac.feed('张三', 500)
34+
35+
print(ac.hint('')) # ['张艺兴', '张艺谋', '张三']
36+
print(ac.hint('张艺')) # ['张艺兴', '张艺谋']
37+
```
38+
39+
### Java 版本
40+
```java
41+
import redis.clients.jedis.Jedis;
42+
43+
import java.util.Set;
44+
45+
public class AutoComplete {
46+
private Jedis client = new Jedis();
47+
48+
public AutoComplete() {
49+
50+
}
51+
52+
/**
53+
* 根据关键词设置分词权重
54+
*
55+
* @param keyword 关键词
56+
* @param weight 权重
57+
*/
58+
public void feed(String keyword, double weight) {
59+
int len = keyword.length();
60+
for (int i = 1; i < len; ++i) {
61+
String key = "auto_complete:" + keyword.substring(0, i);
62+
client.zincrby(key, weight, keyword);
63+
}
64+
}
65+
66+
/**
67+
* 根据前缀获取自动补全的结果
68+
*
69+
* @param prefix 前缀
70+
* @param count 数量
71+
* @return 结果集合
72+
*/
73+
public Set<String> hint(String prefix, long count) {
74+
String key = "auto_complete:" + prefix;
75+
return client.zrevrange(key, 0, count - 1);
76+
}
77+
78+
public static void main(String[] args) {
79+
AutoComplete ac = new AutoComplete();
80+
ac.feed("张艺兴", 5000);
81+
ac.feed("张艺谋", 3000);
82+
ac.feed("张三", 500);
83+
84+
System.out.println(ac.hint("", 10)); // [张艺兴, 张艺谋, 张三]
85+
System.out.println(ac.hint("张艺", 10)); // [张艺兴, 张艺谋]
86+
}
87+
}
88+
```

‎python/src/main/auto_complete.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from redis import Redis
2+
3+
4+
class AutoComplete:
5+
def __init__(self, client: Redis):
6+
self.client = client
7+
8+
def feed(self, keyword: str, weight):
9+
for i in range(1, len(keyword)):
10+
key = 'auto_complete:' + keyword[:i]
11+
self.client.zincrby(key, weight, keyword)
12+
13+
def hint(self, prefix: str, count=10):
14+
key = 'auto_complete:' + prefix
15+
return self.client.zrevrange(key, 0, count - 1)

‎python/src/test/test_auto_complete.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from unittest import TestCase
2+
3+
from redis import Redis
4+
5+
from main.auto_complete import AutoComplete
6+
7+
8+
def test_auto_complete():
9+
redis = Redis(decode_responses=True)
10+
redis.flushall()
11+
test_case = TestCase()
12+
13+
ac = AutoComplete(redis)
14+
ac.feed('张艺兴', 5000)
15+
ac.feed('张艺谋', 3000)
16+
ac.feed('张三', 500)
17+
18+
test_case.assertListEqual(['张艺兴', '张艺谋', '张三'], ac.hint('张'))
19+
test_case.assertListEqual(['张艺兴', '张艺谋'], ac.hint('张艺'))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /