简介
API | 操作描述 |
对数据集内的文件进行多种跨模态检索,支持以图搜图、以文搜图、以文搜文档。 |
功能说明
对数据集内的文件进行多模态混合检索,支持以下功能:
以图搜图:输入图片,检索内容相似的图片。
以文搜图:输入自然语言文本(如"包含一棵大树的图片"),检索语义相关的图片。
以文搜文档:输入自然语言文本,检索语义相关的文本段落及其归属的文档。
方法原型
func (s *MetaInsightService) HybridSearch(ctx context.Context, opt *HybridSearchOptions) (*HybridSearchResult, *Response, error)
请求示例
示例一:以图搜图
package mainimport ("context""encoding/json""fmt""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。// 替换为用户的 region,存储桶 region 可以在 COS 控制台"存储桶概览"查看。u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-beijing.myqcloud.com")metaInsight, _ := url.Parse("https://1250000000.ci.ap-beijing.myqcloud.com")b := &cos.BaseURL{BucketURL: u, MetaInsightURL: metaInsight}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{// 通过环境变量获取密钥// 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥。SecretID: os.Getenv("SECRETID"),SecretKey: os.Getenv("SECRETKEY"),},})opt := &cos.HybridSearchOptions{DatasetName: "imagesearch",Mode: "pic",Templates: "ImageSearch",SearchURIs: []string{"cos://examplebucket-1250000000/test.jpg"},Limit: 10,MatchThreshold: 1,Filter: map[string]interface{}{"$and": []map[string]interface{}{{"MediaType": map[string]interface{}{"$in": []string{"image", "document"},},},{"Size": map[string]interface{}{"$gt": 123,},},},},}res, _, err := c.MetaInsight.HybridSearch(context.Background(), opt)if err != nil {fmt.Println(err)return}bs, _ := json.MarshalIndent(res, "", " ")fmt.Println(string(bs))}
示例二:以文搜图
package mainimport ("context""encoding/json""fmt""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-beijing.myqcloud.com")metaInsight, _ := url.Parse("https://1250000000.ci.ap-beijing.myqcloud.com")b := &cos.BaseURL{BucketURL: u, MetaInsightURL: metaInsight}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"),SecretKey: os.Getenv("SECRETKEY"),},})opt := &cos.HybridSearchOptions{DatasetName: "imagesearch",Mode: "text",Templates: "ImageSearch",SearchText: "包含一棵大树的图片",Limit: 10,MatchThreshold: 80,}res, _, err := c.MetaInsight.HybridSearch(context.Background(), opt)if err != nil {fmt.Println(err)return}bs, _ := json.MarshalIndent(res, "", " ")fmt.Println(string(bs))}
示例三:以文搜文档
package mainimport ("context""encoding/json""fmt""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-beijing.myqcloud.com")metaInsight, _ := url.Parse("https://1250000000.ci.ap-beijing.myqcloud.com")b := &cos.BaseURL{BucketURL: u, MetaInsightURL: metaInsight}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"),SecretKey: os.Getenv("SECRETKEY"),},})opt := &cos.HybridSearchOptions{DatasetName: "docsearch",Mode: "text",Templates: "DocSearch",SearchText: "包含一棵大树的文档",Limit: 10,MatchThreshold: 1,Filter: map[string]interface{}{"$and": []map[string]interface{}{{"MediaType": map[string]interface{}{"$in": []string{"image", "document"},},},{"Size": map[string]interface{}{"$gt": 123,},},},},}res, _, err := c.MetaInsight.HybridSearch(context.Background(), opt)if err != nil {fmt.Println(err)return}bs, _ := json.MarshalIndent(res, "", " ")fmt.Println(string(bs))}
参数说明
参数名称 | 类型 | 描述 | 是否必选 |
DatasetName | String | 数据集名称,同一个账户下唯一。 | 是 |
Mode | String | 检索输入类型,取值如下: pic:输入图片进行以图搜图。text:输入文本进行检索。默认值为 pic。 | 否 |
Templates | String | 指定输出的数据类型,取值如下: ImageSearch:图像检索,输出图片结果。DocSearch:文档检索,输出文档结果,此时 Mode 必须为 text 。 | 是 |
SearchURIs | String Array | 资源标识字段,表示为 COS URI 格式: cos://<BucketName-APPID>/<Path>。当 Mode 为 pic 时必选。 | 否 |
SearchText | String | 检索语句,最多支持60个 UTF-8 字符。当 Mode 为 text 时必选。 | 否 |
Limit | Integer | 返回结果数量,默认值为10,取值范围 (0, 100]。 | 否 |
MatchThreshold | Integer | 返回结果的最低相关度分数阈值,默认值为0,推荐值为80,取值范围 [0, 100]。 | 否 |
Filter | Container | 否 |
返回结果说明
调用
HybridSearch 函数后,会把 API 返回的 JSON 内容解析到 HybridSearchResult 结构体中。具体返回参数如下:参数名称 | 类型 | 描述 |
RequestId | String | 请求 ID。 |
ImageResult | Container Array | 图像检索结果列表(当 Templates 为 ImageSearch 时返回)。 |
DocResult | Container Array | 文档检索结果列表(当 Templates 为 DocSearch 时返回)。 |
ImageResult 子节点:
参数名称 | 类型 | 描述 |
URI | String | 文件的资源地址(COS URI)。 |
Score | Integer | 匹配得分。 |
DocResult 子节点:
参数名称 | 类型 | 描述 |
URI | String | 文档在对象存储中的 URI。 |
Text | String | 匹配的原始文本内容,可能包含图片占位符(如 {Image_0})。 |
Score | Integer | 搜索结果的相关度评分。 |
TextPage | Integer | 匹配内容所在的页码。 |
ImageUrls | Container | 图片占位符到实际图片标识符的映射集。 |