from redis import Redis
from redis.exceptions import ResponseError
def init_redis_index():
r = Redis(host="localhost", port=6379, decode_responses=False)
try:
r.execute_command(
"FT.CREATE", "doc_index",
"ON", "HASH",
"PREFIX", "1", "doc:",
"SCHEMA",
"title", "TEXT",
"url", "TEXT",
"content", "TEXT",
"embedding", "VECTOR", "FLAT", "6",
"TYPE", "FLOAT32",
"DIM", 384,
"DISTANCE_METRIC", "COSINE"
)
print("Redis index created")
except ResponseError as e:
# 이미 인덱스가 있으면 여기로 들어옴
if "Index already exists" in str(e):
print("Redis index already exists")
else:
raise