ElasticSearch|在Bool查询中使用KNN字段时出错

dgtucam1  于 2023-04-05  发布在  ElasticSearch
关注(0)|答案(1)|浏览(465)

我尝试在bool查询下的搜索API中使用knn。但获取和错误。我使用的是ElasticSearch8.6.2
以下是我的查询

GET document-with-embeddings/_search
{
    "query":
        {
            "bool": {
                "must": [
                  {
                    "knn": {
                               "text_embedding.predicted_value": {
                                 "vector": [
                                    -0.06544870883226395,
                                    -0.21647875010967255,
                                    ...................
                       ],
                                "k": 20
                               }
                                
                            }
                  }
                ],
                "filter": [],
                "should": [],
                "must_not": []
            }
        },
    "_source": [
    "name", "description" 
]
}

我的嵌入索引是

properties": {
                "text_embedding.predicted_value": {
                    "type": "dense_vector",
                    "dims": 384,
                    "index": true,
                    "similarity": "cosine"
                },

我得到了这个错误。

{
  "error": {
    "root_cause": [
      {
        "type": "x_content_parse_exception",
        "reason": "[7:28] [bool] failed to parse field [must]"
      }
    ],
    "type": "x_content_parse_exception",
    "reason": "[7:28] [bool] failed to parse field [must]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "[knn] queries cannot be provided directly, use the [knn] body parameter instead"
    }
  },
  "status": 400
}

这里要补充的一点是,我将使用一个复杂的查询。这就是为什么我使用bool。但是下面的简单查询对我来说是有效的,这不是我的目标。

GET document-with-embeddings/_search
{
"knn": {
    "field": "text_embedding.predicted_value",
    "query_vector": [...],
"k": 20,
    "num_candidates": 1000
},
"_source": [
    "custom"
]
}

任何帮助都很感激。

ojsjcaue

ojsjcaue1#

文档显示了这种模式。Knn必须在“查询”之外使用。

POST image-index/_search
{
  "query": {
    "match": {
      "title": {
        "query": "mountain lake",
        "boost": 0.9
      }
    }
  },
  "knn": {
    "field": "image-vector",
    "query_vector": [54, 10, -2],
    "k": 5,
    "num_candidates": 50,
    "boost": 0.1
  },
  "size": 10
}

相关问题