LUCENE-8552: Optimize getMergedFieldInfos for zero or one segment

This commit is contained in:
Christophe Bismuth 2018-11-16 16:45:09 -05:00 committed by David Smiley
parent cd61a926fb
commit db9542526e
3 changed files with 67 additions and 9 deletions

View File

@ -212,11 +212,16 @@ New Features
IndexWriterConfig#setIndexCreatedVersionMajor. This is an expert feature.
(Adrien Grand)
Improvements:
Improvements
* LUCENE-8463: TopFieldCollector can now early-terminates queries when sorting by SortField.DOC.
(Christophe Bismuth via Jim Ferenczi)
Optimizations
* LUCENE-8552: FieldInfos.getMergedFieldInfos no longer does any merging if there is <= 1 segment.
(Christophe Bismuth via David Smiley)
======================= Lucene 7.6.0 =======================
Build

View File

@ -139,15 +139,22 @@ public class FieldInfos implements Iterable<FieldInfo> {
* will be unavailable.
*/
public static FieldInfos getMergedFieldInfos(IndexReader reader) {
final String softDeletesField = reader.leaves().stream()
.map(l -> l.reader().getFieldInfos().getSoftDeletesField())
.filter(Objects::nonNull)
.findAny().orElse(null);
final Builder builder = new Builder(new FieldNumbers(softDeletesField));
for(final LeafReaderContext ctx : reader.leaves()) {
builder.add(ctx.reader().getFieldInfos());
final List<LeafReaderContext> leaves = reader.leaves();
if (leaves.isEmpty()) {
return FieldInfos.EMPTY;
} else if (leaves.size() == 1) {
return leaves.get(0).reader().getFieldInfos();
} else {
final String softDeletesField = leaves.stream()
.map(l -> l.reader().getFieldInfos().getSoftDeletesField())
.filter(Objects::nonNull)
.findAny().orElse(null);
final Builder builder = new Builder(new FieldNumbers(softDeletesField));
for (final LeafReaderContext ctx : leaves) {
builder.add(ctx.reader().getFieldInfos());
}
return builder.finish();
}
return builder.finish();
}
/** Returns a set of names of fields that have a terms index. The order is undefined. */

View File

@ -17,6 +17,7 @@
package org.apache.lucene.index;
import java.io.IOException;
import java.util.Iterator;
import org.apache.lucene.analysis.MockAnalyzer;
@ -26,6 +27,9 @@ import org.apache.lucene.document.StringField;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
public class TestFieldInfos extends LuceneTestCase {
public void testFieldInfos() throws Exception{
@ -89,4 +93,46 @@ public class TestFieldInfos extends LuceneTestCase {
dir.close();
}
public void testMergedFieldInfos_empty() throws IOException {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
IndexReader reader = writer.getReader();
FieldInfos actual = FieldInfos.getMergedFieldInfos(reader);
FieldInfos expected = FieldInfos.EMPTY;
assertThat(actual, sameInstance(expected));
reader.close();
writer.close();
dir.close();
}
public void testMergedFieldInfos_singleLeaf() throws IOException {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
Document d1 = new Document();
d1.add(new StringField("f1", "v1", Field.Store.YES));
writer.addDocument(d1);
writer.commit();
Document d2 = new Document();
d2.add(new StringField("f2", "v2", Field.Store.YES));
writer.addDocument(d2);
writer.commit();
writer.forceMerge(1);
IndexReader reader = writer.getReader();
FieldInfos actual = FieldInfos.getMergedFieldInfos(reader);
FieldInfos expected = reader.leaves().get(0).reader().getFieldInfos();
assertThat(reader.leaves().size(), equalTo(1));
assertThat(actual, sameInstance(expected));
reader.close();
writer.close();
dir.close();
}
}