# Pinned query not good with KNN search

**URL:** <https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning, vector-search\
**Created:** [April 25, 2024, 12:29pm UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210 "2024-04-25T12:29:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chenko](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@Chenko](https://discuss.elastic.co/u/Chenko)\
**Post date:** [April 25, 2024, 12:29pm UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210/1 "2024-04-25T12:29:37Z")

</div>

Hi,

When using a normal knn query this works:

```auto
"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:

```auto

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

```auto
"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](https://discuss.elastic.co/t/inference-api-not-working/358209).

Kind regards,  
Chenko

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [April 25, 2024, 1:03pm UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210/2 "2024-04-25T13:03:08Z")

</div>

@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](https://www.elastic.co/guide/en/elasticsearch/reference/8.12/query-dsl-knn-query.html)

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.

---

<div class="post-metadata">

**Author:** ![Chenko](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@Chenko](https://discuss.elastic.co/u/Chenko)\
**Post date:** [April 25, 2024, 1:23pm UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210/3 "2024-04-25T13:23:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [April 25, 2024, 1:42pm UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210/4 "2024-04-25T13:42:15Z")

</div>

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

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

```auto
{
    "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.

---

<div class="post-metadata">

**Author:** ![Chenko](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@Chenko](https://discuss.elastic.co/u/Chenko)\
**Post date:** [April 25, 2024, 1:51pm UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210/5 "2024-04-25T13:51:51Z")

</div>

Thanks for the example!

> [@BenTrent](#):
>
> It seems like you are using nested vectors (chunked) right?

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?

---

<div class="post-metadata">

**Author:** ![Shell\_Dias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shell_dias/32/136785_2.png) [@Shell\_Dias](https://discuss.elastic.co/u/Shell_Dias)\
**Post date:** [July 24, 2024, 5:18am UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210/6 "2024-07-24T05:18:23Z")

</div>

And the DSL for KNN does not allow passing a text string, only vectors. If you choose to use a trained model that's in the Elasticsearch tool, how can you obtain that vector on a local client?

Considering that Model A does not generate the same vector as Model B.

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [July 24, 2024, 12:09pm UTC](https://discuss.elastic.co/t/pinned-query-not-good-with-knn-search/358210/7 "2024-07-24T12:09:24Z")

</div>

Hi there @Shell_Dias,

I encourage you to start new threads for new topics, so that it doesn't add too much clutter to already resolved topics.

With that said, I also encourage you to check out the [query\_vector\_builder](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#knn-semantic-search) if you're interested in performing kNN search using a text string. You can also upload models using [eland](https://eland.readthedocs.io/en/v8.14.0/) if you're not using a built in Elasticsearch model.
