From 35c339f5f3ce8142b27c8e204342f8ec188b47db Mon Sep 17 00:00:00 2001 From: Alberto Paro Date: Thu, 10 Feb 2011 14:19:26 +0100 Subject: [PATCH] Removed empty fragments in highlight results --- .../search/highlight/HighlightPhase.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java index 56d5edfc763..6bf540ec231 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java @@ -148,8 +148,10 @@ public class HighlightPhase implements SearchHitPhase { for (int i = 0; i < fragments.length; i++) { fragments[i] = fragsList.get(i).toString(); } - HighlightField highlightField = new HighlightField(field.field(), fragments); - highlightFields.put(highlightField.name(), highlightField); + if (fragments.length>0){ + HighlightField highlightField = new HighlightField(field.field(), fragments); + highlightFields.put(highlightField.name(), highlightField); + } } else { FastVectorHighlighter highlighter = buildHighlighter(context, mapper, field); FieldQuery fieldQuery = buildFieldQuery(highlighter, context.query(), hitContext.reader(), field); @@ -162,8 +164,10 @@ public class HighlightPhase implements SearchHitPhase { } catch (IOException e) { throw new FetchPhaseExecutionException(context, "Failed to highlight field [" + field.field() + "]", e); } - HighlightField highlightField = new HighlightField(field.field(), fragments); - highlightFields.put(highlightField.name(), highlightField); + if (fragments.length>0){ + HighlightField highlightField = new HighlightField(field.field(), fragments); + highlightFields.put(highlightField.name(), highlightField); + } } }