milvus-logo
LFAI
< Docs
  • Java
    • v1

search()

The MilvusClient interface. This method conducts an approximate nearest neighbor (ANN) search on a vector field and pairs up with a Boolean expression to conduct filtering on scalar fields before searching.

R<SearchResponse> search(SearchSimpleParam requestParam);

SearchSimpleParam

Use the SearchSimpleParam.Builder to construct a SearchSimpleParam object.

import io.milvus.param.highlevel.dml.SearchSimpleParam;
SearchSimpleParam.Builder builder = SearchSimpleParam.newBuilder();

Methods of SearchSimpleParam.Builder:

Method Description Parameters
withCollectionName(collectionName) Set the collection name. Collection name cannot be empty or null. collectionName: target collection name
withOutFields(List\ outFields) Specifies output scalar fields (Optional).
outFields: The name list of fields to be outputed
withFilter(String filter) filter: The expression to filter scalar fields
withVectors(List\ vectors) Set the target vector. Up to 16384 vectors allowed. vectors:
- If target field type is float vector, List\< List\gt; is required;
- If target field type is binary vector, List\ is required;
withOffset(Long offset) Specify a position, the returned entities before this position will be ignored. Only take effect when the 'limit' value is specified.Default value is 0, start from begin. offset: A value to define the position
withLimit(Long limit) Specify a value to control the returned number of entities. Must be a positive value.Default value is 10, will return without limit. limit: A value to define the limit of returned entities
withConsistencyLevel(ConsistencyLevelEnum consistencyLevel) Consistency level used in the search. If no level is specified, will use default consistency. Please refer to ConsistencyLevelEnum in Misc. consistencyLevel: The consistency level used in the search
build() Construct a SearchSimpleParam object. N/A

The SearchSimpleParam.Builder.build() can throw the following exceptions:

  • ParamException: error if the parameter is invalid.

Returns

This method catches all the exceptions and returns an R<SearchResponse> object.

  • If the API fails on the server side, it returns the error code and message from the server.

  • If the API fails by RPC exception, it returns R.Status.Unknown and the error message of the exception.

  • If the API succeeds, it returns a valid SearchResponse held by the R template.

Example

import io.milvus.param.*;
import io.milvus.response.SearchResultsWrapper;
import io.milvus.grpc.SearchResults;

SearchSimpleParam param = SearchSimpleParam.newBuilder()
        .withCollectionName(COLLECTION_NAME)
        .withVectors(generateFloatVector())
        .withOutputFields(Lists.newArrayList("*"))
        .withFilter(filter)
        .withLimit(100L)
        .withOffset(0L)
        .build();
R<SearchResponse> response = client.search(param)
if (response.getStatus() != R.Status.Success.getCode()) {
    System.out.println(response.getMessage());
}

for (QueryResultsWrapper.RowRecord rowRecord : response.getData().getRowRecords()) {
    System.out.println(rowRecord);
}

Feedback

Was this page helpful?