From 9b093bdbc90d0342c46bf530adb527f8ec970a5e Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Thu, 19 Dec 2024 12:52:55 +0000 Subject: [PATCH] handle null mapping --- .../elasticsearch/action/bulk/TransportShardBulkAction.java | 2 +- .../elasticsearch/action/update/TransportUpdateAction.java | 2 +- .../index/mapper/InferenceMetadataFieldsMapper.java | 6 +++--- .../org/elasticsearch/index/mapper/SourceFieldMapper.java | 2 +- .../search/fetch/subphase/FetchSourcePhase.java | 4 +--- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java b/server/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java index 86a5878a78d3b..89cee714a9ff2 100644 --- a/server/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java +++ b/server/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java @@ -405,7 +405,7 @@ static boolean executeBulkItemRequest( } private static String[] getStoredFieldsSpec(IndexShard indexShard) { - if (InferenceMetadataFieldsMapper.isEnabled(indexShard.mapperService().mappingLookup().getMapping())) { + if (InferenceMetadataFieldsMapper.isEnabled(indexShard.mapperService().mappingLookup())) { if (indexShard.mapperService().mappingLookup().inferenceFields().size() > 0) { // Retrieves the inference metadata field containing the inference results for all semantic fields defined in the mapping. return new String[] { RoutingFieldMapper.NAME, InferenceMetadataFieldsMapper.NAME }; diff --git a/server/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java b/server/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java index 0baa39aeb1497..7d54cb5eee77b 100644 --- a/server/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java +++ b/server/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java @@ -376,7 +376,7 @@ private static UpdateHelper.Result deleteInferenceResults( MappingLookup mappingLookup ) { if (result.getResponseResult() != DocWriteResponse.Result.UPDATED - || InferenceMetadataFieldsMapper.isEnabled(mappingLookup.getMapping())) { + || InferenceMetadataFieldsMapper.isEnabled(mappingLookup)) { return result; } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/InferenceMetadataFieldsMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/InferenceMetadataFieldsMapper.java index 99870036108c4..bd5c173f53175 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/InferenceMetadataFieldsMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/InferenceMetadataFieldsMapper.java @@ -96,10 +96,10 @@ public static boolean isEnabled(Settings settings) { * This indicates whether the new format for semantic text fields is active by verifying the existence * of the {@link InferenceMetadataFieldsMapper} in the mapping's metadata. * - * @param mapping the mapping to evaluate + * @param mappingLookup the mapping to evaluate * @return {@code true} if the {@link InferenceMetadataFieldsMapper} is present; {@code false} otherwise */ - public static boolean isEnabled(Mapping mapping) { - return mapping.getMetadataMapperByName(InferenceMetadataFieldsMapper.NAME) != null; + public static boolean isEnabled(MappingLookup mappingLookup) { + return mappingLookup != null && mappingLookup.getMapping().getMetadataMapperByName(InferenceMetadataFieldsMapper.NAME) != null; } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java index f53c3691d2f41..876545834606a 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java @@ -443,7 +443,7 @@ public BytesReference applyFilters( } var modSourceFilter = sourceFilter; if (context != null - && InferenceMetadataFieldsMapper.isEnabled(context.mappingLookup().getMapping()) + && InferenceMetadataFieldsMapper.isEnabled(context.mappingLookup()) && context.mappingLookup().inferenceFields().isEmpty() == false) { String[] modExcludes = new String[excludes != null ? excludes.length + 1 : 1]; if (excludes != null) { diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java index 0d955ab997618..da2d6577ca743 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java @@ -90,9 +90,7 @@ private void hitExecute(HitContext hitContext) { * to the original _source if it has been requested. */ private Source replaceInferenceMetadataFields(SearchHit hit, Source source) { - if (InferenceMetadataFieldsMapper.isEnabled( - fetchContext.getSearchExecutionContext().getMappingLookup().getMapping() - ) == false) { + if (InferenceMetadataFieldsMapper.isEnabled(fetchContext.getSearchExecutionContext().getMappingLookup()) == false) { return source; }