< Docs
  • Python


This operation conducts a vector similarity search with an optional scalar filtering expression.

Request Syntax

    data: list[list[float]], 
    anns_field: str, 
    param: dict, 
    limit: int 
    expr: str | None, 
    partition_names: list[str] | None, 
    output_fields: list[str] | None, 
    timeout: float | None, 
    round_decimal: int


  • data (list[list[float]]) -


    A list of vector embeddings.

    Milvus searches for the most similar vector embeddings to the specified ones.

  • anns_field (str) -

    The name of the target vector field of the current search.

    This parameter defaults to an empty string. If this parameter is left unspecified, the default value applies, indicating that the only vector field in the collection will be used as the search target.

  • param (dict) -


    The parameter settings specific to this operation.

    • metric_type (str) -

      The metric type applied to this operation. This should be the same as the one used when you index the vector field specified above.

      Possible values are L2, IP, and COSINE.

    • params (dict) -

      Additional parameters.

      • offset (int) -

        The number of records to skip in the search result.

        You can use this parameter in combination with limit to enable pagination.

        The sum of this value and limit should be less than 16,384.

      • radius (float) -

        Determines the threshold of least similarity. When setting metric_type to L2, ensure that this value is greater than that of range_filter. Otherwise, this value should be lower than that of range_filter.

      • range_filter (float) -

        Refines the search to vectors within a specific similarity range. When setting metric_type to IP or COSINE, ensure that this value is greater than that of radius. Otherwise, this value should be lower than that of radius.

    For details on other applicable search parameters, refer to In-memory Index and On-disk Index.

  • limit (int) -

    The total number of entities to return.

    You can use this parameter in combination with offset in param to enable pagination.

    The sum of this value and offset in param should be less than 16,384.

  • expr (str) -

    A scalar filtering condition to filter matching entities.

    The value defaults to None, indicating that scalar filtering is ignored. To build a scalar filtering condition, refer to Boolean Expression Rules.

  • output_fields (list) -

    A list of field names to include in each entity in return.

    The value defaults to None. If left unspecified, only the primary field is included.

  • timeout (float) -

    The timeout duration for this operation. Setting this to None indicates that this operation timeouts when any response arrives or any error occurs.

  • round_decimal (int) -

    The number of decimal places that Milvus rounds the calculated distances to.

    The value defaults to -1, indicating that Milvus skips rounding the calculated distances and returns the raw value.




A SearchResult object that contains a list of Hits objects.

  • Response structure


    A SearchResult object contains a list of Hits objects, each corresponding to a query vector in the search request.

    A Hits object contains a list of Hit objects, each corresponding to an entity hit by the search.

    ├── SearchResult
    │   └── Hits  
    │       ├── ids
    │       ├── distances
    │       └── Hit
    │           ├── id
    │           ├── distance
    │           ├── score
    │           ├── vector
    │           └── get()
  • Properties and methods

    • A Hits object has the following fields:

      • ids (list[int] | list[str])

        A list containing the IDs of the hit entities.

      • distances (list[float])

        A list of distances from the hit entities' vector fields to the query vector.

    • A Hit object has the following fields:

      • id (int | str)

        The ID of a hit entity.

      • distance (float)

        The distance from a hit entity's vector field to the query vector.

      • score (float)

        An alias to distance.

      • vector (list[float])

        The vector field of a hit entity.

      • get(field_name: str)**

        A function to get the value of the specified field in a hit entity.


  • MilvusException

    This exception will be raised when any error occurs during this operation.


from pymilvus import Collection, Partition

# Get an existing collection
collection = Collection(name="test_collection")

# Get an existing partition
partition = Partition(name="test_partition")

LIMIT = 10

param = {
    "metric_type": "COSINE",
    "params": {
        "nprobe": 1024,
        "radius": 0.2,
        "range_filter": 1.0

# Create a search iterator
res =
    expr="id > 3",
    output_fields=["id", "vector"]

for hits in res:
    # Get ids
    # Get distances
    for hit in hits:
        # Get id
        # Get distance
        hit.distance # hit.score
        # Get vector
        # Get output field

Related operations

The following operations are related to search():


Was this page helpful?