From 73fd33242e05fa511d2461a1d24442797d94f58b Mon Sep 17 00:00:00 2001 From: Florian Bernd Date: Tue, 17 Mar 2026 14:34:07 +0100 Subject: [PATCH] Fix regression involving `BulkRequest` per-request index (#8855) (cherry picked from commit ff7b3cb07ff882ee29af9f4a0c2b3651763c333b) --- .../_Shared/Types/Core/Bulk/BulkOperation.cs | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Elastic.Clients.Elasticsearch/_Shared/Types/Core/Bulk/BulkOperation.cs b/src/Elastic.Clients.Elasticsearch/_Shared/Types/Core/Bulk/BulkOperation.cs index 8fedfa07aec..5310d56b6e3 100644 --- a/src/Elastic.Clients.Elasticsearch/_Shared/Types/Core/Bulk/BulkOperation.cs +++ b/src/Elastic.Clients.Elasticsearch/_Shared/Types/Core/Bulk/BulkOperation.cs @@ -112,27 +112,33 @@ void IBulkOperation.PrepareIndex(IndexName? bulkRequestIndex) private void ResolveIndex(IElasticsearchClientSettings settings) { - if (_bulkRequestIndex is not null) - { - var resolvedBulkIndex = settings.Inferrer.IndexName(_bulkRequestIndex); - - string? inferredIndex; - if (Index is not null) - inferredIndex = settings.Inferrer.IndexName(Index); - else if (ClrType is not null) - inferredIndex = settings.Inferrer.TryIndexName(ClrType); - else - inferredIndex = null; - - if (inferredIndex is null || inferredIndex == resolvedBulkIndex) - Index = null; - else - Index = inferredIndex; - } - else + // Index resolution follows a strict hierarchy: + // 1. Explicit per-operation Index (highest priority) + // 2. Bulk request URL-level index + // 3. CLR type inference via DefaultMappingFor / DefaultIndex (lowest priority) + + // When no bulk request index exists, fall back to CLR type inference + // so the operation can resolve its index from settings (DefaultMappingFor / DefaultIndex). + if (_bulkRequestIndex is null) { Index ??= ClrType; + return; } + + // When a bulk request index is set but the operation has no explicit index, + // the bulk request index takes precedence — leave Index null so the + // URL-level index applies. Do not infer from ClrType here, as that would + // allow DefaultIndex to override the explicit bulk request index. + if (Index is null) + return; + + // When the operation has an explicit index, resolve both to compare. + // If they match, clear the per-operation index to avoid redundancy in the + // serialized body. If they differ, keep the per-operation override. + var resolvedBulkIndex = settings.Inferrer.IndexName(_bulkRequestIndex); + var resolvedIndex = settings.Inferrer.IndexName(Index); + + Index = resolvedIndex == resolvedBulkIndex ? null : resolvedIndex; } ///