# 图像搜索

Typesense 可用于:

  1. 基于图像中物品描述的图像搜索
  2. 图像相似性搜索

利用 Typesense 对 CLIP ML 模型 (opens new window)的内置支持。

# 创建集合

我们将创建一个新集合并使用 ts/clip-vit-b-p32 CLIP 模型。

以下是使用的集合模式








 
 
 
 
 



 
 
 
 
 
 
 





{
  "name": "images",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "image",
      "type": "image",
      "store": false
    },
    {
      "name": "embedding",
      "type": "float[]",
      "embed": {
        "from": [
          "image"
        ],
        "model_config": {
          "model_name": "ts/clip-vit-b-p32"
        }
      }
    }
  ]
}

注意名为 image 的字段使用了新的数据类型 type: image,它是 BMP、JPG 或 PNG 格式图像的 base64 编码字符串。

字段定义中的 store: false 属性告诉 Typesense 仅将该字段用于生成嵌入向量,然后丢弃图像数据而不存储在磁盘上。

你也可以将文本和图像合并为单个嵌入向量,使用如下集合模式:








 
 
 
 
 



 
 
 
 
 
 
 






{
  "name": "images",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "image",
      "type": "image",
      "store": false
    },
    {
      "name": "embedding",
      "type": "float[]",
      "embed": {
        "from": [
          "name",
          "image"
        ],
        "model_config": {
          "model_name": "ts/clip-vit-b-p32"
        }
      }
    }
  ]
}

# 为包含图片的文档建立索引

以下是一个示例 JSON 文档,您可以将其导入到 Typesense 中:

{
  "name": "一张包含狗的图片",
  "image": "<图片的 base64 编码字符串>"
}

# 使用语义搜索查找图片

现在我们来搜索所有包含动物的图片:

curl "http://localhost:8108/multi_search" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
          "searches": [
            {
              "collection": "images",
              "q": "animals",
              "query_by": "embedding"
            }
          ]
        }'

在底层,Typesense 使用 q 参数的值通过相同的 CLIP 模型生成嵌入向量,然后对这些向量进行最近邻搜索并返回最接近的结果。

# 搜索相似图片

要搜索与我们数据集中某张特定图片相似的图片,可以使用如下搜索查询:

curl "http://localhost:8108/multi_search" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
          "searches": [
            {
              "collection": "images",
              "q": "*",
              "vector_query": "embedding:([], id:123)"
            }
          ]
        }'

这将返回与文档 id: 123 中的图片相似的所有图片,相似度由 CLIP 模型确定。