mirror of https://github.com/apache/lucene.git
LUCENE-9680 - Re-add IndexWriter::getFieldNames
This commit is contained in:
parent
a53e8e7228
commit
8d0cbcbb53
|
@ -7,6 +7,9 @@ http://s.apache.org/luceneversions
|
||||||
|
|
||||||
New Features
|
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-9322: Vector-valued fields, Lucene90 Codec (Mike Sokolov, Julie Tibshirani, Tomoko Uchida)
|
||||||
|
|
||||||
* LUCENE-9004: Approximate nearest vector search via NSW graphs
|
* LUCENE-9004: Approximate nearest vector search via NSW graphs
|
||||||
|
|
|
@ -25,6 +25,7 @@ import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
import org.apache.lucene.util.ArrayUtil;
|
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() {
|
synchronized void clear() {
|
||||||
numberToName.clear();
|
numberToName.clear();
|
||||||
nameToNumber.clear();
|
nameToNumber.clear();
|
||||||
|
|
|
@ -1987,6 +1987,17 @@ public class IndexWriter
|
||||||
return dvUpdates;
|
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
|
// for test purpose
|
||||||
final synchronized int getSegmentCount() {
|
final synchronized int getSegmentCount() {
|
||||||
return segmentInfos.size();
|
return segmentInfos.size();
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue