diff --git a/src/main/java/org/elasticsearch/common/lucene/document/SingleFieldVisitor.java b/src/main/java/org/elasticsearch/common/lucene/document/SingleFieldVisitor.java index bd829ffa7ee..de3f92cef3a 100644 --- a/src/main/java/org/elasticsearch/common/lucene/document/SingleFieldVisitor.java +++ b/src/main/java/org/elasticsearch/common/lucene/document/SingleFieldVisitor.java @@ -30,10 +30,9 @@ import java.util.List; /** * */ -public class SingleFieldVisitor extends BaseFieldVisitor { +public class SingleFieldVisitor extends AbstractMultipleFieldsVisitor { private String name; - private List values; public SingleFieldVisitor() { } @@ -46,44 +45,11 @@ public class SingleFieldVisitor extends BaseFieldVisitor { this.name = name; } - @Override - public Document createDocument() { - Document document = new Document(); - for (String value : values) { - document.add(new StoredField(name, value)); - } - return document; - } - - @Override - public void reset() { - values = null; - } - - public String value() { - return values.get(0); - } - - public List values() { - return values; - } - @Override public Status needsField(FieldInfo fieldInfo) throws IOException { if (name.equals(fieldInfo.name)) { return Status.YES; } - - return values != null ? Status.STOP : Status.NO; - } - - @Override - public void stringField(FieldInfo fieldInfo, String value) throws IOException { - if (fieldInfo.name.equals(name)) { - if (values == null) { - values = new ArrayList(); - } - values.add(value); - } + return Status.NO; } } diff --git a/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java b/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java index 74e0105916b..48c1515be74 100644 --- a/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java +++ b/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java @@ -23,6 +23,8 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexableField; import org.apache.lucene.search.Query; import org.apache.lucene.search.highlight.*; import org.apache.lucene.search.highlight.Formatter; @@ -169,7 +171,13 @@ public class HighlightPhase extends AbstractComponent implements FetchSubPhase { try { SingleFieldVisitor fieldVisitor = new SingleFieldVisitor(mapper.names().indexName()); hitContext.reader().document(hitContext.docId(), fieldVisitor); - textsToHighlight = (List) fieldVisitor.values(); + Document doc = fieldVisitor.createDocument(); + textsToHighlight = new ArrayList(doc.getFields().size()); + for (IndexableField docField : doc.getFields()) { + if (docField.stringValue() != null) { + textsToHighlight.add(docField.stringValue()); + } + } } catch (Exception e) { throw new FetchPhaseExecutionException(context, "Failed to highlight field [" + field.field() + "]", e); } diff --git a/src/main/java/org/elasticsearch/search/lookup/FieldsLookup.java b/src/main/java/org/elasticsearch/search/lookup/FieldsLookup.java index 9175dd1e69c..543c7e6b8dc 100644 --- a/src/main/java/org/elasticsearch/search/lookup/FieldsLookup.java +++ b/src/main/java/org/elasticsearch/search/lookup/FieldsLookup.java @@ -159,6 +159,8 @@ public class FieldsLookup implements Map { data.doc(fieldVisitor.createDocument()); } catch (IOException e) { throw new ElasticSearchParseException("failed to load field [" + name + "]", e); + } finally { + fieldVisitor.reset(); } } return data;