From 9709c61ed11bebc582b6f2e3409431c1bd948331 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Tue, 17 Jun 2025 19:17:01 +0100 Subject: [PATCH] Fix NPE in SemanticTextHighlighter (#129509) If the model settings is null then the field is guaranteed to not have any indexed content. Closes #129501 --- docs/changelog/129509.yaml | 6 ++++ .../highlight/SemanticTextHighlighter.java | 2 +- .../90_semantic_text_highlighter.yml | 19 ++++++++++++ .../90_semantic_text_highlighter_bwc.yml | 29 +++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 docs/changelog/129509.yaml diff --git a/docs/changelog/129509.yaml b/docs/changelog/129509.yaml new file mode 100644 index 0000000000000..859ad837a3c8d --- /dev/null +++ b/docs/changelog/129509.yaml @@ -0,0 +1,6 @@ +pr: 129509 +summary: Fix NPE in `SemanticTextHighlighter` +area: Search +type: bug +issues: + - 129501 diff --git a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/highlight/SemanticTextHighlighter.java b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/highlight/SemanticTextHighlighter.java index a277ddefb9590..92333a10c4d08 100644 --- a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/highlight/SemanticTextHighlighter.java +++ b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/highlight/SemanticTextHighlighter.java @@ -74,7 +74,7 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc return null; } SemanticTextFieldType fieldType = (SemanticTextFieldType) fieldContext.fieldType; - if (fieldType.getEmbeddingsField() == null) { + if (fieldType.getModelSettings() == null || fieldType.getEmbeddingsField() == null) { // nothing indexed yet return null; } diff --git a/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter.yml b/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter.yml index a25a1464a7226..021dfe320d78e 100644 --- a/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter.yml +++ b/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter.yml @@ -60,6 +60,9 @@ setup: body: type: semantic_text inference_id: dense-inference-id + another_body: + type: semantic_text + inference_id: dense-inference-id - do: index: @@ -79,6 +82,22 @@ setup: body: [ "ElasticSearch is an open source, distributed, RESTful, search engine which is built on top of Lucene internally and enjoys all the features it provides.", "You Know, for Search!" ] refresh: true +--- +"Highlighting empty field": + - do: + search: + index: test-dense-index + body: + query: + match_all: {} + highlight: + fields: + another_body: {} + + - match: { hits.total.value: 1 } + - match: { hits.hits.0._id: "doc_1" } + - not_exists: hits.hits.0.highlight.another_body + --- "Highlighting using a sparse embedding model": - do: diff --git a/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter_bwc.yml b/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter_bwc.yml index 6fcb1bb5a31b3..1e874d60a016c 100644 --- a/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter_bwc.yml +++ b/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/90_semantic_text_highlighter_bwc.yml @@ -58,6 +58,35 @@ setup: body: type: semantic_text inference_id: dense-inference-id + another_body: + type: semantic_text + inference_id: dense-inference-id + +--- +"Highlighting empty field": + - do: + index: + index: test-dense-index + id: doc_1 + body: + body: [ "ElasticSearch is an open source, distributed, RESTful, search engine which is built on top of Lucene internally and enjoys all the features it provides.", "You Know, for Search!" ] + refresh: true + + - match: { result: created } + + - do: + search: + index: test-dense-index + body: + query: + match_all: {} + highlight: + fields: + another_body: {} + + - match: { hits.total.value: 1 } + - match: { hits.hits.0._id: "doc_1" } + - not_exists: hits.hits.0.highlight.another_body --- "Highlighting using a sparse embedding model":