Scope
- JDK version: JDK 17 or later
- Spring Boot version: Spring Boot 3 GA or later
- Spring AI Alibaba version: The nested SearchFilters example in this topic was verified with version 1.1.2.3
Choose an invocation method
In a Spring AI Alibaba application, you can use the SDK retriever or call the RAG API directly through a Spring HTTP client. Both support _operator with nested _conditions, but their endpoints and parameters differ:
| Method | Retrieval endpoint | Knowledge base parameter | Use case |
|---|---|---|---|
| SAA DashScope SDK | /api/v1/indices/pipeline/{pipeline_id}/retrieve | Set indexName in DashScopeDocumentRetrieverOptions. The SDK looks up the knowledge base and resolves the retrieval path. | Use an existing retriever in an Advisor-based Q&A flow |
| RAG API (direct HTTP) | https://{workspace_id}.cn-beijing.maas.aliyuncs.com/api/v1/indices/rag/index/retrieve | Pass the knowledge base ID as index_id in the request body. | Control HTTP requests, retrieval parameters, and result handling |
ChatClient to generate answers without changing frameworks. Do not set DashScopeApi.baseUrl to the full RAG API retrieval URL: the SDK appends its own Pipeline path, so these addresses are not interchangeable.
Procedure
The following steps use the SAA DashScope SDK. For direct HTTP retrieval, see Call the RAG API directly over HTTP.
1. Get the project code
For the complete sample code, see the bailian-rag-knowledge project in the Spring AI Alibaba project examples. Download the entire examples folder to your local machine to ensure the project structure and dependencies are complete.
2. Configure the environment
Obtain the API key for Alibaba Cloud Model Studio and configure it as an environment variable named AI_DASHSCOPE_API_KEY. Using an environment variable helps you avoid the security risks of hard coding.
To perform operations on a knowledge base in a sub-workspace, you also need to obtain the workspace ID and configure it as an environment variable named AI_DASHSCOPE_WORKSPACE_ID.
3. Sample code
Use DashScopeApi to retrieve an Alibaba Cloud Model Studio knowledge base. The Spring AI Alibaba DashScope SDK sends retrieval requests to this path:
/api/v1/indices/rag/index/retrieve are separate retrieval endpoints. Both support _operator with nested _conditions to express multiple OR conditions on the same field.
Controller sample code
Controller sample code
DashScopeDocumentRetriever retrieves the most relevant text segments. It then submits these segments along with the original question to the large language model (LLM), which is qwen-max by default, to generate an answer.
Service sample code
Service sample code
searchFilters example matches records whose department equals Corporate, whose department contains Research, or whose document_type contains Policy. Omit searchFilters if filtering is not required. For the complete syntax and differences between invocation methods, see SearchFilters for a knowledge base.
Best practice: Call the RAG API directly over HTTP
The following example uses RestClient in the preceding SAA project to call the RAG API, convert the results to Document objects, and use the same DocumentRetrievalAdvisor. It implements a separate HTTP retriever rather than changing the SAA SDK endpoint.
Before running the example:
- Ensure the project includes Spring Web and Jackson, for example through
spring-boot-starter-web.RestClientrequires Spring Framework 6.1 or later, available in Spring Boot 3.2 or later. Also meet the dependency requirements of your SAA version. - Set the
AI_DASHSCOPE_API_KEY,AI_DASHSCOPE_WORKSPACE_ID, andBAILIAN_INDEX_IDenvironment variables to the API key, target workspace ID, and knowledge base ID. Direct HTTP calls require an explicit workspace ID, even for the default workspace. For authentication and permissions, see RAG API authentication. - Complete data import into the knowledge base. Reuse the preceding
orGroupand replace its example fields and values with searchable fields and values from your knowledge base. The HTTP request body usesindex_idandsearch_filters, not the OpenAPI parametersIndexIdandSearchFilters.
RagApiDocumentRetriever.java in the same package as CloudRagService, and use the same package declaration at the top of the file.
RagApiDocumentRetriever.java
RagApiDocumentRetriever.java
CloudRagService. In its constructor, replace the initialization of retrieverOptions and DashScopeDocumentRetriever with the remaining code. Keep orGroup, the prompt template, and the ChatClient initialization. Do not register a second Service:
_conditions to a string first.
The response's data.nodes are mapped to Document objects containing text, metadata, and similarity scores. An empty list means that no chunks matched. RestClient.retrieve() throws on HTTP errors; business failures or missing node lists also cause an exception rather than an empty result. Calls to the preceding Controller still generate an answer from the original question and retrieved context. Retain the response's request_id for troubleshooting, but do not log the API key.
Reactive applications: RestClient is synchronous, so the preceding flow includes blocking retrieval. For end-to-end nonblocking processing, call the same RAG API through WebClient with the same authentication and JSON body, and compose retrieval and model calls through Mono/Flux. Do not call .block() on reactive event-loop threads. Using WebClient does not turn RAG Retrieve into an SSE endpoint: retrieval still returns a single JSON response.
Learn more
- Spring AI Alibaba: Provides documentation, tutorials, hands-on blogs, and a developer community to help you quickly develop generative AI applications in Java.
- Create and use a knowledge base: Learn more about the core features and best practices of Alibaba Cloud Model Studio knowledge bases.