Not able to get more than 10 results in ANN search using ES 8.5.0 docker image

Hi team,

I am using the Elasticsearch 8.5.0 docker image (installed and started as explained here) to do some experimenting with ANN search.
I followed this guide to perform approximate KNN search and use a POST command similar to the following one to perform a query:

POST image-index/_search
{
  "knn": {
    "field": "image-vector",
    "query_vector": [-5, 9, -12],
    "k": 10,
    "num_candidates": 100
  },
  "fields": [ "title", "file-type" ]
}

My problem is that the parameter k (number of retrieved vectors) seems to be limited to 10: if I decrease it it works well, showing less than 10 results, but as soon as I try to increase it past 10 (for example to 15), it still shows only 10 results.
Is this a limitation of the docker image or did I skip some important configurations?

Thank you very much

Hi @wole , you can think of the 'knn' section as adding 'k' additional matching documents to the search. The default 'size' for the search is still 10, which selects the 10 top-scoring hits. So if you want more than 'k' results returned, you need to adjust the overall 'size' parameter too:

{
  "knn": {
    "field": "image-vector",
    "query_vector": [-5, 9, -12],
    "k": 50,
    "num_candidates": 100
  },
  "fields": [ "title", "file-type" ],
  "size": 50
}

I realize this is a bit confusing when you're only using kNN-only. But it fits with how the search API works, and is convenient when you're combining both 'knn' and 'query' sections.

thank you very much, it works now!

Paginate search results | Elasticsearch Guide [8.5] | Elastic explains it more.

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