Vector Similarity Search
This topic introduces how to perform entity search in Milvus.
Vector similarity search in Milvus calculates the distance between the query vector and the vectors in the collection, and returns the most similar results. You can also perform mixed search by specifying a boolean expression to filter scalar fields or primary key fields.
The following example demonstrates how to perform vector similarity search on a dataset of 2000 rows, consisting of book IDs (primary key), word frequency (scalar field), and book descriptions (vector field). This simulates the scenario of searching for specific books based on vectorized descriptions. Milvus will return the most similar results based on your defined query vector and search parameters.
Load Collection
Before performing vector similarity search, Milvus executes all search and query operations in memory. Please load the collection into memory before performing vector similarity search.
err := milvusClient.LoadCollection(
context.Background(), // ctx
"book", // CollectionName
false // async
)
if err != nil {
log.Fatal("failed to load collection:", err.Error())
}
Prepare Search Parameters
Prepare parameters suitable for your search scenario. The following example defines using Euclidean distance to calculate distance and retrieving vectors from the ten nearest clusters built by the IVF_FLAT index.
sp, _ := entity.NewIndexIvfFlatSearchParam( // NewIndex*SearchParam func
10, // searchParam
)
opt := client.SearchQueryOptionFunc(func(option *client.SearchQueryOption) {
option.Limit = 3
option.Offset = 0
option.ConsistencyLevel = entity.ClStrong
option.IgnoreGrowing = false
})
Parameter | Description | Options |
---|---|---|
NewIndex*SearchParam func |
Function to create entity.SearchParam based on different index types. |
For Float Vectors: - NewIndexFlatSearchParam() (FLAT) - NewIndexIvfFlatSearchParam(nprobe int) (IVF_FLAT) - NewIndexIvfSQ8SearchParam(nprobe int) (IVF_SQ8) - NewIndexIvfPQSearchParam(nprobe int) (RNSG) - NewIndexHNSWSearchParam(ef int) (HNSW) For Binary Vectors: - NewIndexBinFlatSearchParam(nprobe int) (BIN_FLAT) - NewIndexBinIvfFlatSearchParam(nprobe int) (BIN_IVF_FLAT) |
sp |
Index-specific search parameters returned by the previous function. | See Vector Index for details. |
opt |
Options for ANN search. | - Limit : Specify the number of entities to return. - Offset : Specify the number of entities to skip during search. The sum of this parameter and Limit should be less than 16384. For example, if you want to query the 9th and 10th nearest neighbors of a vector, set Limit to 2 and Offset to 8 . - ConsistencyLevel : Specify the consistency level applied during search. - Ignore Growing : Indicates whether to ignore growing segments in similarity search. The default value is False , indicating that the search involves growing segments. |
Perform Vector Search
Perform vector search using Milvus. To search in a specific partition, please specify the partition name list.
Milvus supports setting a consistency level for search. The example in this topic sets the consistency level to Strong
. You can also set the consistency level to Bounded
, Session
, or Eventually
. For more information on the four consistency levels in Milvus, see Consistency.
searchResult, err := milvusClient.Search(
context.Background(), // ctx
"book", // CollectionName
[]string{}, // partitionNames
"", // expr
[]string{"book_id"}, // outputFields
[]entity.Vector{entity.FloatVector([]float32{0.1, 0.2})}, // vectors
"book_intro", // vectorField
entity.L2, // metricType
10, // topK
sp, // sp
opt,
)
if err != nil {
log.Fatal("Failed to search collection:", err.Error())
}
Parameter | Description | Options |
---|---|---|
ctx |
Context used to control the API call process | N/A |
CollectionName |
Name of the collection to be loaded | N/A |
partitionNames |
List of partition names to be loaded. If empty, search all partitions | N/A |
expr |
Boolean expression for filtering properties | See Boolean Expression Rules for detailed information |
output_fields |
Names of fields to be returned | N/A |
vectors |
Vectors to be searched | N/A |
vectorField |
Name of the field to be searched | N/A |
metricType |
Metric type used for search | This parameter must be the same as the metric type used to build the index |
topK |
Number of results to be returned. The sum of this value and offset in opts should be less than 16384 |
N/A |
sp |
entity.SearchParam specific to the index |
N/A |
Check the primary key values and distances of the most similar vectors.
fmt.Printf("%#v\n", searchResult)
for _, sr := range searchResult {
fmt.Println(sr.IDs)
fmt.Println(sr.Scores)
}
After the search is completed, release the collection loaded in Milvus to reduce memory consumption.
err := milvusClient.ReleaseCollection(
context.Background(), // ctx
"book", // CollectionName
)
if err != nil {
log.Fatal("Failed to release collection:", err.Error())
}
Limitations
Feature | Maximum Limit |
---|---|
Length of collection name | 255 characters |
Number of partitions in a collection | 4,096 |
Number of fields in a collection | 256 |
Number of shards in a collection | 256 |
Dimensionality of vectors | 32,768 |
Top K | 16,384 |
Number of target input vectors | 16,384 |
Performing Mixed Search
Mixed search is essentially a vector search with attribute filtering. By specifying a boolean expression for filtering scalar fields or primary key fields, you can limit the search based on specific conditions.
The following example demonstrates how to perform mixed search based on regular vector search. Suppose you want to search for specific books based on the vectorized introduction of the books, but only want to retrieve books within a specific word count range. You can then filter the word_count
field by specifying a boolean expression in the search parameters. Milvus will only search for similar vectors within entities that match the expression.
sp, _ := entity.NewIndexFlatSearchParam(
10,
)
opt := client.SearchQueryOptionFunc(func(option *client.SearchQueryOption) {
option.Limit = 3
option.Offset = 0
option.ConsistencyLevel = entity.ClStrong
option.IgnoreGrowing = false
})
searchResult, err := milvusClient.Search(
context.Background(),
"book",
[]string{},
"word_count <= 11000",
[]string{"book_id"},
[]entity.Vector{entity.FloatVector([]float32{0.1, 0.2})},
"book_intro",
entity.L2,
2,
sp,
opt,
)
if err != nil {
log.Fatal("Failed to search collection:", err.Error())
}
Check the returned results.
fmt.Printf("%#v\n", searchResult)
for _, sr := range searchResult {
fmt.Println(sr.IDs)
fmt.Println(sr.Scores)
}