-
Couldn't load subscription status.
- Fork 1.2k
Description
Elasticsearch .NET Client Issue Report
Elastic.Clients.Elasticsearch version: 8.17.1
Elasticsearch version: 8.15.3
.NET runtime version: 9.0
Operating system version: Windows
Description of the problem
When sorting by a numeric field that has inconsistent data types across shards (FLOAT vs DOUBLE), the query executes successfully when run directly in Elasticsearch console but fails when executed through the .NET client with the error:
the field has incompatible sort types: [FLOAT] and [DOUBLE] across shards!
The same query, same data, same mapping, but executed through different interfaces produce different behaviors.
Steps to reproduce
- Create an index with a numeric field that has mixed FLOAT/DOUBLE types across shards
- Execute a search query with sorting on the numeric field using the .NET client:
var searchResult = await elasticClient.SearchAsync<HouseHoldRecord>(c =>
{
c.Query(BuildSearch(options));
c.Sort(s => s.Field(f => f.LastReportedRevenue!, new FieldSort { Order = SortOrder.Desc }));
c.Size(options.PageSize);
c.From((options.PageIndex - 1) * options.PageSize);
c.TrackTotalHits(new TrackHits(true));
});- The query fails with shard incompatibility error
Expected behavior
The query should work consistently between direct Elasticsearch console execution and .NET client execution, or provide better handling of mixed numeric types across shards.
Console Query (Works)
POST /my-index/_search
{
"from": 0,
"query": {
"bool": {
"boost": 2,
"must": {
"match": {
"taxCode.keyword": {
"query": "1234567890"
}
}
},
"_name": "SearchRecord"
}
},
"size": 30,
"sort": {
"lastReportedRevenue": {
"order": "desc"
}
},
"track_total_hits": true
}.NET Client Query (Fails)
var searchRequest = new SearchRequestDescriptor<MyRecord>()
.Query(q => q.Term(t => t.Field("taxCode.keyword").Value("1234567890")))
.Sort(s => s.Field("lastReportedRevenue", new FieldSort { Order = SortOrder.Desc }))
.Size(30)
.TrackTotalHits(new TrackHits(true));
var response = await elasticClient.SearchAsync(searchRequest);Connection Settings
var settings = new ElasticsearchClientSettings(new Uri("https://localhost:9200"))
.DefaultIndex("my-index");
var elasticClient = new ElasticsearchClient(settings);Workaround Found
Using script-based sorting resolves the issue:
c.Sort(s => s.Script(new ScriptSort
{
Script = new InlineScript("Math.round(doc['lastReportedRevenue'].value * 100) / 100.0")
{
Lang = "painless"
},
Type = "number",
Order = SortOrder.Desc
}));Alternative Solutions Attempted
- Using UnmappedType:
c.Sort(s => s.Field("lastReportedRevenue", new FieldSort
{
Order = SortOrder.Desc,
UnmappedType = "double"
}));- Using Expression Script:
c.Sort(s => s.Script(new ScriptSort
{
Script = new InlineScript("doc['lastReportedRevenue'].value")
{
Lang = "expression"
},
Type = "number",
Order = SortOrder.Desc
}));Additional Context
- This issue appears to be related to how the .NET client handles field sorting when there are type inconsistencies across shards
- Direct Elasticsearch queries handle this scenario more gracefully
- The issue may be version-specific as different Elasticsearch versions (8.15.1 vs 8.16.1) show different behaviors
- Script-based sorting works as a workaround but impacts performance
Debug Information
Error occurs during query execution, not during client initialization. The field mapping shows mixed types across different shards which causes the sorting conflict in the .NET client but not in direct console queries.
Environment Details
- Framework: .NET 9.0
- Client: Elastic.Clients.Elasticsearch 8.17.1
- Server: Elasticsearch 8.15.3
- OS: Windows
- Deployment: Multi-shard setup with type inconsistencies