searchByVector 用于向量检索。根据查询的向量,搜索与其距离最近的 limit 个向量。
说明
public DataApiResponse<SearchResult> searchByVector(SearchByVectorRequest request) throws ApiClientException, VectorApiException
请求参数是 SearchByVectorParam,SearchByVectorParam 实例包含的参数如下表所示。
参数 | 类型 | 是否必选 | 参数说明 |
|---|---|---|---|
denseVector | List | 是 | 用于检索的向量字段名称。 |
sparseVector | Map<String, Float> | 否 | 检索的稀疏向量。 |
filter | Map<String, Object> | 否 | 过滤条件,详见标量过滤
|
outputFields | List | 否 | 要返回的标量字段列表.
|
limit | Integer | 否 | 检索结果数量,最大5000个。 |
offset | Integer | 否 | 偏移量。仅分页场景下使用,不建议设置过大值,否则有深分页影响。默认值为0。设置值至少为0,语义和mysql的offset相同。 |
advance | SearchAdvance | 否 | 高级参数,详见检索公共参数 |
属性 | 类型 | 说明 |
|---|---|---|
data | List | 见下 |
filterMatchedCount | Integer | 筛选匹配数量 |
totalReturnCount | Integer | 返回数量 |
realTextQuery | String | |
tokenUsage | Object | 使用token |
属性 | 类型 | 说明 |
|---|---|---|
id | Object | 主键 id。 |
fields | Map<String, Object> | 请求返回中的 fields 字段 |
score | Float | 相似度 |
annScore | Float | ann得分 |
package org.example.newsubproduct.data.search; import com.volcengine.vikingdb.runtime.core.ClientConfig; import com.volcengine.vikingdb.runtime.core.auth.AuthWithAkSk; import com.volcengine.vikingdb.runtime.enums.Scheme; import com.volcengine.vikingdb.runtime.exception.VectorApiException; import com.volcengine.vikingdb.runtime.vector.model.request.*; import com.volcengine.vikingdb.runtime.vector.model.response.*; import com.volcengine.vikingdb.runtime.vector.service.VectorService; import java.util.ArrayList; import java.util.List; import java.util.Random; public class SearchByVector { public static void main(String[] args) { VectorService vectorService = null; try { vectorService = new VectorService( Scheme.HTTPS, "api-vikingdb.vikingdb.cn-beijing.volces.com", // 填写向量库数据面v2的域名 https://www.volcengine.com/docs/84313/1792715 "cn-beijing", new AuthWithAkSk(System.getenv("AK"), System.getenv("SK")), ClientConfig.builder().build() ); } catch (Exception e) { System.err.println("Client initialization failed: " + e.getMessage()); e.printStackTrace(); return; } SearchByVectorRequest request = SearchByVectorRequest.builder() .collectionName("your_collection_name") // 替换为您的集合名称 .indexName("your_index_name") // 替换为您的索引名称 .denseVector(generateRandomVector(128)) // 提供用于查询的向量 .limit(5) // 返回最相似的 5 个结果 .build(); try { DataApiResponse<SearchResult> response = vectorService.searchByVector(request); System.out.println("request success:"); System.out.println(response); } catch (VectorApiException vectorApiException) { System.err.println("request vectorApiException:"); System.out.println("apiName: " + vectorApiException.getApiName()); System.out.println("httpStatusCode: " + vectorApiException.getHttpStatusCode()); System.out.println("code: " + vectorApiException.getCode()); System.out.println("message: " + vectorApiException.getMessage()); System.out.println("requestId: " + vectorApiException.getRequestId()); System.out.println("responseContext: " + vectorApiException.getResponseContext().getBody()); } catch (Exception e) { System.err.println("request exception, message : " + e.getMessage()); e.printStackTrace(); } } private static List<Float> generateRandomVector(int n) { List<Float> randomList = new ArrayList(); Random random = new Random(); for(int i = 0; i < n; ++i) { float randomFloat = random.nextFloat() * 2.0F - 1.0F; randomList.add(randomFloat); } return randomList; } }