Say I have two indexes with the following schema.
SCHEMA = [
TextField("id"),
VectorField("embedding", "FLAT", {"TYPE": "FLOAT32", "DIM": 768, "DISTANCE_METRIC": "COSINE"}),
]
I have two known ids from each index. Is it possible to retrieve the vector similarity score between those two objects from Redis? Here is some example python code of how the docs approximately describe querying.
query = ???
query_vector = redis.hget(key="embedding", name="products:23")
results = redis.ft("services").search(query, query_params={"vector": query_vector})
If you know what two documents you want to get the distance between, the best way I can think of for getting it would be
To achieve the second step, you can try having some field (tag or numeric for example) with a unique value for each document (like the doc name itself) and look for it before performing the KNN query.
You can also try using the INKEYS query parameter to limit the search to the second document only. From the documentation:
Finally, hybrid queries in redisearch are using heuristics to decide in which way to perform the query. If you’re sure the pre-filter passes a single doc, you can specify the policy for the query instead of using the heuristic to make sure it will use
AD-HOC BROUT FORCE
, which will take the docs that passed the filter and directly compare them to the query, instead of the general flat way (as you have a FLAT index in your example) of multiplying the query against the entire dataset. The heuristic probably choose this way automatically, but you can force the ad-hoc by adding theHYBRID_POLICY
parameter to the query:And one last note: you might want to consider, in the case when you are only looking for the distance between two vectors, simply
HGET
the two embeddings and perform the multiplication locally using bumpy or any other library.Hope that helps!