Skip to content

Make the exception obvious when caused by knn dimensions mismatch #1333

Closed

Description

@pioorg

Description

Elasticsearch client, 9.5.x
When there's a knn query, and the number dimensions of query embedding doesn't match what's in the index, the exception is not obvious to understand. It would help, if it could be obvious.

Example request:

SearchResponse<CatalogueItem> response = esClient.search(s -> s
                    .index(indexName)
                    .knn(knn -> knn
                        .field("embedding")
                        .queryVector(queryVector)
                        .k(k)
                        .numCandidates(numCandidates)
                    )
                    // Also set the size of the search response to k
                    .size(k)
                    .source(src -> src.filter(f -> f.includes("filename", "full-path", "price"))),
                CatalogueItem.class);

The actual exception:

Caused by: co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
	at co.elastic.clients.transport.ElasticsearchTransportBase.getApiResponse(ElasticsearchTransportBase.java:421)
	at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:204)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:5207)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:5250)

When debugging and examining the error in ElasticsearchTransportBase:419, one can easily see:

{
  "error": {
    "phase": "dfs",
    "failed_shards": [
      {
        "shard": 0,
        "index": "catalogue",
        "node": "geWMIwPdScSqIlebJP7eCw",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: The query vector has a different number of dimensions [100] than the document vectors [768].",
          "index_uuid": "h9wJXp-ES6WE3ihk-qsKIQ",
          "index": "catalogue",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "The query vector has a different number of dimensions [100] than the document vectors [768]."
          }
        }
      }
    ],
    "grouped": true,
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "root_cause": [
      {
        "index_uuid": "h9wJXp-ES6WE3ihk-qsKIQ",
        "index": "catalogue",
        "type": "query_shard_exception",
        "reason": "failed to create query: The query vector has a different number of dimensions [100] than the document vectors [768]."
      }
    ]
  },
  "status": 400
}

IMHO it would help a great deal if the exception had the root_cause too.

Activity

  1. l-trotta commented on Sep 4, 2026

    @l-trotta
    Contributor

    Thanks for making me aware of this! I've added all nested root causes to the main error message in #1335, it will be in the next patch. A failing Knn query such at this one will output:

    [es/search] failed: [search_phase_execution_exception] all shards failed; root cause: [query_shard_exception] failed to create query: The query vector has a different number of dimensions [2] than the document vectors [3].
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions