Skip to content

Commit

Permalink
Basic redisearch
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbern committed Apr 14, 2023
1 parent 8f26c2e commit b89394b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
10 changes: 10 additions & 0 deletions algos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@ float:
args: [[100, 200, 400, 1000, 2000, 4000, 10000]]
query-args: [[1, 2, 4, 10, 20, 40, 100]]

redisearch:
docker-tag: ann-benchmarks-redisearch
module: ann_benchmarks.algorithms.redisearch
constructor: Redisearch
base-args: ["@metric"]
run-groups:
redisearch:
arg-groups: [[4, 8, 12, 16, 24, 36, 48, 64, 96]]
query-args: [[10, 20, 40, 80, 120, 200, 400, 600, 800]]

euclidean:
vamana(diskann):
docker-tag: ann-benchmarks-diskann
Expand Down
79 changes: 79 additions & 0 deletions ann_benchmarks/algorithms/redisearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import subprocess
import sys

from redis import Redis

from .base import BaseANN


class Redisearch(BaseANN):
def __init__(self, metric, M):
self.metric = metric
self.ef_construction = 500
self.M = M

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

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

# Create index
args = [
"FT.CREATE",
"ann_benchmarks",
"vector",
"VECTOR",
"HNSW",
"10",
"TYPE",
"FLOAT32",
"DIM",
X.shape[0],
"DISTANCE_METRIC",
{'angular': 'cosine', 'euclidean': 'l2'}[self.metric],
"M",
self.M,
"EF_CONSTRUCTION",
self.ef_construction
]
self.redis.execute_command(*args, target_nodes='random')

# Insert vectors
p = self.redis.pipeline(transaction=False)
count = 0
for i, v in enumerate(X):
p.execute_command('HSET', i, 'vector', v.tobytes())
count += 1
if count == 1000:
p.execute()
p.reset()
count = 0
p.execute()

def set_query_arguments(self, ef):
self.ef = ef

def query(self, v, n):
q = [
"FT.SEARCH",
"ann_benchmarks",
f"*=>[KNN {n} @vector $BLOB EF_RUNTIME {self.ef}]"
"NOCONTENT",
"SORTBY",
"__vector_score",
"LIMIT",
"0",
str(n),
"PARAMS",
"2",
"BLOB",
v.tobytes(),
"DIALECT",
"2"
]
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})"
16 changes: 16 additions & 0 deletions install/Dockerfile.redisearch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM redislabs/redisearch as redisearch
FROM ann-benchmarks

RUN apt-get update && apt-get install -y curl lsb-core
RUN curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
RUN echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list
RUN apt-get update
RUN apt-get install -y redis

COPY --from=redisearch /usr/lib/redis/modules/redisearch.so /usr/lib/redisearch.so
RUN ls -al /usr/lib

# Sanity check that redis-server can run
RUN redis-server --loadmodule /usr/lib/redisearch.so

RUN pip3 install redisearch redis

0 comments on commit b89394b

Please sign in to comment.