Document chunking

更新时间:
复制 MD 格式

Call the document splitting service on AI Search Open Platform using the Java SDK to split a text document into chunks for use cases such as retrieval-augmented generation (RAG).

Prerequisites

Before you begin, ensure that you have:

Split a document

The following example splits a plain text document into chunks with a maximum size of 300 characters.

package com.aliyun.sample;

import com.aliyun.searchplat20240529.Client;
import com.aliyun.searchplat20240529.models.GetDocumentSplitRequest;
import com.aliyun.searchplat20240529.models.GetDocumentSplitResponse;
import com.aliyun.teaopenapi.models.Config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SplitterDemo2 {
    public static void main(String[] args) throws Exception {
        // Configure the client
        Config config = new Config();
        // Set your API key (format: OS-****)
        config.setBearerToken("your-api-key: OS-****");
        // Set the endpoint — do not include http://
        config.setEndpoint("Replace with the API endpoint: ****.opensearch.aliyuncs.com");
        config.setProtocol("http");
        Client client = new Client(config);

        // Build the request
        GetDocumentSplitRequest request = GetDocumentSplitRequest.build(new HashMap<String, Map<String, Object>>() {{
            put("document", new HashMap<String, Object>() {{
                put("content", "Benefits\nIndustry Algorithm Edition\n...");
                put("content_type", "text");
            }});
            put("strategy", new HashMap<String, Object>() {{
                put("max_chunk_size", 300);
                put("need_sentence", false);
            }});
        }});

        // Send the request and print the results
        GetDocumentSplitResponse response = client.getDocumentSplit("default", "ops-document-split-001", request);
        System.out.println(response.getStatusCode());
        System.out.println(Arrays.toString(
            response.getBody().getResult().getChunks().stream()
                .map(chunk -> chunk.getContent())
                .toArray()
        ));
    }
}

Replace the following placeholders with your actual values:

PlaceholderDescriptionExample
your-api-key: OS-****Your API keyOS-abc123
****.opensearch.aliyuncs.comYour API endpointmy-instance.opensearch.aliyuncs.com

Key parameters

The request body cannot exceed 8 MB. For a full parameter reference, see Document splitting API reference.

ParameterTypeDescription
document.contentStringThe text content to split.
document.content_typeStringThe content type. Set to "text" for plain text.
strategy.max_chunk_sizeIntegerThe maximum number of characters per chunk.
strategy.need_sentenceBooleanWhether to split only at sentence boundaries.

The client.getDocumentSplit() method takes three arguments: the workspace name ("default"), the service name ("ops-document-split-001"), and the request object.

Response

The response contains a list of chunks, each with a content field holding the split text. Access the chunks via:

response.getBody().getResult().getChunks()

Each chunk exposes getContent() to retrieve its text. The example above prints all chunk contents as an array.

What's next