Skip to content

Commit

Permalink
Fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbern committed Apr 14, 2023
1 parent 72fda01 commit 7bc4641
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions ann_benchmarks/algorithms/redisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ def __init__(self, metric, M):

def fit(self, X):
# Start Redis in the background
subprocess.run("redis-server --daemonize yes --loadmodule /usr/lib/redis/modules/redisearch.so", shell=True, check=True, stdout=sys.stdout, stderr=sys.stderr)
cmd = "redis-server --daemonize yes --loadmodule /usr/lib/redis/modules/redisearch.so"
print("Starting Redis:", cmd)
subprocess.run(cmd, shell=True, check=True, stdout=sys.stdout, stderr=sys.stderr)

# Sleep a bit to make sure the server is running
print("Sleeping 3s to wait for Redis server to start up...")
time.sleep(3)

# Connect to Redis
print("Connecting to Redis...")
self.redis = Redis(host="localhost", port=6379, decode_responses=False)

# Create index
args = [
"FT.CREATE",
"ann_benchmarks",
"SCHEMA",
"vector",
"VECTOR",
"HNSW",
Expand All @@ -42,6 +47,7 @@ def fit(self, X):
"EF_CONSTRUCTION",
self.ef_construction
]
print("Running Redis command:", args)
self.redis.execute_command(*args, target_nodes='random')

# Insert vectors
Expand All @@ -63,7 +69,7 @@ def query(self, v, n):
q = [
"FT.SEARCH",
"ann_benchmarks",
f"*=>[KNN {n} @vector $BLOB EF_RUNTIME {self.ef}]"
f"*=>[KNN {n} @vector $BLOB EF_RUNTIME {self.ef}]",
"NOCONTENT",
"SORTBY",
"__vector_score",
Expand All @@ -80,4 +86,4 @@ def query(self, v, n):
return [int(doc) for doc in self.redis.execute_command(*q, target_nodes='random')[1:]]

def __str__(self):
return f"Redisearch(M={self.m}, ef={self.ef})"
return f"Redisearch(M={self.M}, ef={self.ef})"

0 comments on commit 7bc4641

Please sign in to comment.