Optimize performance of unscored queries

This commit is contained in:
Andrea Cavalli 2021-07-27 00:32:30 +02:00
parent ea86bf7a43
commit 7abcdf05f9
5 changed files with 68 additions and 4 deletions

View File

@ -0,0 +1,62 @@
package it.cavallium.dbengine.lucene;
import java.io.IOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.StoredFieldVisitor;
import org.apache.lucene.index.StoredFieldVisitor.Status;
public class DocumentStoredSingleFieldVisitor extends StoredFieldVisitor {
private final Document doc = new Document();
private final String fieldToAdd;
public DocumentStoredSingleFieldVisitor(String fieldToAdd) {
this.fieldToAdd = fieldToAdd;
}
public DocumentStoredSingleFieldVisitor() {
this.fieldToAdd = null;
}
public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException {
this.doc.add(new StoredField(fieldInfo.name, value));
}
public void stringField(FieldInfo fieldInfo, String value) throws IOException {
FieldType ft = new FieldType(TextField.TYPE_STORED);
ft.setStoreTermVectors(fieldInfo.hasVectors());
ft.setOmitNorms(fieldInfo.omitsNorms());
ft.setIndexOptions(fieldInfo.getIndexOptions());
this.doc.add(new StoredField(fieldInfo.name, (String)Objects.requireNonNull(value, "String value should not be null"), ft));
}
public void intField(FieldInfo fieldInfo, int value) {
this.doc.add(new StoredField(fieldInfo.name, value));
}
public void longField(FieldInfo fieldInfo, long value) {
this.doc.add(new StoredField(fieldInfo.name, value));
}
public void floatField(FieldInfo fieldInfo, float value) {
this.doc.add(new StoredField(fieldInfo.name, value));
}
public void doubleField(FieldInfo fieldInfo, double value) {
this.doc.add(new StoredField(fieldInfo.name, value));
}
public Status needsField(FieldInfo fieldInfo) {
return Objects.equals(this.fieldToAdd, fieldInfo.name) ? Status.YES : Status.NO;
}
public Document getDocument() {
return this.doc;
}
}

View File

@ -188,7 +188,9 @@ public class LuceneUtils {
if (docId > indexReader.maxDoc()) {
throw new IOException("Document " + docId + " > maxDoc (" +indexReader.maxDoc() + ")");
}
Document d = indexReader.document(docId, Set.of(keyFieldName));
DocumentStoredSingleFieldVisitor visitor = new DocumentStoredSingleFieldVisitor(keyFieldName);
indexReader.document(docId, visitor);
Document d = visitor.getDocument();
if (d.getFields().isEmpty()) {
throw new NoSuchElementException(
"Can't get key (field \"" + keyFieldName + "\") of document docId: " + docId + ". Available fields: []");

View File

@ -135,7 +135,7 @@ class ScoredSimpleLuceneShardSearcher implements LuceneShardSearcher {
s -> {}
)
.subscribeOn(scheduler)
.concatMap(topFieldDoc -> LuceneUtils
.flatMapSequential(topFieldDoc -> LuceneUtils
.convertHits(topFieldDoc.scoreDocs, indexSearchers, keyFieldName, scheduler)
);
});

View File

@ -93,7 +93,7 @@ public class SimpleLuceneLocalSearcher implements LuceneLocalSearcher {
s -> {}
)
.subscribeOn(scheduler)
.concatMap(topFieldDoc -> LuceneUtils
.flatMapSequential(topFieldDoc -> LuceneUtils
.convertHits(topFieldDoc.scoreDocs, IndexSearchers.unsharded(indexSearcher), keyFieldName, scheduler)
);
});

View File

@ -126,7 +126,7 @@ class UnscoredLuceneShardSearcher implements LuceneShardSearcher {
s -> {}
)
.subscribeOn(scheduler)
.concatMap(topFieldDoc -> LuceneUtils
.flatMapSequential(topFieldDoc -> LuceneUtils
.convertHits(topFieldDoc.scoreDocs, indexSearchers, keyFieldName, scheduler)
);
});