-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathredis_autocomplete.py
105 lines (86 loc) · 2.75 KB
/
redis_autocomplete.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import redis
"""
redis 實做 autocomplete
"""
client = redis.Redis(host="localhost", port=6379, db=0)
"""
範例
help
hello
hell
hex
helloween
"""
auto_index_name = "autocomplete"
def init_set_data():
client.zadd(auto_index_name, {"hello": 0})
client.zadd(auto_index_name, {"hello*": 0})
client.zadd(auto_index_name, {"hell": 0})
client.zadd(auto_index_name, {"hell*": 0})
client.zadd(auto_index_name, {"hel": 0})
client.zadd(auto_index_name, {"he": 0})
client.zadd(auto_index_name, {"h": 0})
client.zadd(auto_index_name, {"hex": 0})
client.zadd(auto_index_name, {"hex*": 0})
client.zadd(auto_index_name, {"help": 0})
# client.zadd(auto_index_name, {"help*": 0})
client.zadd(auto_index_name, {"helloween": 0})
# client.zadd(auto_index_name, {"helloween*": 0})
client.zadd(auto_index_name, {"hellowee": 0})
client.zadd(auto_index_name, {"hellowe": 0})
client.zadd(auto_index_name, {"hellow": 0})
client.zadd(auto_index_name, {"hello ": 0})
client.zadd(auto_index_name, {"hello w": 0})
client.zadd(auto_index_name, {"hello wo": 0})
client.zadd(auto_index_name, {"hello wor": 0})
client.zadd(auto_index_name, {"hello worl": 0})
client.zadd(auto_index_name, {"hello world": 0})
# client.zadd(auto_index_name, {"hello world*": 0})
client.zadd(auto_index_name, {"hello everyone": 0})
# client.zadd(auto_index_name, {"hello everyone*": 0})
client.zadd(auto_index_name, {"hello everyon": 0})
client.zadd(auto_index_name, {"hello everyo": 0})
client.zadd(auto_index_name, {"hello every": 0})
client.zadd(auto_index_name, {"hello ever": 0})
client.zadd(auto_index_name, {"hello eve": 0})
client.zadd(auto_index_name, {"hello ev": 0})
client.zadd(auto_index_name, {"hello e": 0})
def get_index(content):
index = client.zrank(auto_index_name, content)
if index:
return index
raise Exception("key {} not in redis".format(content))
def ex1():
# 定位
index = get_index("hell")
print("index:", index)
# result
print(client.zrange(auto_index_name, index, index + 4, withscores=True))
def ex2():
# 定位
index = get_index("hello")
print("index:", index)
# result
print(client.zrange(auto_index_name, index, index + 4, withscores=True))
def ex3():
# 定位
index = get_index("hello w")
print("index:", index)
# result
print(client.zrange(auto_index_name, index, index + 4, withscores=True))
def ex4():
# 定位
index = get_index("hello e")
print("index:", index)
# result
print(client.zrange(auto_index_name, index, index + 4, withscores=True))
init_set_data()
try:
ex1()
# ex2()
# ex3()
# ex4()
except Exception as e:
print(e)
# clear redis data
# client.flushall()