Skip to main content

Query()

This method conducts metadata filtering within a collection.

func (c *GrpcClient) Query(ctx context.Context, collectionName string, partitionNames []string, expr string, outputFields []string, opts ...SearchQueryOptionFunc) (ResultSet, error)

Parameter

Description

Type

ctx

Context for the current call to work.

context.Context

collectionName

Name of a collection.

string

partitionNames

List of partition names.

If left empty, all partitions are involved in this operation. Otherwise, only the specified partitions are involved.

[]string

expr

Boolean expression for metadata filtering.

For details, refer to Scalar Filtering Rules.

string

outputFields

List of field names to include in the return.

[]string

opts

Extra query options.

You can add multiple client.SearchQueryOptionFunc instances.

...client.SearchQueryOptionFunc

Return

A client.ResultSet, which is a slice of entity.Column.

client.ResultSet

The client.ResultSet provides the following methods for you to manipulate the query results.

Method

Return Type

Description

GetColumn(fieldName string)

entity.Column

Return the column with the provided name.

Len()

int

Return the number of entities in the query result.

Slice(start, end int)

client.ResultSet

Return a slice of the query result.

Errors

Any error in the execution of the request. Possible errors are as follows:

  • ErrClientNotReady: The client is not connected to Milvus.

  • ErrCollectionNotExists: The collection with the specified name does not exist.

  • The call to this API fails.

Example

// query
queryRes, errQuery := mc.Query(context.Background(), collectionName, []string{}, "10< id < 20", []string{}, client.WithSearchQueryConsistencyLevel(entity.ClStrong))
if errQuery != nil {
log.Fatal("failed to query collection:", errQuery.Error())
}
for _, res := range queryRes {
log.Println(res.Name(), res.FieldData())
}