分布式搜索参考 Elasticsearch 进行设计,在 Framework 中定义了常见搜索的常见操作,例如搜索、插入、批量插入、修改和删除等。在需要使用搜索高级用例的场景中,可以通过获取原生客户端委托实例方式来进行使用。
分布式搜索抽象设计如下:
@startuml
package "cloudapp-base-api" {
interface SearchService<Client, Request, Response> {
Collection<String> search(String indexName,String fieldName,String pattern,int maxResults)
Response search(Request request)
String insertDocument(String indexName, Map<String, ?> doc)
List<String> bulkInsertDocuments(String indexName, List<Map<String, ?>> docs)
boolean deleteDocument(String indexName, String docId)
boolean updateDocument(String indexName, String docId, Map<String, ?> doc)
}
DelegatingClientAware <-- SearchService : extends
}
@enduml-
定义搜索服务接口 SearchService ,继承接口 DelegatingClientAware ,主要方法有:
-
getDelegatingClient() 获取搜索客户端委托实例;
-
search(String indexName,String fieldName,String pattern,int maxResults) 在指定索引字段通过模式搜索;
-
search(Request request) 通过请求信息搜索;
-
insertDocument(String indexName, Map<String, ?> doc) 在索引中创建文档;
-
bulkInsertDocuments(String indexName, List<Map<String, ?>> docs) 批量插入文档;
-
deleteDocument(String indexName, String docId) 删除文档;
-
updateDocument(String indexName, String docId, Map<String, ?> doc) 更新文档;
-
分布式搜索实现的配置如下:
- 定义分布式存储的配置参数类 ElasticsearchProperties ,使用注解 @ConfigurationProperties(prefix = "com.alibaba.cloudapp.search.elasticsearch") ,配置参数类字段如下:
| 字段名 | 数据类型 | 默认值 | 备注 |
|---|---|---|---|
| enabled | boolean | false | 是否启用阿里云 elastisearch |
| host | String | - | elasticsearch 连接地址 |
| port | int | 9200 | 端口 |
| username | String | - | 用户名 |
| password | String | - | 密码 |
| useHttps | boolean | false | 是否启用 https |
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudapp</groupId>
<artifactId>cloudapp-framework-dependencies</artifactId>
<version>${cloudapp.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloudapp</groupId>
<artifactId>spring-boot-starter-cloudapp</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloudapp</groupId>
<artifactId>cloudapp-spring-search-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>spring:
application:
name: elasticsearch-demo
io:
cloudapp:
search:
elasticsearch:
enabled: true
host: ${ES_HOST}
username: ${ES_USERNAME}
password: ${ES_PASSWORD}@RestController
public class ElasticsearchDemoController {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchDemoController.class);
@Autowired
SearchService searchService;
@RequestMapping("/testSearch")
public void testSearch() {
String indexName = "test-index";
String fieldName = "title";
String pattern = "EDAS";
int maxResults = 10;
Collection<String> result = searchService.search(indexName, fieldName, pattern, maxResults);
LOGGER.info("search document result: {}", JSON.toJSONString(result));
}
}