This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-full-text.git


The following commit(s) were added to refs/heads/main by this push:
     new ddf0ad9  [java] Expose the default text field from an index (#25)
ddf0ad9 is described below

commit ddf0ad98da2a9eaab74e2515cf971ecd6d44ec53
Author: zhoulii <[email protected]>
AuthorDate: Mon Nov 23 16:15:53 2026 +0800

    [java] Expose the default text field from an index (#25)
---
 .../paimon/index/fulltext/FullTextIndexReader.java |  8 ++++
 .../paimon/index/fulltext/FullTextNative.java      |  2 +
 .../fulltext/FullTextNativeRoundTripTest.java      | 52 ++++++++++++++++++++++
 jni/src/lib.rs                                     | 22 ++++++++-
 4 files changed, 83 insertions(+), 1 deletion(-)

diff --git 
a/java/src/main/java/org/apache/paimon/index/fulltext/FullTextIndexReader.java 
b/java/src/main/java/org/apache/paimon/index/fulltext/FullTextIndexReader.java
index 60ec6c0..58eb1a0 100644
--- 
a/java/src/main/java/org/apache/paimon/index/fulltext/FullTextIndexReader.java
+++ 
b/java/src/main/java/org/apache/paimon/index/fulltext/FullTextIndexReader.java
@@ -57,6 +57,14 @@ public final class FullTextIndexReader implements 
AutoCloseable {
         return FullTextNative.readMetrics(requireOpen());
     }
 
+    /**
+     * Returns the text field used by {@link 
FullTextIndexWriter#addDocument(long, String)}.
+     * The field is read from this index's resolved configuration.
+     */
+    public String defaultTextField() {
+        return FullTextNative.defaultTextField(requireOpen());
+    }
+
     public void prewarm() {
         FullTextNative.prewarm(requireOpen());
     }
diff --git 
a/java/src/main/java/org/apache/paimon/index/fulltext/FullTextNative.java 
b/java/src/main/java/org/apache/paimon/index/fulltext/FullTextNative.java
index f249f93..0139930 100644
--- a/java/src/main/java/org/apache/paimon/index/fulltext/FullTextNative.java
+++ b/java/src/main/java/org/apache/paimon/index/fulltext/FullTextNative.java
@@ -132,5 +132,7 @@ final class FullTextNative {
 
     static native FullTextReadMetrics readMetrics(long readerPtr);
 
+    static native String defaultTextField(long readerPtr);
+
     static native void freeReader(long readerPtr);
 }
diff --git 
a/java/src/test/java/org/apache/paimon/index/fulltext/FullTextNativeRoundTripTest.java
 
b/java/src/test/java/org/apache/paimon/index/fulltext/FullTextNativeRoundTripTest.java
index 95bb8ad..e7f3b3b 100644
--- 
a/java/src/test/java/org/apache/paimon/index/fulltext/FullTextNativeRoundTripTest.java
+++ 
b/java/src/test/java/org/apache/paimon/index/fulltext/FullTextNativeRoundTripTest.java
@@ -24,6 +24,7 @@ import org.junit.Test;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -63,6 +64,7 @@ public class FullTextNativeRoundTripTest {
                 };
 
         try (FullTextIndexReader reader = new FullTextIndexReader(input)) {
+            assertEquals("text", reader.defaultTextField());
             int bytesReadAtOpen = bytesRead.get();
             FullTextReadMetrics metrics = reader.readMetrics();
             assertTrue(metrics.preadCalls() >= 2);
@@ -112,6 +114,7 @@ public class FullTextNativeRoundTripTest {
                         preadBytes(indexBytes, position, buffer, offset, 
length);
 
         try (FullTextIndexReader reader = new FullTextIndexReader(input)) {
+            assertEquals("title", reader.defaultTextField());
             FullTextSearchResult result =
                     reader.search("{\"match\":{\"query\":\"paimon\"}}", 10);
 
@@ -121,6 +124,55 @@ public class FullTextNativeRoundTripTest {
         }
     }
 
+    @Test
+    public void testDefaultTextFieldReflectsArchiveConfiguration() throws 
Exception {
+        assumeTrue("native library must be available", 
nativeLibraryAvailable());
+        for (Map<String, String> options :
+                Arrays.asList(
+                    Collections.singletonMap("text-field", "内容"),
+                    Collections.singletonMap("text-fields", "body,title"),
+                    conflictingFieldOptions()
+                )) {
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
+            try (FullTextIndexWriter writer = 
FullTextIndexWriter.create(options)) {
+                writer.addDocument(3L, "apache paimon");
+                writer.writeIndex(output::write);
+            }
+            byte[] indexBytes = output.toByteArray();
+            FullTextIndexReader reader =
+                    new FullTextIndexReader(
+                            (position, buffer, offset, length) ->
+                                    preadBytes(indexBytes, position, buffer, 
offset, length));
+            String field;
+            try {
+                field = reader.defaultTextField();
+                assertEquals(1, reader.search(matchQuery("paimon", field), 
10).size());
+                if (options.containsKey("text-field")) {
+                    assertEquals("内容", field);
+                } else if (!options.containsKey("columns")) {
+                    assertEquals("body", field);
+                } else {
+                    assertTrue(field.equals("body") || field.equals("title"));
+                }
+            } finally {
+                reader.close();
+            }
+            try {
+                reader.defaultTextField();
+                fail("Expected closed reader to fail");
+            } catch (IllegalStateException expected) {
+                assertEquals("FullTextIndexReader is closed", 
expected.getMessage());
+            }
+        }
+    }
+
+    private static Map<String, String> conflictingFieldOptions() {
+        Map<String, String> options = new LinkedHashMap<>();
+        options.put("text-fields", "body");
+        options.put("columns", "title");
+        return options;
+    }
+
     private static boolean nativeLibraryAvailable() {
         String path = System.getenv("PAIMON_FTINDEX_JNI_LIB_PATH");
         if (path != null && !path.isEmpty() && new File(path).isFile()) {
diff --git a/jni/src/lib.rs b/jni/src/lib.rs
index 20f418f..3df3772 100644
--- a/jni/src/lib.rs
+++ b/jni/src/lib.rs
@@ -16,7 +16,7 @@
 // under the License.
 
 use jni::objects::{GlobalRef, JByteArray, JClass, JObject, JObjectArray, 
JString, JValue};
-use jni::sys::{jint, jlong, jobject};
+use jni::sys::{jint, jlong, jobject, jstring};
 use jni::{JNIEnv, JavaVM};
 use paimon_ftindex_core::io::{ReadRequest, SeekRead, SeekWrite};
 use paimon_ftindex_core::{FullTextIndexConfig, FullTextIndexReader, 
FullTextIndexWriter};
@@ -278,6 +278,18 @@ pub extern "system" fn 
Java_org_apache_paimon_index_fulltext_FullTextNative_read
     }
 }
 
+#[no_mangle]
+pub extern "system" fn 
Java_org_apache_paimon_index_fulltext_FullTextNative_defaultTextField(
+    mut env: JNIEnv,
+    _class: JClass,
+    reader_ptr: jlong,
+) -> jstring {
+    match default_text_field(&mut env, reader_ptr) {
+        Ok(field) => field,
+        Err(e) => throw_and_return(&mut env, &e, ptr::null_mut()),
+    }
+}
+
 #[no_mangle]
 pub extern "system" fn 
Java_org_apache_paimon_index_fulltext_FullTextNative_freeReader(
     _env: JNIEnv,
@@ -404,6 +416,14 @@ fn prewarm(reader_ptr: jlong) -> Result<(), String> {
     reader.inner.prewarm().map_err(|e| e.to_string())
 }
 
+fn default_text_field(env: &mut JNIEnv, reader_ptr: jlong) -> Result<jstring, 
String> {
+    let reader = handle_ref::<ReaderHandle>(reader_ptr, "reader")?;
+    let field = reader.inner.metadata().config.default_text_field();
+    env.new_string(field)
+        .map(|field| field.into_raw())
+        .map_err(|e| e.to_string())
+}
+
 fn read_metrics(env: &mut JNIEnv, reader_ptr: jlong) -> Result<jobject, 
String> {
     let reader = handle_ref::<ReaderHandle>(reader_ptr, "reader")?;
     let metrics = reader.inner.read_metrics();

Reply via email to