Skip to content

Apache Lucene open-source search software

Fix luke terms export failure on Windows when field names contain characters forbidden in file name (#15838)

gaobinlong149881f0799b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index d96058f1e63..b69e8ddafc6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -156,6 +156,8 @@ Bug Fixes
* GITHUB#14971: Fix CachedExecutor thread pool leak in ConcurrentMergeScheduler
when IndexWriter initialization fails. (Su Beom)
+* GITHUB#15838: Fix luke terms export failure on Windows when field names contain characters forbidden in file name (Binlong Gao)
+
Changes in Runtime Behavior
---------------------
* GITHUB#14187: The query cache is now disabled by default. (Adrien Grand)
diff --git a/lucene/luke/src/java/org/apache/lucene/luke/models/tools/IndexToolsImpl.java b/lucene/luke/src/java/org/apache/lucene/luke/models/tools/IndexToolsImpl.java
index 8d76e9e6739..151b06b07ef 100644
--- a/lucene/luke/src/java/org/apache/lucene/luke/models/tools/IndexToolsImpl.java
+++ b/lucene/luke/src/java/org/apache/lucene/luke/models/tools/IndexToolsImpl.java
@@ -199,7 +199,10 @@ public final class IndexToolsImpl extends LukeModel implements IndexTools {
@Override
public String exportTerms(String destDir, String field, String delimiter) {
- String filename = "terms_" + field + "_" + System.currentTimeMillis() + ".out";
+ // Sanitize field name for use in file name: replace forbidden characters on Windows
+ // (< > : " /\ | ? *) with '_' to avoid file creation failure.
+ String sanitizedFieldName = field.replaceAll("[<>:\"/\\\\|?*]", "_");
+ String filename = "terms_" + sanitizedFieldName + "_" + System.currentTimeMillis() + ".out";
Path path = Paths.get(destDir, filename);
try {
Terms terms = MultiTerms.getTerms(reader, field);