dense_vector 变成“float”类型的字段

问题描述 投票:0回答:1

我尝试使用elasticsearch实现一个搜索引擎。为此,我创建了一个带有包含dense_vector 字段的映射的索引。 PUT {{弹性 uri}}/{{索引}}

{
    "mappings": {
        "properties": {
            ...
            "title_embeddings": {
                "type": "dense_vector",
                "index": true,
                "similarity": "cosine",
                "dims": 1024,
                "index_options": {
                    "type": "hnsw",
                    "ef_construction": 100,
                    "m": 16
                }
            },
            "title": {
                "type": "text"
            },
            ...
        }
    }
}

问题是,当我使用API检索它时,该字段的类型是“float”而不是dense_vector。

获取{{elastic-uri}}/{{index}}/_search

{
    "{{index}}": {
        "mappings": {
            "properties": {
                ...
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "title_embeddings": {
                    "type": "float"
                }
                ...
            }
        }
    }
}

而且我无法对其使用 KNN 搜索,因为它不是正确的类型:

获取{{elastic-uri}}/{{index}}/_search

{
  "knn": {
    "field": "title_embeddings",
    "query_vector": [0.1, 3.2, 2.1],
    "k": 2,
    "num_candidates": 100
  }
}

错误:无法创建查询:[knn] 查询仅在 [dense_vector] 字段上受支持

elasticsearch mapping artificial-intelligence knn embedding
1个回答
0
投票

我使用以下代码索引我的数据:

es.index(
            index='{{index}}',
            document=json.dumps(page.__dict__),
            error_trace=True
        )

使用这个新代码就可以工作了:

 es.index(
            index='{{index}}',
            document={
                "id": page.id,
                "title": page.title,
                "title_embeddings": page.title_embeddings,
                ...
            },
            error_trace=True
        )
© www.soinside.com 2019 - 2024. All rights reserved.