Conduct a Vector Similarity Search

This page will show you how to conduct a similarity search in Milvus.

Parameters marked with * are specific to Python SDK, and those marked with ** are specific to Node.js SDK.

  1. Create search parameters:
>>> search_params = {"metric_type": "L2", "params": {"nprobe": 10}}
const searchParams = {
  anns_field: "example_field",
  topk: "4",
  metric_type: "L2",
  params: JSON.stringify({ nprobe: 10 }),
};
Detailed Description
Parameter Description Note
metric_type Metrics used to measure similarity of vectors Find more options in Simlarity Metrics.
Mandatory
index_type Type of index used to accelerate the vector search Find more options in Index Selection.
Mandatory
params Search parameter(s) specific to the index Find more parameter details of different indexes in Index Selection.
Mandatory
anns_field** Name of the field to search on Mandatory
topk** Number of the most similar results to return Mandatory
  1. Load the collection to memory before conducting a vector similarity search:
>>> collection.load()
await milvusClient.collectionManager.loadCollection({
  collection_name: COLLECTION_NAME,
});
Detailed Description
Parameter Description Note
collection_name** Name of the collection to load Mandatory
  1. Search with newly created random vectors:

Milvus returns the IDs of the most similar vectors and their distances.

>>> results = collection.search(vectors[:5], field_name, param=search_params, limit=10, expr=None)
>>> results[0].ids
[424363819726212428, 424363819726212436, ...]
>>> results[0].distances
[0.0, 1.0862197875976562, 1.1029295921325684, ...]
await milvusClient.dataManager.search({
  collection_name: COLLECTION_NAME,
  // partition_names: [],
  expr: "",
  vectors: [[1, 2, 3, 4, 5, 6, 7, 8]],
  search_params: searchParams,
  vector_type: 100, // Float vector -> 100
});
Detailed Description
Parameter Description Note
collection_name** Name of the collection to search Mandatory
vectors Vectors to search with. Lehgth of the data represents the number of query nq. Mandatory
anns_field Name of the field to search on Mandatory
params* Search parameter(s) specific to the index Find more parameter details of different indexes in Index Selection.
Mandatory
limit* Number of the most similar results to return Mandatory
expr Boolean expression used to filter attribute Find more expression details in Predicate Expressions.
Optional
partition_names Name of the partition to search on Optional
output_fields Name of the field to return (vector field not support in current release) Optional
timeout Timeout (in seconds) to allow for RPC. Clients wait until server responds or error occurs when it is set to None. Optional
vector_type** Pre-check of binary/float vectors. 100 for binary vectors and 101 for float vectors. Mandatory

To search in a specific partition or field, specify the name of the partition and field.

>>> collection.search(vectors[:5], field_name, param=search_params, limit=10, expr=None, partition_names=[partition_name])
await milvusClient.dataManager.search({
  collection_name: COLLECTION_NAME,
  partition_names: [partition_name],
  expr: "",
  vectors: [[1, 2, 3, 4, 5, 6, 7, 8]],
  search_params: searchParams,
  vector_type: 100, // Float vector -> 100
});
  1. Release the collections loaded in Milvus to reduce memory consumption when the search is completed:
>>> collection.release()
await milvusClient.collectionManager.releaseCollection({  collection_name: COLLECTION_NAME,});
Detailed Description
Parameter Description Note
collection_name** Name of the collection to release Mandatory
Is this page helpful?
Scored Successfully!