Skip to content

Apache Lucene open-source search software

LUCENE-7659: Added IndexWriter#getFieldNames() to return all visible field names

Ishan Chattopadhyaya733060121dc6
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 9a1622f27cd..06212ae303f 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -77,6 +77,11 @@ API Changes
* LUCENE-7643: Replaced doc-values queries in lucene/sandbox with factory
methods on the *DocValuesField classes. (Adrien Grand)
+* LUCENE-7659: Added a IndexWriter#getFieldNames() method (experimental) to return
+ all field names as visible from the IndexWriter. This would be useful for
+ IndexWriter#updateDocValues() calls, to prevent calling with non-existent
+ docValues fields (Ishan Chattopadhyaya, Adrien Grand, Mike McCandless)
+
New Features
* LUCENE-7623: Add FunctionScoreQuery and FunctionMatchQuery (Alan Woodward,
diff --git a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java
index c80fb854b49..890dccabfb9 100644
--- a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java
+++ b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java
@@ -20,8 +20,10 @@ package org.apache.lucene.index;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
+import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
@@ -324,6 +326,10 @@ public class FieldInfos implements Iterable<FieldInfo> {
}
}
+ synchronized Set<String> getFieldNames() {
+ return Collections.unmodifiableSet(new HashSet<String>(nameToNumber.keySet()));
+ }
+
synchronized void clear() {
numberToName.clear();
nameToNumber.clear();
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
index 0fc2e2476a8..cbf2ae27193 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
@@ -1783,6 +1783,22 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {
return flushDeletesCount.get();
}
+ /**
+ * Return an unmodifiable set of all field names as visible
+ * from this IndexWriter, across all segments of the index.
+ * Useful for knowing which fields exist, before {@link #updateDocValues(Term, Field...)} is
+ * attempted. We could phase out this method if
+ * {@link #updateDocValues(Term, Field...)} could create the non-existent
+ * docValues fields as necessary, instead of throwing
+ * IllegalArgumentException for attempts to update non-existent
+ * docValues fields.
+ * @lucene.internal
+ * @lucene.experimental
+ */
+ public Set<String> getFieldNames() {
+ return globalFieldNumberMap.getFieldNames(); // FieldNumbers#getFieldNames() returns an unmodifiableSet
+ }
+
final String newSegmentName() {
// Cannot synchronize on IndexWriter because that causes
// deadlock