LUCENE-9680 - Re-add IndexWriter::getFieldNames

This commit is contained in:
orenovadia 2021-02-02 14:38:43 -08:00 committed by GitHub
parent a53e8e7228
commit 8d0cbcbb53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 0 deletions

View File

@ -7,6 +7,9 @@ http://s.apache.org/luceneversions
New Features
* LUCENE-9680: IndexWriter#getFieldNames() method added to get fields present in index.
This method was removed in LUCENE-8909. (Oren Ovadia)
* LUCENE-9322: Vector-valued fields, Lucene90 Codec (Mike Sokolov, Julie Tibshirani, Tomoko Uchida)
* LUCENE-9004: Approximate nearest vector search via NSW graphs

View File

@ -25,6 +25,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.lucene.util.ArrayUtil;
@ -681,6 +682,10 @@ public class FieldInfos implements Iterable<FieldInfo> {
}
}
synchronized Set<String> getFieldNames() {
return Set.copyOf(nameToNumber.keySet());
}
synchronized void clear() {
numberToName.clear();
nameToNumber.clear();

View File

@ -1987,6 +1987,17 @@ public class IndexWriter
return dvUpdates;
}
/**
* Return an unmodifiable set of all field names as visible from this IndexWriter, across all
* segments of the index.
*
* @lucene.experimental
*/
public Set<String> getFieldNames() {
// FieldNumbers#getFieldNames() returns an unmodifiableSet
return globalFieldNumberMap.getFieldNames();
}
// for test purpose
final synchronized int getSegmentCount() {
return segmentInfos.size();

View File

@ -4635,4 +4635,52 @@ public class TestIndexWriter extends LuceneTestCase {
}
}
}
public void testGetFieldNames() throws IOException {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
assertEquals(Set.of(), writer.getFieldNames());
addDocWithField(writer, "f1");
assertEquals(Set.of("f1"), writer.getFieldNames());
// should be unmodifiable:
final Set<String> fieldSet = writer.getFieldNames();
assertThrows(UnsupportedOperationException.class, () -> fieldSet.add("cannot modify"));
assertThrows(UnsupportedOperationException.class, () -> fieldSet.remove("f1"));
addDocWithField(writer, "f2");
assertEquals(Set.of("f1", "f2"), writer.getFieldNames());
// set from a previous call is an independent immutable copy, cannot be modified.
assertEquals(Set.of("f1"), fieldSet);
// flush should not have an effect on field names
writer.flush();
assertEquals(Set.of("f1", "f2"), writer.getFieldNames());
// commit should not have an effect on field names
writer.commit();
assertEquals(Set.of("f1", "f2"), writer.getFieldNames());
writer.close();
// new writer should identify committed fields
writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
assertEquals(Set.of("f1", "f2"), writer.getFieldNames());
writer.deleteAll();
assertEquals(Set.of(), writer.getFieldNames());
writer.close();
dir.close();
}
private static void addDocWithField(IndexWriter writer, String field) throws IOException {
Document doc = new Document();
doc.add(newField(field, "value", storedTextType));
writer.addDocument(doc);
}
}