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:
Activated AI Search Open Platform. For more information, see Activate AI Search Open Platform
Completed authentication using an API key. For more information, see Manage API key
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:
| Placeholder | Description | Example |
|---|---|---|
your-api-key: OS-**** | Your API key | OS-abc123 |
****.opensearch.aliyuncs.com | Your API endpoint | my-instance.opensearch.aliyuncs.com |
Key parameters
The request body cannot exceed 8 MB. For a full parameter reference, see Document splitting API reference.
| Parameter | Type | Description |
|---|---|---|
document.content | String | The text content to split. |
document.content_type | String | The content type. Set to "text" for plain text. |
strategy.max_chunk_size | Integer | The maximum number of characters per chunk. |
strategy.need_sentence | Boolean | Whether 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
Document splitting API reference — full parameter list, supported content types, and response schema