Pinned query not good with KNN search

Hi,

When using a normal knn query this works:

"knn": [
    {
    "inner_hits": {
      "_source": false,
      "fields": [
        "passages.text"
      ]
    },
    "field": "passages.vector.predicted_value",
    "k": 5,
    "num_candidates": 100,
    "query_vector_builder": {
      "text_embedding": {
        "model_id": "elastic__multilingual-e5-small-optimized",
        "model_text": "test text"
      }
    }
  },
  {
    "field": "title.predicted_value",
    "k": 5,
    "num_candidates": 100,
    "query_vector_builder": {
      "text_embedding": {
        "model_id": "elastic__multilingual-e5-small-optimized",
        "model_text": "test text"
      }
    }
  }]

Not only does an array not work, when just using 1 of the above knn queries:


{
 "size": 3,
 "query": {
   "pinned": {
     "ids": [ "1", "2" ],
     "organic": {
       "knn": {
    "inner_hits": {
      "_source": false,
      "fields": [
        "passages.text"
      ]
    },
    "field": "passages.vector.predicted_value",
    "k": 5,
    "num_candidates": 100,
    "query_vector_builder": {
      "text_embedding": {
        "model_id": "elastic__multilingual-e5-small-optimized",
        "model_text": "test query"
      }
    }
  }
     }
   }
 }
}



Returns the following error:

"caused_by": {
      "type": "x_content_parse_exception",
      "reason": "[7:15] [pinned] failed to parse field [organic]",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "[knn] queries cannot be provided directly, use the [knn] body parameter instead"
      }
    }

So currently I have the following issues;

  1. I am not able to do multiple knn queries ( Do knn queries on multiple fields.)
  2. I am not able to use knn queries with text, it seems I need to embed the text first.

Ps. I could not test with embeddings yet as I have problem with my inference pipeline.

Kind regards,
Chenko

@Chenko

Before 8.12, knn couldn't be provided as a query object directly. We only supported a top-level knn object.

Here are the docs for 8.12 on using knn in a query: Knn query | Elasticsearch Guide [8.12] | Elastic

In your case, for pinned query to work with knn, you must be at least on 8.12, and then utilize nested for your passage vectors.

Hi Ben,

Thanks for the swift response!

I See, this is indeed the case for me. I am currently using 8.11.

As for when I go to 8.12 I would be able to use text instead of first using the inference pipeline?

When you say I must utilize nested for my passage vectors, do you mean I need to do a nested query instead of a normal one with knn?
Could you give an example?

It seems like you are using nested vectors (chunked) right?

So, for pinned query and this, it would be something like:

{
    "size": 5,
    "query": {
        "pinned": {
            "ids": [
                "1",
                "2"
            ],
            "organic": {
				"nested": {
					"path": "passages",
					"inner_hits": {
						"_source": false,
						"fields": [
							"passages.text"
						]
					},
					"query": {
						"knn": {
							"field": "passages.vector.predicted_value",
							"query_vector": [...],
							"num_candidates": 100
						}
					}
				}
            }
        }
    }
}

Frustratingly, we didn't add the query vector builder stuff to knn query until later, sorry :(. That isn't until 8.14.

1 Like

Thanks for the example!

You are correct, I am indeed using nested vectors in the passages field. I am however also using normal vectors for the title.

I am now eagerly awaiting 8.14. any idea when it is set to release?