Link Search Menu Expand Document Documentation Menu

向量搜索入门

本指南将向您展示如何在 OpenSearch 中使用您自己的向量。您将学习如何创建向量索引、添加位置数据以及运行向量搜索以在坐标平面上查找最近的酒店。虽然本示例为简单起见使用了二维向量,但相同的方法也适用于语义搜索和推荐系统中使用的更高维向量。

前提条件:安装 OpenSearch

如果您尚未安装 OpenSearch,请按照以下步骤创建集群。

在开始之前,请确保您的环境中已安装并运行 Docker
此演示配置不安全,不应用于生产环境。

下载并运行 OpenSearch

docker pull opensearchproject/opensearch:latest && docker run -it -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" -e "DISABLE_SECURITY_PLUGIN=true" opensearchproject/opensearch:latest

OpenSearch 现在在端口 9200 上运行。要验证 OpenSearch 是否正在运行,请发送以下请求:

curl https://:9200

您应该会收到如下响应:

{
  "name" : "a937e018cee5",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "GLAjAG6bTeWErFUy_d-CLw",
  "version" : {
    "distribution" : "opensearch",
    "number" : <version>,
    "build_type" : <build-type>,
    "build_hash" : <build-hash>,
    "build_date" : <build-date>,
    "build_snapshot" : false,
    "lucene_version" : <lucene-version>,
    "minimum_wire_compatibility_version" : "7.10.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "The OpenSearch Project: https://opensearch.org.cn/"
}

更多信息请参阅快速安装指南安装与升级 OpenSearch

步骤 1:创建向量索引

首先,创建一个用于存储示例酒店数据的索引。要向 OpenSearch 表明这是一个向量索引,请将 index.knn 设置为 true。您将把向量存储在一个名为 location 的向量字段中。您将摄入的向量是二维的,并且向量之间的距离将使用 欧几里得 l2 相似性度量来计算。

PUT /hotels-index
{
  "settings": {
    "index.knn": true
  },
  "mappings": {
    "properties": {
      "location": {
        "type": "knn_vector",
        "dimension": 2,
        "space_type": "l2"
      }
    }
  }
}

向量查询通常具有 size > 0 的值,因此默认情况下它们不会进入请求缓存。在 OpenSearch 2.19 或更高版本中,如果您的工作负载主要由向量查询组成,请考虑将动态集群设置 indices.requests.cache.maximum_cacheable_size 增加到更大的值,例如 256。这允许 size 最多为 256 的查询进入请求缓存,从而提高性能。更多信息请参阅请求缓存

步骤 2:向索引添加数据

接下来,向索引添加数据。每个文档代表一家酒店。每个文档中的 location 字段包含一个二维向量,用于指定酒店的位置。

POST /_bulk
{ "index": { "_index": "hotels-index", "_id": "1" } }
{ "location": [5.2, 4.4] }
{ "index": { "_index": "hotels-index", "_id": "2" } }
{ "location": [5.2, 3.9] }
{ "index": { "_index": "hotels-index", "_id": "3" } }
{ "location": [4.9, 3.4] }
{ "index": { "_index": "hotels-index", "_id": "4" } }
{ "location": [4.2, 4.6] }
{ "index": { "_index": "hotels-index", "_id": "5" } }
{ "location": [3.3, 4.5] }

步骤 3:搜索您的数据

现在搜索距离标记位置 [5, 4] 最近的酒店。要搜索最近的三家酒店,请将 k 设置为 3

POST /hotels-index/_search
{
  "size": 3,
  "query": {
    "knn": {
      "location": {
        "vector": [5, 4],
        "k": 3
      }
    }
  }
}

下图显示了坐标平面上的酒店。查询点标记为 Pin,每家酒店都标有其文档编号。

Hotels on a coordinate plane

响应中包含距离指定标记位置最近的酒店。

{
  "took": 1093,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 0.952381,
    "hits": [
      {
        "_index": "hotels-index",
        "_id": "2",
        "_score": 0.952381,
        "_source": {
          "location": [
            5.2,
            3.9
          ]
        }
      },
      {
        "_index": "hotels-index",
        "_id": "1",
        "_score": 0.8333333,
        "_source": {
          "location": [
            5.2,
            4.4
          ]
        }
      },
      {
        "_index": "hotels-index",
        "_id": "3",
        "_score": 0.72992706,
        "_source": {
          "location": [
            4.9,
            3.4
          ]
        }
      }
    ]
  }
}

自动生成向量嵌入

如果您的数据尚未采用向量格式,您可以直接在 OpenSearch 中生成向量嵌入。这允许您将文本或图像转换为其数值表示,以便进行相似性搜索。更多信息请参阅自动生成向量嵌入

后续步骤

剩余 350 字符

有问题?

想要贡献?