# How to create a search request with multiple knn fields using the Node Client

**URL:** <https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098>\
**Category:** Elasticsearch\
**Tags:** language-clients, vector-search\
**Created:** [September 25, 2024, 9:43am UTC](https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098 "2024-09-25T09:43:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Mcpherson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_mcpherson/32/137850_2.png) [@Bruce\_Mcpherson](https://discuss.elastic.co/u/Bruce_Mcpherson)\
**Post date:** [September 25, 2024, 9:43am UTC](https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098/1 "2024-09-25T09:43:19Z")

</div>

My first post here - I'm using elasticsearch 8.15.0 Node client.

I have an index with multiple dense-vector fields, and I want to search on them all.

With the python client, I am able to do this.  
`{query: {bool: {should:[{knn:{knn spec...}}]}}`

According to the api docs I should be able to do this with v8.15.0  
`{knn: [{knn spec...}]}`

The node client insists that the knn property be specficied as a body parameter separate from the query like this.  
`search ({query, knn, ...})`

Using the approach, where the knn specs are included in a query.bool{}, (which works with Python), returns an error with the Node client.

Using the API documented approach with a knn array as a separate body argument to the Node client returns this error  
` Unknown key for a START_ARRAY in [knn]`

What is correct approach with the Node client for a query with multiple knn fields?

thank you

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [September 25, 2024, 10:45am UTC](https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098/2 "2024-09-25T10:45:12Z")

</div>

Hi @Bruce_Mcpherson !

[knn query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-knn-query.html) and [knn section](https://www.elastic.co/guide/en/elasticsearch/reference/8.9/knn-search.html#approximate-knn) allow a single field to be used.

You could combine multiple knn queries using a `bool` query:

```json
{
  "query": {
    "bool": {
      "should": [
        {
          "knn": {
            "field": "field1",
            ...
          }
        },
        {
          "knn": {
            "field": "field2",
            ...
          }
        }
      ]
    }
  }
}

```

That will allow to adjust the score via individually boosting each query.

You can also use [RRF](https://www.elastic.co/guide/en/elasticsearch/reference/current/rrf.html) to combine the queries.

---

<div class="post-metadata">

**Author:** ![Bruce\_Mcpherson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_mcpherson/32/137850_2.png) [@Bruce\_Mcpherson](https://discuss.elastic.co/u/Bruce_Mcpherson)\
**Post date:** [September 25, 2024, 11:42am UTC](https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098/3 "2024-09-25T11:42:18Z")

</div>

Thanks for the response, Carlos.

That was the approach I used with the Python client and it worked fine. However with the Node client, I get this error

> illegal\_argument\_exception: [knn] queries cannot be provided directly, use the [knn] body parameter instead

This is what led me supplying the knn as a top level body argument rather than as part of a query as described here.  
[multiple knn fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#:~:text=Search%20multiple%20kNN%20fields&text=This%20search%20finds%20the%20global,top%2D10%20documents%20are%20returned.)

I haven't come across rrf before (I see it's in preview) - but I'll take a look.

In the meantime, is there any reason you can think of that knn as a body argument with an array would not work as described returning this error

> Unknown key for a START\_ARRAY in [knn]

It does work with a single knn.

thank you

---

<div class="post-metadata">

**Author:** ![Alex\_Salgado-Elastic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_salgado-elastic/32/103081_2.png) [@Alex\_Salgado-Elastic](https://discuss.elastic.co/u/Alex_Salgado-Elastic)\
**Post date:** [September 25, 2024, 12:55pm UTC](https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098/4 "2024-09-25T12:55:16Z")

</div>

Hi @Bruce_Mcpherson , welcome to our community.

1- Can you share the exact error message you're receiving when trying to run the query with multiple `knn` fields? This will help identify if the issue is due to query formatting or something else.

2-Have you tried using a `bool.should` query to combine multiple `knn` searches into a single request? This approach allows you to run multiple `knn` queries on different vector fields and combine their results.

---

<div class="post-metadata">

**Author:** ![Bruce\_Mcpherson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_mcpherson/32/137850_2.png) [@Bruce\_Mcpherson](https://discuss.elastic.co/u/Bruce_Mcpherson)\
**Post date:** [September 25, 2024, 1:27pm UTC](https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098/5 "2024-09-25T13:27:33Z")

</div>

Hi Alex

The message for bool/should was

> illegal\_argument\_exception: [knn] queries cannot be provided directly, use the [knn] body parameter instead

And for knn array method:

> Unknown key for a START\_ARRAY in [knn]

However - **it was all my bad**. I am running 8.15.0 client, but an earlier v8 version es server on kubernetes. I've just upgraded my kube server to 8.15.0 to match the client. Both methods now work!

Thank you both for your excellent and prompt responses.

---

<div class="post-metadata">

**Author:** ![Sean\_Story](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sean_story/32/69987_2.png) [@Sean\_Story](https://discuss.elastic.co/u/Sean_Story)\
**Post date:** [September 25, 2024, 1:28pm UTC](https://discuss.elastic.co/t/how-to-create-a-search-request-with-multiple-knn-fields-using-the-node-client/367098/6 "2024-09-25T13:28:07Z")

</div>

Added #language-clients
