# KNN search fails on empty index

**URL:** <https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [October 11, 2024, 8:32am UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670 "2024-10-11T08:32:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JornWildt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jornwildt/32/134845_2.png) [@JornWildt](https://discuss.elastic.co/u/JornWildt)\
**Post date:** [October 11, 2024, 8:32am UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/1 "2024-10-11T08:32:12Z")

</div>

Doing a KNN search on an empty index fails with "All shards failed".

A normal search with a GET like this succeeds with zero its:

```
https://host/my-index/_search

```

But if I do a POST like this, ES fails with status code 400 "All shards failed":

```auto
{
  "fields": [
    {
      "field": "text"
    }
  ],
  "knn": {
    "field": "vectorIndex",
    "k": 13,
    "num_candidates": 26,
    "query_vector": [1,2,3,4,5,6,7,...],
    "similarity": -0.24000001
  },
  "size": 13
}

```

I would expect the KNN search to succeed with status code 200 and zero hits.

As it is now, the application using ES returns "error" to the end user though it should return "No results" because of the status code 400.

Is there any way to avoid this? Is it a bug or a feature?

---

<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:** [October 11, 2024, 8:48am UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/2 "2024-10-11T08:48:38Z")

</div>

What if you run the `knn` part as a query instead?

```auto
GET /_search
{
  "query": {
    "knn": {
      // ...
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![JornWildt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jornwildt/32/134845_2.png) [@JornWildt](https://discuss.elastic.co/u/JornWildt)\
**Post date:** [October 11, 2024, 12:01pm UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/3 "2024-10-11T12:01:18Z")

</div>

Same result with GET:

```auto
GET https://HOST/vector-index/_search HTTP/1.1
Host: HOST
Accept: application/vnd.elasticsearch+json;compatible-with=8
User-Agent: elasticsearch-net/8.15.2+974651520349bd64e6c4755da6f8eed1a4bd6b52 (Microsoft Windows 10.0.22631; .NET 8.0.10; Elastic.Clients.Elasticsearch)
x-elastic-client-meta: es=8.15.2,a=1,net=8.0.10,so=8.0.10,t=0.4.22+b65498cbdc63d96c1653577b4706b74bf3c20179
Authorization: ApiKey XXX
traceparent: 00-42a5813a14052c77129faf3873ca446e-bd585f06235aad38-00
Content-Type: application/vnd.elasticsearch+json;compatible-with=8
Content-Length: 9656

{"fields":[{"field":"text"}],"knn":{"field":"vectorIndex","k":19,"num_candidates":38,"query_vector":[...],"similarity":-0.25},"size":19}

```

Output:

```auto
HTTP/1.1 400 Bad Request
Server: openresty
Date: Fri, 11 Oct 2024 11:58:17 GMT
Content-Type: application/vnd.elasticsearch+json;compatible-with=8
Content-Length: 857
Connection: keep-alive
X-elastic-product: Elasticsearch

{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: Cannot invoke \"java.lang.Integer.intValue()\" because \"this.dims\" is null","index_uuid":"KgM1GR66RN6xl7zInIBj4w","index":"f2dev12-expert-environment-chunk-index"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"dfs","grouped":true,"failed_shards":[{"shard":0,"index":"vector-index","node":"85NOPUP4QsWu31m9ieaOng","reason":{"type":"query_shard_exception","reason":"failed to create query: Cannot invoke \"java.lang.Integer.intValue()\" because \"this.dims\" is null","index_uuid":"KgM1GR66RN6xl7zInIBj4w","index":"vector-index","caused_by":{"type":"null_pointer_exception","reason":"Cannot invoke \"java.lang.Integer.intValue()\" because \"this.dims\" is null"}}}]},"status":400}

```

---

<div class="post-metadata">

**Author:** ![JornWildt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jornwildt/32/134845_2.png) [@JornWildt](https://discuss.elastic.co/u/JornWildt)\
**Post date:** [October 11, 2024, 12:02pm UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/4 "2024-10-11T12:02:04Z")

</div>

But "GET"? Really? You are not supposed to pass any HTTP body to a GET request.

---

<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:** [October 11, 2024, 12:13pm UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/5 "2024-10-11T12:13:49Z")

</div>

What version are you using @JornWildt ? stack trace indicates this is a bug for sure.

EDIT:

Found the issue & the PR that fixes it: [vector search error when the target index is empty · Issue #111733 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/111733)

> <https://github.com/elastic/elasticsearch/pull/111756>
>
> Resolves https://github.com/elastic/elasticsearch/issues/111733 
> 
> Script to re…produce (slightly expanded from bug report issue): 
> \`\`\`
> DELETE esre-team-test-chunks
> 
> PUT esre-team-test-chunks
> {
> "mappings": {
> "dynamic\_templates": \[\],
> "properties": {
> "metadata": {
> "properties": {
> "esre": {
> "properties": {
> "created\_at": {
> "type": "date"
> },
> "embedding\_model": {
> "type": "text"
> },
> "file\_id": {
> "type": "text"
> },
> "team\_id": {
> "type": "text"
> },
> "updated\_at": {
> "type": "date"
> }
> }
> },
> "source": {
> "type": "text",
> "fields": {
> "keyword": {
> "type": "keyword",
> "ignore\_above": 256
> }
> }
> }
> }
> },
> "text": {
> "type": "text",
> "fields": {
> "ngram": {
> "type": "text"
> }
> }
> },
> "text\_embedding": {
> "type": "dense\_vector",
> "index": true,
> "similarity": "cosine"
> }
> }
> }
> }
> 
> 
> PUT \_inference/text\_embedding/my-e5-model
> {
> "service": "elasticsearch",
> "service\_settings": {
> "num\_allocations": 1,
> "num\_threads": 1,
> "model\_id": ".multilingual-e5-small"
> }
> }
> 
> GET /esre-team-test-chunks/\_search
> {
> "fields": \[
> "text"
> \],
> "knn": {
> "field": "text\_embedding",
> "k": 10,
> "num\_candidates": 100,
> "query\_vector\_builder": {
> "text\_embedding": {
> "model\_id": ".multilingual-e5-small",
> "model\_text": "test"
> }
> }
> }
> }
> 
> GET /esre-team-test-chunks/\_search
> {
> "fields": \[
> "text"
> \],
> "knn": {
> "field": "text\_embedding",
> "k": 10,
> "num\_candidates": 100,
> "query\_vector": \[1.0, 2.0, 3.0 \]
> }
> }
> 
> \`\`\`

---

<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:** [October 11, 2024, 12:14pm UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/6 "2024-10-11T12:14:37Z")

</div>

> But "GET"? Really? You are not supposed to pass any HTTP body to a GET request.

ES has supported `GET` with a body for about a decade now, that is unlikely to change. But you can use `POST` if you are more comfortable with that 🙂

---

<div class="post-metadata">

**Author:** ![JornWildt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jornwildt/32/134845_2.png) [@JornWildt](https://discuss.elastic.co/u/JornWildt)\
**Post date:** [October 11, 2024, 12:33pm UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/7 "2024-10-11T12:33:07Z")

</div>

Very nice, thanks a lot.

---

<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:** [October 11, 2024, 12:47pm UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/8 "2024-10-11T12:47:28Z")

</div>

That's not the request I mentioned. Whatever `GET` or `POST` verb.

---

<div class="post-metadata">

**Author:** ![JornWildt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jornwildt/32/134845_2.png) [@JornWildt](https://discuss.elastic.co/u/JornWildt)\
**Post date:** [October 11, 2024, 1:07pm UTC](https://discuss.elastic.co/t/knn-search-fails-on-empty-index/368670/9 "2024-10-11T13:07:08Z")

</div>

Sorry, I misunderstood you. Anyway - it seems to be fixed in 8.15.1. See [Elasticsearch version 8.15.1 | Elasticsearch Guide [8.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/release-notes-8.15.1.html) (bottom).
