# KnnQuery in java client

**URL:** <https://discuss.elastic.co/t/knnquery-in-java-client/356645>\
**Category:** Elasticsearch\
**Tags:** language-clients, vector-search\
**Created:** [April 3, 2024, 5:36am UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645 "2024-04-03T05:36:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aj\_stor](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aj\_stor](https://discuss.elastic.co/u/aj_stor)\
**Post date:** [April 3, 2024, 5:36am UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645/1 "2024-04-03T05:36:19Z")

</div>

I am trying to execute a KnnQuery ([Knn query | Elasticsearch Guide [8.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-knn-query.html)) in my java application

(So not the knn search: [k-nearest neighbor (kNN) search | Elasticsearch Guide [8.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#knn-similarity-search)).

When you use knn in a query, you can't set parameter `k` since the `size` parameter is used.

But, the Java client with version 8.13 requires the `k` parameter: (`co.elastic.clients.util.MissingRequiredPropertyException: Missing required property 'KnnQuery.k'` ):

 ![image (5)](https://us1.discourse-cdn.com/elastic/original/3X/c/f/cf47c5fa7272566413271ec8dc2bd43198ce7e20.jpeg)

So my problem is kind of a deadlock. Elasticsearch doesn't support the `k` parameter when knn is used in a query, but the java client requires the `k` parameter.

So anyone else having this issue?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 3, 2024, 12:09pm UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645/2 "2024-04-03T12:09:30Z")

</div>

It's a bug. Could you open an issue in [GitHub - elastic/elasticsearch-java: Official Elasticsearch Java Client](https://github.com/elastic/elasticsearch-java)?

There's no workaround when using `knn` as a query vs the top level `knn` request.

---

<div class="post-metadata">

**Author:** ![Quentin\_Pradet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quentin_pradet/32/94192_2.png) [@Quentin\_Pradet](https://discuss.elastic.co/u/Quentin_Pradet)\
**Post date:** [April 8, 2024, 1:50pm UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645/3 "2024-04-08T13:50:23Z")

</div>

Thanks for the report. We [fixed the issue in the Elasticsearch specification](https://github.com/elastic/elasticsearch-specification/pull/2490) and now need to generate a new Java client.

I previously thought there was an existing workaround by setting the new values explicitly, but that does not work so I removed them from this post.

Sorry for the hassle.

---

<div class="post-metadata">

**Author:** ![WEI\_GUO](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wei_guo/32/100652_2.png) [@WEI\_GUO](https://discuss.elastic.co/u/WEI_GUO)\
**Post date:** [April 15, 2024, 12:53am UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645/4 "2024-04-15T00:53:21Z")

</div>

How to set `k` to `size`?  
if I set any value for k, the elastic will give error on

```auto
    "root_cause": [
      {
        "type": "x_content_parse_exception",
        "reason": "[15:23] [knn] unknown field [k]"
      }
    ],

```

---

<div class="post-metadata">

**Author:** ![Quentin\_Pradet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quentin_pradet/32/94192_2.png) [@Quentin\_Pradet](https://discuss.elastic.co/u/Quentin_Pradet)\
**Post date:** [April 15, 2024, 4:41am UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645/5 "2024-04-15T04:41:56Z")

</div>

@WEI_GUO Hello, can you please share the code you used to set `k`?

---

<div class="post-metadata">

**Author:** ![Quentin\_Pradet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quentin_pradet/32/94192_2.png) [@Quentin\_Pradet](https://discuss.elastic.co/u/Quentin_Pradet)\
**Post date:** [April 15, 2024, 11:36am UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645/6 "2024-04-15T11:36:30Z")

</div>

@BenTrent tells me that the knn query does not allow `k` to be set at all in knn queries (while it works in the top level knn objects). So you have to wait for the next Java client, sorry. The good news is that the specification was fixed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 13, 2024, 11:37am UTC](https://discuss.elastic.co/t/knnquery-in-java-client/356645/7 "2024-05-13T11:37:21Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
