Skip to content

Commit 44a0d5c

Browse files
authored
Merge pull request #390 from brianxiadong/feat-brianxiadong-reader-es
Feat : Add Elasticsearch Document Reader | 添加 Elasticsearch 文档读取器
2 parents 4968e18 + e68d029 commit 44a0d5c

File tree

6 files changed

+759
-0
lines changed

6 files changed

+759
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Spring AI Alibaba Elasticsearch Document Reader
2+
3+
This module provides a document reader implementation for Elasticsearch, allowing you to retrieve documents from Elasticsearch indices for use with Spring AI.
4+
5+
本模块提供了 Elasticsearch 的文档读取器实现,允许从 Elasticsearch 索引中检索文档以供 Spring AI 使用。
6+
7+
## Features | 特性
8+
9+
- Read documents from Elasticsearch indices | 从 Elasticsearch 索引中读取文档
10+
- Support for both single node and cluster mode | 支持单节点和集群模式
11+
- Support for HTTPS and basic authentication | 支持 HTTPS 和基本认证
12+
- Customizable query field | 可自定义查询字段
13+
- Configurable maximum results | 可配置最大结果数
14+
- Support for both simple retrieval and query-based search | 支持简单检索和基于查询的搜索
15+
16+
## Usage | 使用方法
17+
18+
### Maven Dependency | Maven 依赖
19+
20+
```xml
21+
<dependency>
22+
<groupId>com.alibaba.cloud.ai</groupId>
23+
<artifactId>es-document-reader</artifactId>
24+
<version>${version}</version>
25+
</dependency>
26+
```
27+
28+
### Single Node Configuration | 单节点配置
29+
30+
```java
31+
ElasticsearchConfig config = new ElasticsearchConfig();
32+
config.setHost("localhost"); // Default: localhost | 默认值:localhost
33+
config.setPort(9200); // Default: 9200 | 默认值:9200
34+
config.setIndex("your-index"); // Required | 必填
35+
config.setQueryField("content"); // Default: content | 默认值:content
36+
config.setMaxResults(10); // Default: 10 | 默认值:10
37+
config.setScheme("https"); // Default: http | 默认值:http
38+
39+
// Optional authentication | 可选的认证配置
40+
config.setUsername("your-username");
41+
config.setPassword("your-password");
42+
43+
ElasticsearchDocumentReader reader = new ElasticsearchDocumentReader(config);
44+
```
45+
46+
### Cluster Configuration | 集群配置
47+
48+
```java
49+
ElasticsearchConfig config = new ElasticsearchConfig();
50+
// Configure cluster nodes | 配置集群节点
51+
config.setNodes(Arrays.asList(
52+
"node1:9200",
53+
"node2:9201",
54+
"node3:9202"
55+
));
56+
config.setIndex("your-index");
57+
config.setQueryField("content");
58+
config.setScheme("https");
59+
60+
// Optional authentication (applied to all nodes) | 可选的认证配置(应用于所有节点)
61+
config.setUsername("your-username");
62+
config.setPassword("your-password");
63+
64+
ElasticsearchDocumentReader reader = new ElasticsearchDocumentReader(config);
65+
```
66+
67+
### Reading Documents | 读取文档
68+
69+
```java
70+
// Get all documents | 获取所有文档
71+
List<Document> documents = reader.get();
72+
73+
// Get document by ID | 通过 ID 获取文档
74+
Document document = reader.getById("document-id");
75+
76+
// Search documents by query | 通过查询搜索文档
77+
List<Document> queryResults = reader.readWithQuery("your search query");
78+
```
79+
80+
## Configuration Properties | 配置属性
81+
82+
| Property 属性 | Description 描述 | Default Value 默认值 |
83+
|------------|----------------|------------------|
84+
| host | Elasticsearch host 主机地址 | localhost |
85+
| port | Elasticsearch port 端口 | 9200 |
86+
| nodes | List of cluster nodes (host:port) 集群节点列表 | [] |
87+
| index | Index name to query 要查询的索引名称 | - |
88+
| queryField | Field to search in 搜索字段 | content |
89+
| username | Username for authentication 认证用户名 | - |
90+
| password | Password for authentication 认证密码 | - |
91+
| maxResults | Maximum number of results to return 最大返回结果数 | 10 |
92+
| scheme | Connection scheme (http/https) 连接方案 | http |
93+
94+
## Cluster Support | 集群支持
95+
96+
The reader supports both single node and cluster configurations:
97+
读取器支持单节点和集群两种配置方式:
98+
99+
- If `nodes` is provided, it will use cluster mode | 如果提供了 `nodes`,将使用集群模式
100+
- If `nodes` is empty, it will use single node mode with `host` and `port` | 如果 `nodes` 为空,将使用 `host``port` 的单节点模式
101+
- All nodes in the cluster share the same authentication and scheme settings | 集群中的所有节点共享相同的认证和方案设置
102+
103+
## HTTPS Support | HTTPS 支持
104+
105+
For secure connections: | 对于安全连接:
106+
107+
1. Set `scheme` to "https" | 将 `scheme` 设置为 "https"
108+
2. The reader will automatically: | 读取器将自动:
109+
- Create a secure SSL context | 创建安全的 SSL 上下文
110+
- Trust all certificates (for development) | 信任所有证书(用于开发环境)
111+
- Handle hostname verification | 处理主机名验证
112+
- Apply authentication if provided | 应用提供的认证信息
113+
114+
## Testing | 测试
115+
116+
The module includes comprehensive tests. To run the tests:
117+
模块包含完整的测试。要运行测试:
118+
119+
```bash
120+
mvn test
121+
```
122+
123+
## Requirements | 要求
124+
125+
- Java 17 or later | Java 17 或更高版本
126+
- Elasticsearch 8.x | Elasticsearch 8.x 版本
127+
- Docker (for running tests) | Docker(用于运行测试)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2024-2025 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>com.alibaba.cloud.ai</groupId>
23+
<artifactId>spring-ai-alibaba</artifactId>
24+
<version>${revision}</version>
25+
<relativePath>../../../pom.xml</relativePath>
26+
</parent>
27+
28+
<artifactId>es-document-reader</artifactId>
29+
<name>Spring AI Alibaba Elasticsearch Document Reader</name>
30+
<description>Spring AI Alibaba Elasticsearch Document Reader</description>
31+
32+
<dependencies>
33+
<!-- Spring AI -->
34+
<dependency>
35+
<groupId>org.springframework.ai</groupId>
36+
<artifactId>spring-ai-core</artifactId>
37+
</dependency>
38+
39+
<!-- Elasticsearch -->
40+
<dependency>
41+
<groupId>co.elastic.clients</groupId>
42+
<artifactId>elasticsearch-java</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.fasterxml.jackson.core</groupId>
46+
<artifactId>jackson-databind</artifactId>
47+
</dependency>
48+
49+
<!-- Test dependencies -->
50+
<dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.mockito</groupId>
57+
<artifactId>mockito-core</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.assertj</groupId>
62+
<artifactId>assertj-core</artifactId>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.testcontainers</groupId>
67+
<artifactId>elasticsearch</artifactId>
68+
<scope>test</scope>
69+
</dependency>
70+
</dependencies>
71+
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.cloud.ai.document.reader.es;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
/**
22+
* Configuration class for Elasticsearch document reader. Contains all necessary settings
23+
* to connect to and query Elasticsearch.
24+
*
25+
* @author brianxiadong
26+
* @since 0.0.1
27+
*/
28+
public class ElasticsearchConfig {
29+
30+
/**
31+
* Elasticsearch host URL
32+
*/
33+
private String host = "localhost";
34+
35+
/**
36+
* Elasticsearch port
37+
*/
38+
private int port = 9200;
39+
40+
/**
41+
* List of cluster nodes in format: hostname:port
42+
*/
43+
private List<String> nodes = new ArrayList<>();
44+
45+
/**
46+
* Index name to query
47+
*/
48+
private String index;
49+
50+
/**
51+
* Query field to search in
52+
*/
53+
private String queryField = "content";
54+
55+
/**
56+
* Username for authentication (optional)
57+
*/
58+
private String username;
59+
60+
/**
61+
* Password for authentication (optional)
62+
*/
63+
private String password;
64+
65+
/**
66+
* Maximum number of documents to retrieve
67+
*/
68+
private int maxResults = 10;
69+
70+
/**
71+
* Connection scheme (http/https)
72+
*/
73+
private String scheme = "http";
74+
75+
// Getters and Setters
76+
public String getHost() {
77+
return host;
78+
}
79+
80+
public void setHost(String host) {
81+
this.host = host;
82+
}
83+
84+
public int getPort() {
85+
return port;
86+
}
87+
88+
public void setPort(int port) {
89+
this.port = port;
90+
}
91+
92+
public List<String> getNodes() {
93+
return nodes;
94+
}
95+
96+
public void setNodes(List<String> nodes) {
97+
this.nodes = nodes;
98+
}
99+
100+
public String getIndex() {
101+
return index;
102+
}
103+
104+
public void setIndex(String index) {
105+
this.index = index;
106+
}
107+
108+
public String getQueryField() {
109+
return queryField;
110+
}
111+
112+
public void setQueryField(String queryField) {
113+
this.queryField = queryField;
114+
}
115+
116+
public String getUsername() {
117+
return username;
118+
}
119+
120+
public void setUsername(String username) {
121+
this.username = username;
122+
}
123+
124+
public String getPassword() {
125+
return password;
126+
}
127+
128+
public void setPassword(String password) {
129+
this.password = password;
130+
}
131+
132+
public int getMaxResults() {
133+
return maxResults;
134+
}
135+
136+
public void setMaxResults(int maxResults) {
137+
this.maxResults = maxResults;
138+
}
139+
140+
public String getScheme() {
141+
return scheme;
142+
}
143+
144+
public void setScheme(String scheme) {
145+
this.scheme = scheme;
146+
}
147+
148+
}

0 commit comments

Comments
 (0)