Query vectors (Java SDK V2)
This article describes how to use the OSS Java SDK V2 to call the QueryVectors operation to retrieve the vectors in a vector index that are most similar to a query vector, and to call the QueryVectorsFusion operation to perform combined vector and text search on a fusion index.
Permissions
An Alibaba Cloud account has full permissions by default, while RAM users and RAM roles have none. The Alibaba Cloud account owner or an administrator must grant access using a RAM Policy or a Bucket Policy.
|
API |
Action |
Description |
|
QueryVectors |
oss:QueryVectors |
Retrieves vectors using a similarity query. |
Sample code
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;
import com.aliyun.sdk.service.oss2.vectors.models.QueryVectorsRequest;
import com.aliyun.sdk.service.oss2.vectors.models.QueryVectorsResult;
import java.util.*;
public class QueryVectorsSample {
public static void main(String[] args) throws Exception {
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
.region("cn-hangzhou")
.accountId("1234567890")
.credentialsProvider(provider)
.build()) {
Map<String, Object> queryVector = new HashMap<>();
queryVector.put("float32", new float[]{0.1f, 0.2f, 0.3f});
QueryVectorsRequest request = QueryVectorsRequest.newBuilder()
.bucket("examplebucket")
.indexName("exampleindex")
.queryVector(queryVector)
.topK(10)
.returnDistance(true)
.returnMetadata(false)
.build();
QueryVectorsResult result = client.queryVectors(request);
System.out.printf("status code: %d, request id: %s%n", result.statusCode(), result.requestId());
}
}
}
Fusion search
For a fusion index, you can call the QueryVectorsFusion operation to combine vector search (KNN) with text search. You can pass text match conditions directly in the query, or combine multiple retrievers by using RrfRetriever or WeightRetriever. RrfRetriever fuses the results of all retrievers by using reciprocal rank fusion (RRF), and WeightRetriever fuses the results based on weights.
The fusion search feature requires alibabacloud-oss-v2 (Java SDK V2) 0.6.1 or later, and the index to query must be a fusion index.
Sample code
The following code performs fusion search on a fusion index, combining a KNN vector search with a text match search on the title field by using RRF:
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;
import com.aliyun.sdk.service.oss2.vectors.models.*;
import java.util.*;
public class QueryVectorsFusionSample {
public static void main(String[] args) throws Exception {
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
.region("cn-hangzhou")
.accountId("1234567890")
.credentialsProvider(provider)
.build()) {
// Build the KNN vector retriever
Knn knn = Knn.newBuilder()
.field("vector_field")
.queryVector(Arrays.asList(0.1f, 0.2f, 0.3f))
.topK(10)
.build();
// Build the text match condition (exact match on the title field with a boost of 2.0)
Map<String, Object> textMatch = new HashMap<>();
textMatch.put("value", "hello world");
textMatch.put("boost", 2.0f);
Map<String, Object> condition = new HashMap<>();
condition.put("$textMatch", textMatch);
Map<String, Object> query = new HashMap<>();
query.put("title", condition);
// Fuse vector search and text search by using RRF
RrfRetriever retriever = RrfRetriever.newBuilder()
.k(50)
.windowSize(100)
.retrievers(Arrays.asList(
RetrieverComponent.newBuilder()
.retriever(knn)
.weight(1.0f)
.build(),
RetrieverComponent.newBuilder()
.retriever(SimpleRetriever.newBuilder().query(query).build())
.weight(2.0f)
.build()))
.build();
QueryVectorsFusionRequest request = QueryVectorsFusionRequest.newBuilder()
.bucket("examplebucket")
.indexName("exampleindex")
.limit(10)
.returnMetadata(false)
.retriever(retriever)
.build();
QueryVectorsFusionResult result = client.queryVectorsFusion(request);
System.out.printf("status code: %d, request id: %s%n", result.statusCode(), result.requestId());
}
}
}
If you only need text search, you can pass the text match condition described above directly in the query without building a retriever. To fuse the results of multiple retrievers based on weights, replace RrfRetriever with WeightRetriever.
Related documents
For the synchronous code sample, see QueryVectors.java.
For the asynchronous code sample, see QueryVectorsAsync.java, QueryVectorsFusion.java, and QueryVectorsFusionAsync.java.