lucene 4: make FieldVistors behave similar to FieldSelectors

Added back reset() method for now to make things work. Will refactor it out when we have tests passing.
This commit is contained in:
Igor Motov 2012-11-04 20:31:46 -05:00 committed by Shay Banon
parent 7aac88cf5c
commit 9ad05ecdea
10 changed files with 57 additions and 32 deletions

View File

@ -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<String> fieldsToAdd;
/** Load only fields named in the provided <code>Set&lt;String&gt;</code>. */
public MultipleFieldsVisitor(Set<String> fieldsToAdd) {
this.fieldsToAdd = fieldsToAdd;
}
/** Load only fields named in the provided <code>Set&lt;String&gt;</code>. */
public MultipleFieldsVisitor(String... fields) {
fieldsToAdd = new HashSet<String>(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();
}
}
}

View File

@ -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();
}

View File

@ -55,6 +55,11 @@ public class SingleFieldVisitor extends BaseFieldVisitor {
return document;
}
@Override
public void reset() {
values = null;
}
public String value() {
return values.get(0);
}

View File

@ -54,6 +54,11 @@ public class SourceFieldVisitor extends BaseFieldVisitor {
return document;
}
@Override
public void reset() {
source = null;
}
public BytesRef source() {
return source;
}

View File

@ -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

View File

@ -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<String> fieldsToAdd = new HashSet<String>();
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 + ")";

View File

@ -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)) {

View File

@ -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)) {

View File

@ -57,6 +57,11 @@ public class UidFieldVisitor extends BaseFieldVisitor {
return document;
}
@Override
public void reset() {
uid = null;
}
public String uid() {
return uid;
}

View File

@ -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) {