弹性搜索 | 在布尔查询中使用KNN字段时出现错误

huangapple go评论53阅读模式
英文:

Elastic Search | Error when Using KNN field in Bool query

问题

我正在尝试在“bool”查询下使用knn,但出现错误。我正在使用Elasticsearch 8.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"
    ]
}

感谢任何帮助。

英文:

I am trying to use knn in search API under bool query. But getting and error. I am using elastic search 8.6.2

Here is my query

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" 
]
}

And my indexing for the embedding is

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

And I am getting this error.

{
  "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
}

One point to add here, I will use a complex query. That's why I used bool. But a simple query as the below one works for me, which is not my goal.

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

Any help is appreciated.

答案1

得分: 1

[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#_combine_approximate_knn_with_other_features
英文:

The documentation shows this mode. The Knn must be used out of "query".

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
}

答案2

得分: 0

我在Kibana开发工具控制台中像这样进行了一个knn查询

```python
    POST <索引名称>/_search
    {
      "knn": {
        "field": "<包含向量的字段名称>",
        "query_vector": [<实际查询向量的数值>],
        "k": 10,
        "num_candidates": 100
      }
    }

以及在Python中像这样进行了查询:

    query = {
                "field": "<包含向量的字段名称>",
                "query_vector": query_vector,
                "k": 10,
                "num_candidates": 50
    }
              
    response = es.search(index="<索引名称>", knn=query)

<details>
<summary>英文:</summary>

I made a knn query like this from the kibana dev tool console :

```python
    POST &lt;index name&gt;/_search
    {
      &quot;knn&quot;: {
        &quot;field&quot;: &quot;&lt;name of field that contains the vector&gt;&quot;,
        &quot;query_vector&quot;: [&lt; actual query vector values &gt;],
        &quot;k&quot;: 10,
        &quot;num_candidates&quot;: 100
      }
    }

and like this with python :

    query = {
                &quot;field&quot;: &quot;&lt;name of field that contains the vector&gt;&quot;,
                &quot;query_vector&quot;: query_vector,
                &quot;k&quot;: 10,
                &quot;num_candidates&quot;: 50
    }
              
    response = es.search(index=&quot;&lt;index name&gt;&quot;, knn=query)

huangapple
  • 本文由 发表于 2023年3月31日 16:40:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定