英文:
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 <index name>/_search
{
"knn": {
"field": "<name of field that contains the vector>",
"query_vector": [< actual query vector values >],
"k": 10,
"num_candidates": 100
}
}
and like this with python :
query = {
"field": "<name of field that contains the vector>",
"query_vector": query_vector,
"k": 10,
"num_candidates": 50
}
response = es.search(index="<index name>", knn=query)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论