Skip to content

Sorting in collapse on incompatible field types across shards returns 500 #127965

@mayya-sharipova

Description

@mayya-sharipova

Elasticsearch Version

9.0.1

Installed Plugins

No response

Java Version

bundled

OS Version

All

Problem Description

Sorting in collapse on incompatible field types across shards returns 500.

If there are shards in a search request that have fields indexed on incompatible types (e.g. on index a field is indexed as float and another as long), when doing sort on these fields in a collapse query, ES returns 500 error. While it should return 400x error as it is a user error, we don't allow sorting on incompatible field types.

Steps to Reproduce

PUT /my_store_inventory_long
{
  "mappings": {
    "properties": {
      "item_id": { "type": "keyword" },
      "product_name": { "type": "text" },
      "brand_id": { "type": "long" },  // brand_id is long in this index
      "price": { "type": "float" },
      "timestamp": { "type": "date" }
    }
  }
}

PUT /my_store_inventory_float
{
  "mappings": {
    "properties": {
      "item_id": { "type": "keyword" },
      "product_name": { "type": "text" },
      "brand_id": { "type": "float" }, // brand_id is float in this index
      "price": { "type": "float" },
      "timestamp": { "type": "date" }
    }
  }
}


POST my_store_inventory_long/_bulk
{ "index": { "_id": "1" } }
{ "item_id": "item_A1", "product_name": "Super Widget X", "brand_id": 101, "price": 10.99, "timestamp": "2025-05-01T10:00:00Z" }

POST my_store_inventory_float/_bulk
{ "index": { "_id": "1" } }
{ "item_id": "item_A2", "product_name": "Basic Widget", "brand_id": 101, "price": 8.99, "timestamp": "2025-05-02T11:00:00Z" }


GET my_store_inventory*/_search?error_trace
{
  "query": {
    "match_all": {} 
  },
  "collapse": {
    "field": "brand_id" 
  },
  "sort": [
    { "brand_id": "asc" }, 
    { "price": "desc" }    
  ]
}

returns 500:

"caused_by": {
      "type": "class_cast_exception",
      "reason": "class java.lang.Long cannot be cast to class java.lang.Float (java.lang.Long and java.lang.Float are in module java.base of loader 'bootstrap')",
      "stack_trace": """java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Float (java.lang.Long and java.lang.Float are in module java.base of loader 'bootstrap')
	at java.base/java.lang.Float.compareTo(Float.java:78)
	at [email protected]/org.apache.lucene.search.FieldComparator.compareValues(FieldComparator.java:115)
	at [email protected]/org.elasticsearch.lucene.grouping.TopFieldGroups$MergeSortQueue.lessThan(TopFieldGroups.java:143)
	at [email protected]/org.elasticsearch.lucene.grouping.TopFieldGroups$MergeSortQueue.lessThan(TopFieldGroups.java:99)
	at [email protected]/org.apache.lucene.util.PriorityQueue.upHeap(PriorityQueue.java:277)

Logs (if relevant)

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions