milvus-logo
LFAI
< Docs
  • Java
    • v2

query()

This operation conducts a scalar filtering with a specified boolean expression.

public QueryResp query(QueryReq request)

Request Syntax

query(QueryReq.builder()
    .collectionName(String collectionName)
    .partitionNames(List<String> partitionNames)
    .outputFields(List<String> outputFields)
    .ids(List<Object> ids)
    .filter(String filter)
    .consistencyLevel(ConsistencyLevel consistencyLevel)
    .offset(long offset)
    .limit(long limit)
    .build()
)

BUILDER METHODS:

  • collectionName(String collectionName)

    The name of an existing collection.

  • partitionNames(List<String> partitionNames)

    A list of partition names.

  • outputFields(List<String> outputFields)

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

    The value defaults to None. If left unspecified, all fields in the collection are selected as the output fields.

  • ids(List<Object> ids)

    The IDs of entities to query.

  • filter(String filter)

    A scalar filtering condition to filter matching entities.

    You can set this parameter to an empty string to skip scalar filtering. To build a scalar filtering condition, refer to Boolean Expression Rules.

  • consistencyLevel(ConsistencyLevel consistencyLevel)

    The consistency level of the target collection.

    The value defaults to the one specified when you create the current collection, with options of Strong (0), Bounded (1), Session (2), and Eventually (3).

    what is the consistency level?

    Consistency in a distributed database specifically refers to the property that ensures every node or replica has the same view of data when writing or reading data at a given time.

    Milvus supports four consistency levels: Strong, Bounded Staleness, Session, and Eventually. The default consistency level in Milvus is Bounded Staleness.

    You can easily tune the consistency level when conducting a vector similarity search or query to make it best suit your application.

  • offset(long offset)

    The number of records to skip in the query 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.

  • limit(long limit)

    The number of records to return in the query result.

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

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

RETURN TYPE:

List<QueryResult>

RETURNS:

A list of QueryResult objects representing specific query results with the specified output fields

PARAMETERS:

  • entity (Map<String,Object>)

    A map that contains key-value pairs of field names and their values.

notes

If the number of returned entities is less than expected, duplicate entities may exist in your collection.

EXCEPTIONS:

  • MilvusClientExceptions

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

Example

//query by filter "id < 10"
QueryReq queryReq = QueryReq.builder()
        .collectionName("test")
        .filter("id < 10")
        .build();
QueryResp queryResp = client.query(queryReq);
// QueryResp(queryResults=[QueryResp.QueryResult(entity={vector=[0.54480153, 0.8220552], id=0}), QueryResp.QueryResult(entity={vector=[0.75052005, 0.7581255], id=1}), QueryResp.QueryResult(entity={vector=[0.56026894, 0.23027527], id=2}), QueryResp.QueryResult(entity={vector=[0.38339353, 0.56710696], id=3}), QueryResp.QueryResult(entity={vector=[0.7986327, 0.54763454], id=4}), QueryResp.QueryResult(entity={vector=[0.5766768, 0.12176812], id=5})])