diff --git a/src/main/java/org/elasticsearch/common/lucene/document/MultipleFieldsVisitor.java b/src/main/java/org/elasticsearch/common/lucene/document/AbstractMultipleFieldsVisitor.java similarity index 64% rename from src/main/java/org/elasticsearch/common/lucene/document/MultipleFieldsVisitor.java rename to src/main/java/org/elasticsearch/common/lucene/document/AbstractMultipleFieldsVisitor.java index 21fd6f5006a..df76014bc74 100644 --- a/src/main/java/org/elasticsearch/common/lucene/document/MultipleFieldsVisitor.java +++ b/src/main/java/org/elasticsearch/common/lucene/document/AbstractMultipleFieldsVisitor.java @@ -10,28 +10,9 @@ import java.util.Set; /** * */ -public class MultipleFieldsVisitor extends BaseFieldVisitor { +public abstract class AbstractMultipleFieldsVisitor extends BaseFieldVisitor { protected Document doc = new Document(); - protected final Set fieldsToAdd; - - /** Load only fields named in the provided Set<String>. */ - public MultipleFieldsVisitor(Set fieldsToAdd) { - this.fieldsToAdd = fieldsToAdd; - } - - /** Load only fields named in the provided Set<String>. */ - public MultipleFieldsVisitor(String... fields) { - fieldsToAdd = new HashSet(fields.length); - for(String field : fields) { - fieldsToAdd.add(field); - } - } - - /** Load all stored fields. */ - public MultipleFieldsVisitor() { - this.fieldsToAdd = null; - } @Override public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException { @@ -68,13 +49,15 @@ public class MultipleFieldsVisitor extends BaseFieldVisitor { doc.add(new StoredField(fieldInfo.name, value)); } - @Override - public Status needsField(FieldInfo fieldInfo) throws IOException { - return fieldsToAdd == null || fieldsToAdd.contains(fieldInfo.name) ? Status.YES : Status.NO; - } - @Override public Document createDocument() { return doc; } + + @Override + public void reset() { + if (!doc.getFields().isEmpty()) { + doc = new Document(); + } + } } diff --git a/src/main/java/org/elasticsearch/common/lucene/document/BaseFieldVisitor.java b/src/main/java/org/elasticsearch/common/lucene/document/BaseFieldVisitor.java index a86ce265632..8044de316e9 100644 --- a/src/main/java/org/elasticsearch/common/lucene/document/BaseFieldVisitor.java +++ b/src/main/java/org/elasticsearch/common/lucene/document/BaseFieldVisitor.java @@ -8,4 +8,7 @@ public abstract class BaseFieldVisitor extends StoredFieldVisitor { // LUCENE 4 UPGRADE: Added for now to make everything work. Want to make use of Document as less as possible. public abstract Document createDocument(); + // LUCENE 4 UPGRADE: Added for now for compatibility with Selectors + public abstract void reset(); + } 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 09ff642c8b6..bd829ffa7ee 100644 --- a/src/main/java/org/elasticsearch/common/lucene/document/SingleFieldVisitor.java +++ b/src/main/java/org/elasticsearch/common/lucene/document/SingleFieldVisitor.java @@ -55,6 +55,11 @@ public class SingleFieldVisitor extends BaseFieldVisitor { return document; } + @Override + public void reset() { + values = null; + } + public String value() { return values.get(0); } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldVisitor.java b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldVisitor.java index 4b378acdf3a..907e4a7f208 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldVisitor.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldVisitor.java @@ -54,6 +54,11 @@ public class SourceFieldVisitor extends BaseFieldVisitor { return document; } + @Override + public void reset() { + source = null; + } + public BytesRef source() { return source; } diff --git a/src/main/java/org/elasticsearch/index/mapper/selector/AllButSourceFieldVisitor.java b/src/main/java/org/elasticsearch/index/mapper/selector/AllButSourceFieldVisitor.java index c586c7b4094..ef89bf897a0 100644 --- a/src/main/java/org/elasticsearch/index/mapper/selector/AllButSourceFieldVisitor.java +++ b/src/main/java/org/elasticsearch/index/mapper/selector/AllButSourceFieldVisitor.java @@ -20,7 +20,7 @@ package org.elasticsearch.index.mapper.selector; import org.apache.lucene.index.FieldInfo; -import org.elasticsearch.common.lucene.document.MultipleFieldsVisitor; +import org.elasticsearch.common.lucene.document.AbstractMultipleFieldsVisitor; import org.elasticsearch.index.mapper.internal.SourceFieldMapper; import java.io.IOException; @@ -28,14 +28,14 @@ import java.io.IOException; /** * A field selector that loads all fields except the source field. */ -public class AllButSourceFieldVisitor extends MultipleFieldsVisitor { +public class AllButSourceFieldVisitor extends AbstractMultipleFieldsVisitor { @Override public Status needsField(FieldInfo fieldInfo) throws IOException { if (SourceFieldMapper.NAME.equals(fieldInfo.name)) { return Status.NO; } - return super.needsField(fieldInfo); + return Status.YES; } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/selector/FieldMappersFieldVisitor.java b/src/main/java/org/elasticsearch/index/mapper/selector/FieldMappersFieldVisitor.java index 3661167fec2..87ffa34fbd5 100644 --- a/src/main/java/org/elasticsearch/index/mapper/selector/FieldMappersFieldVisitor.java +++ b/src/main/java/org/elasticsearch/index/mapper/selector/FieldMappersFieldVisitor.java @@ -19,14 +19,21 @@ package org.elasticsearch.index.mapper.selector; -import org.elasticsearch.common.lucene.document.MultipleFieldsVisitor; +import org.apache.lucene.index.FieldInfo; +import org.elasticsearch.common.lucene.document.AbstractMultipleFieldsVisitor; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMappers; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + /** * */ -public class FieldMappersFieldVisitor extends MultipleFieldsVisitor { +public class FieldMappersFieldVisitor extends AbstractMultipleFieldsVisitor { + + protected final Set fieldsToAdd = new HashSet(); public void add(String fieldName) { fieldsToAdd.add(fieldName); @@ -38,6 +45,11 @@ public class FieldMappersFieldVisitor extends MultipleFieldsVisitor { } } + @Override + public Status needsField(FieldInfo fieldInfo) throws IOException { + return fieldsToAdd.contains(fieldInfo.name) ? Status.YES : Status.NO; + } + @Override public String toString() { return "fields(" + fieldsToAdd + ")"; diff --git a/src/main/java/org/elasticsearch/index/mapper/selector/UidAndRoutingFieldVisitor.java b/src/main/java/org/elasticsearch/index/mapper/selector/UidAndRoutingFieldVisitor.java index f037412d1e1..6a75d1efd8d 100644 --- a/src/main/java/org/elasticsearch/index/mapper/selector/UidAndRoutingFieldVisitor.java +++ b/src/main/java/org/elasticsearch/index/mapper/selector/UidAndRoutingFieldVisitor.java @@ -45,6 +45,12 @@ public class UidAndRoutingFieldVisitor extends BaseFieldVisitor { return document; } + @Override + public void reset() { + uid = null; + routing = null; + } + @Override public Status needsField(FieldInfo fieldInfo) throws IOException { if (RoutingFieldMapper.NAME.equals(fieldInfo.name)) { diff --git a/src/main/java/org/elasticsearch/index/mapper/selector/UidAndSourceFieldVisitor.java b/src/main/java/org/elasticsearch/index/mapper/selector/UidAndSourceFieldVisitor.java index e5431b4770d..fab029d7759 100644 --- a/src/main/java/org/elasticsearch/index/mapper/selector/UidAndSourceFieldVisitor.java +++ b/src/main/java/org/elasticsearch/index/mapper/selector/UidAndSourceFieldVisitor.java @@ -45,6 +45,12 @@ public class UidAndSourceFieldVisitor extends BaseFieldVisitor { return document; } + @Override + public void reset() { + source = null; + uid = null; + } + @Override public Status needsField(FieldInfo fieldInfo) throws IOException { if (SourceFieldMapper.NAME.equals(fieldInfo.name)) { diff --git a/src/main/java/org/elasticsearch/index/mapper/selector/UidFieldVisitor.java b/src/main/java/org/elasticsearch/index/mapper/selector/UidFieldVisitor.java index e3dc0fe5cf6..15d05ef77c8 100644 --- a/src/main/java/org/elasticsearch/index/mapper/selector/UidFieldVisitor.java +++ b/src/main/java/org/elasticsearch/index/mapper/selector/UidFieldVisitor.java @@ -57,6 +57,11 @@ public class UidFieldVisitor extends BaseFieldVisitor { return document; } + @Override + public void reset() { + uid = null; + } + public String uid() { return uid; } diff --git a/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index e0393866ed4..5342984cb7d 100644 --- a/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -37,10 +37,9 @@ import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.internal.SourceFieldMapper; import org.elasticsearch.index.mapper.internal.UidFieldMapper; +import org.elasticsearch.index.mapper.selector.*; import org.elasticsearch.index.mapper.selector.AllButSourceFieldVisitor; import org.elasticsearch.index.mapper.selector.FieldMappersFieldVisitor; -import org.elasticsearch.index.mapper.selector.UidAndSourceFieldVisitor; -import org.elasticsearch.index.mapper.selector.UidFieldVisitor; import org.elasticsearch.indices.TypeMissingException; import org.elasticsearch.search.SearchHitField; import org.elasticsearch.search.SearchParseElement; @@ -291,6 +290,7 @@ public class FetchPhase implements SearchPhase { if (fieldVisitor == null) { return context.searcher().doc(docId); } + fieldVisitor.reset(); context.searcher().doc(docId, fieldVisitor); return fieldVisitor.createDocument(); } catch (IOException e) {