milvus-logo
LFAI
< Docs
  • Python

query()

This method conducts a vector query.

Invocation

query(expr, offset, limit, output_fields=None, partition_names=None, timeout=None)

Parameters

ParameterDescriptionTypeRequired
exprBoolean expression to filter the dataStringTrue
partition_namesList of names of the partitions to search on.
All partition will be searched if it is left empty.
list[String]False
limitNumber of nearest records to return. The value of this parameter should be less than 16384.IntegerFalse
offsetNumber of results to skip in the returned set. This parameter is available only when limit is specified, and the sum of this value and limit should be less than 16384.IntegerFalse
output_fieldsList of names of fields to outputlist[String]False
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse
kwargs: consistency_levelConsistency level used in the search.String/IntegerFalse
kwargs: guarantee_timestampMilvus searches on the data view before this timestamp when it is provided. Otherwise, it searches the most updated data view. It can be only used in Customized level of consistency.IntegerFalse
kwargs: graceful_timePyMilvus will use current timestamp minus the graceful_time as the guarantee_timestamp for search. It can be only used in Bounded level of consistency.IntegerFalse
kwargs: travel_timestampTimestamp that is used for Time Travel. Users can specify a timestamp in a search to get results based on a data view at a specified point in time.IntegerFalse
kwargs: ignore_growingWhether to ignore growing segments during similarity searches. The value defaults to False, indicating that searches involve growing segments.BoolFalse

Return

A list that contains all results.

Raises

  • RpcError: error if gRPC encounter an error.
  • ParamError: error if the parameters are invalid.
  • DataTypeNotMatchException: error if wrong type of data is passed to server.
  • BaseException: error if the return result from server is not ok.

Example

from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
res = collection.query(
  expr="book_id in [2,4,6,8]", 
  output_fields=["book_id", "book_intro"],
  consistency_level="Strong"
)
sorted_res = sorted(res, key=lambda k: k['book_id'])
sorted_res

# You can also reference dynamic fields in filter expressions,
# and include them in output fields
res = collection.query(
    # demontrates the ways to reference a dynamic field.
    expr='$meta["dynamic_field_1"] > 10 and dynamic_field_2 == 10',
    # sets the names of the fields you want to retrieve from the search result.
    output_fields=['title', 'dynamic_field_1', 'dynamic_field_2'], 
)

sorted_res = sorted(res, key=lambda k: k['dynamic_field_1'])
Feedback

Was this page helpful?