Skip to main content

Similarity search

Return the closest_n entries most similar to a query vector, ranked by a similarity metric. This is the core vector-search operation.

Every operation works on both Vector DB stores (you supply raw vectors) and AI stores (you supply text or images and Ahnlich AI generates the embeddings for you). Use the Vector DB / AI switch in the sample below.

Parameters

ParameterTypeDescription
storestringName of the store to search.
search_inputvectorThe query vector (StoreKey). Must match the store's dimension.
closest_nint (> 0)How many results to return.
algorithmenumSimilarity metric: CosineSimilarity, EuclideanDistance, or DotProductSimilarity.
conditionpredicate · optionalRestrict which vectors are considered. None searches all. See Predicates.
schemastring · optionalSchema to target. Defaults to public.

All requests accept an optional schema field. When omitted, the server uses the public schema.

Behavior

  • The server compares the query vector against stored vectors using the chosen metric.
  • An optional condition narrows candidates by metadata before ranking.
  • Results are ordered from most to least similar.

Sample query

Python
import asyncio
from grpclib.client import Channel
from ahnlich_client_py.grpc.services.db_service import DbServiceStub
from ahnlich_client_py.grpc.db import query as db_query
from ahnlich_client_py.grpc import keyval, predicates
from ahnlich_client_py.grpc.algorithm.algorithms import Algorithm


async def get_simn():
async with Channel(host="127.0.0.1", port=1369) as channel:
client = DbServiceStub(channel)


search_key = keyval.StoreKey(key=[1.0, 2.0, 3.0, 4.0])


response = await client.get_sim_n(
db_query.GetSimN(
store="my_store",
schema="analytics",
search_input=search_key,
closest_n=3,
algorithm=Algorithm.CosineSimilarity,
condition=None # Optional: filter results using predicates
)
)

print(response.entries) # [(key, value, score), ...]


if __name__ == "__main__":
asyncio.run(get_simn())

Response

A ranked list of entries, each with its key (vector), value (metadata), and similarity score.