本指南展示如何通过用户生成的嵌入向量(embeddings)来实现AI驱动的搜索,而无需依赖第三方工具。

前提条件

  • 一个 Meilisearch 项目

配置自定义嵌入器

配置 embedder 索引设置,将其来源设置为 userProvided

curl \
  -X PATCH 'MEILISEARCH_URL/indexes/movies/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
    "embedders": {
      "image2text": {
        "source":  "userProvided",
        "dimensions": 3
      }
    }
  }'

向 Meilisearch 添加文档

接下来,使用/documents端点上传向量化文档。将向量数据放置在文档的 _vectors 字段中:

curl -X POST -H 'content-type: application/json' \
'localhost:7700/indexes/products/documents' \
--data-binary '[
    { "id": 0, "_vectors": {"image2text": [0, 0.8, -0.2]}, "text": "frying pan" },
    { "id": 1, "_vectors": {"image2text": [1, -0.2, 0]}, "text": "baking dish" }
]'

使用用户提供的嵌入向量进行向量搜索

当使用自定义嵌入器(embedder)时,您需要同时向量化您的文档和用户查询。

获取查询向量后,将其传递给 vector 搜索参数以执行 AI 驱动的搜索:

curl -X POST -H 'content-type: application/json' \
  'localhost:7700/indexes/products/search' \
  --data-binary '{ "vector": [0, 1, 2] }'

vector 必须是一个数字数组,表示搜索向量。在使用用户提供的嵌入向量进行向量搜索时,您需要自行生成这些向量。

vector 可以与其他搜索参数一起使用,包括 filtersort

curl -X POST -H 'content-type: application/json' \
  'localhost:7700/indexes/products/search' \
  --data-binary '{
    "vector": [0, 1, 2],
    "filter": "price < 10",
    "sort": ["price:asc"]
  }'