lucene 4: rollback optimization in SingleFieldVisitor for now to make it work

This commit is contained in:
Igor Motov 2012-11-04 23:42:26 -05:00 committed by Shay Banon
parent cfbd17992a
commit ffd262e96f
3 changed files with 13 additions and 37 deletions

View File

@ -30,10 +30,9 @@ import java.util.List;
/** /**
* *
*/ */
public class SingleFieldVisitor extends BaseFieldVisitor { public class SingleFieldVisitor extends AbstractMultipleFieldsVisitor {
private String name; private String name;
private List<String> values;
public SingleFieldVisitor() { public SingleFieldVisitor() {
} }
@ -46,44 +45,11 @@ public class SingleFieldVisitor extends BaseFieldVisitor {
this.name = name; 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<String> values() {
return values;
}
@Override @Override
public Status needsField(FieldInfo fieldInfo) throws IOException { public Status needsField(FieldInfo fieldInfo) throws IOException {
if (name.equals(fieldInfo.name)) { if (name.equals(fieldInfo.name)) {
return Status.YES; return Status.YES;
} }
return Status.NO;
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<String>();
}
values.add(value);
}
} }
} }

View File

@ -23,6 +23,8 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream; 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.Query;
import org.apache.lucene.search.highlight.*; import org.apache.lucene.search.highlight.*;
import org.apache.lucene.search.highlight.Formatter; import org.apache.lucene.search.highlight.Formatter;
@ -169,7 +171,13 @@ public class HighlightPhase extends AbstractComponent implements FetchSubPhase {
try { try {
SingleFieldVisitor fieldVisitor = new SingleFieldVisitor(mapper.names().indexName()); SingleFieldVisitor fieldVisitor = new SingleFieldVisitor(mapper.names().indexName());
hitContext.reader().document(hitContext.docId(), fieldVisitor); hitContext.reader().document(hitContext.docId(), fieldVisitor);
textsToHighlight = (List) fieldVisitor.values(); Document doc = fieldVisitor.createDocument();
textsToHighlight = new ArrayList<Object>(doc.getFields().size());
for (IndexableField docField : doc.getFields()) {
if (docField.stringValue() != null) {
textsToHighlight.add(docField.stringValue());
}
}
} catch (Exception e) { } catch (Exception e) {
throw new FetchPhaseExecutionException(context, "Failed to highlight field [" + field.field() + "]", e); throw new FetchPhaseExecutionException(context, "Failed to highlight field [" + field.field() + "]", e);
} }

View File

@ -159,6 +159,8 @@ public class FieldsLookup implements Map {
data.doc(fieldVisitor.createDocument()); data.doc(fieldVisitor.createDocument());
} catch (IOException e) { } catch (IOException e) {
throw new ElasticSearchParseException("failed to load field [" + name + "]", e); throw new ElasticSearchParseException("failed to load field [" + name + "]", e);
} finally {
fieldVisitor.reset();
} }
} }
return data; return data;