Skip to content

Commit

Permalink
feat:引入oss
Browse files Browse the repository at this point in the history
  • Loading branch information
Java-Edge committed Nov 2, 2024
1 parent 6fdf3a2 commit 4802deb
Show file tree
Hide file tree
Showing 15 changed files with 1,093 additions and 118 deletions.
1 change: 1 addition & 0 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Binary file not shown.
119 changes: 63 additions & 56 deletions common/src/main/java/com/javagpt/common/util/EsDocumentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import co.elastic.clients.util.ObjectBuilder;
import jakarta.annotation.Resource;
Expand All @@ -26,8 +28,7 @@
public class EsDocumentUtils {

@Resource
private ElasticsearchClient elasticsearchClient;

private ElasticsearchClient esClient;

/**
* 1、向Document(活动表)中添加数据(文档)
Expand All @@ -38,9 +39,6 @@ public class EsDocumentUtils {
* @param document 数据行-文档
* @param id 表的主键id
* @param classz 表结构对应的实体类
* @param <T>
* @param <U>
* @return
*/
@SneakyThrows
public <T, U> CreateResponse addDocument(String indexName, T document, String id, Class<U> classz) {
Expand All @@ -50,7 +48,7 @@ public <T, U> CreateResponse addDocument(String indexName, T document, String id
Assert.isTrue(classz != null, "classz 表结构对应的实体类-不能为空");

// 构建一个创建Doc的请求
CreateResponse createResponse = elasticsearchClient.create(item -> item.index(indexName).id(id).document(document));
CreateResponse createResponse = esClient.create(item -> item.index(indexName).id(id).document(document));
return createResponse;
}

Expand All @@ -63,9 +61,6 @@ public <T, U> CreateResponse addDocument(String indexName, T document, String id
* @param documents 多数据行
* @param id 表的主键id
* @param classz 表结构对应的实体类
* @param <T>
* @param <U>
* @return
*/
@SneakyThrows
public <T, U> BulkResponse batchAddDocument(String indexName, List<T> documents, String id, Class<U> classz) {
Expand All @@ -79,41 +74,37 @@ public <T, U> BulkResponse batchAddDocument(String indexName, List<T> documents,
for (T item : documents) {
bulkOperations.add(BulkOperation.of(o -> o.index(i -> i.document(item))));
}
BulkResponse bulkResponse = elasticsearchClient.bulk(b -> b.index(indexName).operations(bulkOperations));
BulkResponse bulkResponse = esClient.bulk(b -> b.index(indexName).operations(bulkOperations));
return bulkResponse;
}

/**
* 2、更新文档数据-map入参方式
* 更新文档数据-map入参方式
*
* @param indexName 索引名称-即表名
* @param id 表的主键id
* @param map 构建需要修改的内容,这里使用了Map
* @param classz 表结构对应的实体类
* @param <U>
*/
@SneakyThrows
public <U> UpdateResponse updateDocumentByMap(String indexName, String id, Map<String, Object> map, Class<U> classz) {
public <U> UpdateResponse updateDocByMap(String indexName, String id, Map<String, Object> map, Class<U> classz) {
Assert.isTrue(StringUtils.isNotBlank(indexName), "indexName 不能为空");
Assert.isTrue(StringUtils.isNotBlank(id), "id 不能为空");
Assert.isTrue(map != null, "map 需要修改的内容-不能为空");
Assert.isTrue(classz != null, "classz 表结构对应的实体类-不能为空");

// 构建修改文档的请求
UpdateResponse<U> response = elasticsearchClient.update(item -> item.index(indexName).id(id).doc(map), classz);
UpdateResponse<U> response = esClient.update(item -> item.index(indexName).id(id).doc(map), classz);
return response;
}

/**
* 3、更新文档数据-model入参方式
* 更新文档数据-model入参方式
*
* @param indexName 索引名称-即表名
* @param id 表的主键id
* @param document 数据行-文档
* @param classz 表结构对应的实体类
* @param <T>
* @param <U>
* @return
*/
@SneakyThrows
public <T, U> UpdateResponse updateDocumentByModel(String indexName, String id, T document, Class<U> classz) {
Expand All @@ -123,74 +114,85 @@ public <T, U> UpdateResponse updateDocumentByModel(String indexName, String id,
Assert.isTrue(classz != null, "classz 表结构对应的实体类-不能为空");

// 构建修改文档的请求
UpdateResponse<U> response = elasticsearchClient.update(item -> item.index(indexName).id(id).doc(document), classz);
UpdateResponse<U> response = esClient.update(item -> item.index(indexName).id(id).doc(document), classz);
return response;
}

/**
* 4、判断Document是否存在
* 判断Document是否存在
*
* @param indexName 索引名称-即表名
* @param id 表的主键id
* @return
*/
@SneakyThrows
public BooleanResponse existsDocument(String indexName, String id) {
Assert.isTrue(StringUtils.isNotBlank(indexName), "indexName 不能为空");
Assert.isTrue(StringUtils.isNotBlank(id), "id 不能为空");

BooleanResponse response = elasticsearchClient.exists(item -> item.index(indexName).id(id));
BooleanResponse response = esClient.exists(item -> item.index(indexName).id(id));
return response;
}

/**
* 5、查询Document
* 查询Document
*
* @param indexName 索引名称-即表名
* @param id 表的主键id
* @param classz 表结构对应的实体类
* @param <U>
* @return
*/
@SneakyThrows
public <U> GetResponse<U> getDocument(String indexName, String id, Class<U> classz) {
Assert.isTrue(StringUtils.isNotBlank(indexName), "indexName 不能为空");
Assert.isTrue(StringUtils.isNotBlank(id), "id 不能为空");
Assert.isTrue(classz != null, "classz 表结构对应的实体类-不能为空");
GetResponse<U> response = elasticsearchClient.get(item -> item.index(indexName).id(id), classz);
GetResponse<U> response = esClient.get(item -> item.index(indexName).id(id), classz);
return response;
}

/**
* 6、删除Document
* 删除Document
*
* @param indexName 索引名称-即表名
* @param id 表的主键id
* @return
*/
@SneakyThrows
public DeleteResponse deleteDocument(String indexName, String id) {
Assert.isTrue(StringUtils.isNotBlank(indexName), "indexName 不能为空");
Assert.isTrue(StringUtils.isNotBlank(id), "id 不能为空");

DeleteResponse response = elasticsearchClient.delete(item -> item.index(indexName).id(id));
DeleteResponse response = esClient.delete(item -> item.index(indexName).id(id));
return response;
}


/**
* 7、根据某个字段精确查询数据---term查询(完全匹配)
* 根据某个字段精确查询数据---term查询(完全匹配)
* 不使用分词器精确查找
*
* @param indexName 索引名称-即表名
* @param classz 表结构对应的实体类
* @param field 要查询的字段
* @param value 要查询的字段值
* @param <U>
*/
@SneakyThrows
public <U> SearchResponse<U> getDocumentsByField(String indexName, Class<U> classz, String field, String value) {
SearchResponse<U> searchResponse = elasticsearchClient.search(s -> deal(s, indexName, field, value), classz);
SearchResponse<U> searchResponse = esClient.search(s -> deal(s, indexName, field, value), classz);
return searchResponse;
}

/**
* 8、自定义查询条件查询
* 不使用分词器精确查找
* @param indexName 索引名称-即表名
* @param classz 表结构对应的实体类
* @param query 自定义的查询条件
* @param <U>
*/
@SneakyThrows
public <U> SearchResponse<U> getDocumentsByCustomize(String indexName, Class<U> classz, Query query){
SearchResponse<U> searchResponse = esClient.search(s ->
s.index(indexName)
.query(query)
,classz);
return searchResponse;
}

Expand All @@ -208,35 +210,33 @@ private <U> ObjectBuilder<SearchRequest> deal(SearchRequest.Builder s, String in
}

/**
* 8、自定义查询条件查询
* 自定义查询条件查询
* 不使用分词器精确查找
*
* @param indexName 索引名称-即表名
* @param classz 表结构对应的实体类
* @param query 自定义的查询条件
* @param <U>
*/
@SneakyThrows
public <U> SearchResponse<U> getDocumentsByCustomize(String indexName, Class<U> classz, Query query) {
SearchResponse<U> searchResponse = elasticsearchClient.search(s ->
public <U> SearchResponse<U> getDocsByCustomize(String indexName, Class<U> classz, Query query) {
SearchResponse<U> searchResponse = esClient.search(s ->
s.index(indexName)
.query(query)
, classz);
return searchResponse;
}

/**
* 9、自定义多查询条件查询
* 自定义多查询条件查询
* 不使用分词器精确查找
*
* @param indexName 索引名称-即表名
* @param classz 表结构对应的实体类
* @param queryList 自定义的查询条件多查询条件查询
* @param <U>
*/
@SneakyThrows
public <U> SearchResponse<U> getDocumentsByCustomizeMore(String indexName, Class<U> classz, List<Query> queryList) {
SearchResponse<U> searchResponse = elasticsearchClient.search(s ->
SearchResponse<U> searchResponse = esClient.search(s ->
s.index(indexName)
.query(q -> q.bool(b -> b.must(queryList)))
, classz);
Expand All @@ -245,38 +245,45 @@ public <U> SearchResponse<U> getDocumentsByCustomizeMore(String indexName, Class


/**
* 10、es基于距离查询
*
* @param indexName
* @param classz
* @param geoDistanceQuery
* @param <U>
* @return
* es基于距离查询
*/
@SneakyThrows
public <U> SearchResponse<U> geoDistanceSelect(String indexName, Class<U> classz, GeoDistanceQuery geoDistanceQuery) {
SearchResponse<U> searchResponse = elasticsearchClient.search(s ->
SearchResponse<U> searchResponse = esClient.search(s ->
s.index(indexName)
.query(q -> q.bool(t -> t.must(k -> k.geoDistance(geoDistanceQuery)).queryName("location")))
, classz);
return searchResponse;
}

/**
* 10、es基于矩形范围查询
*
* @param indexName
* @param classz
* @param geoBoundingBoxQuery
* @param <U>
* @return
* es基于矩形范围查询
*/
@SneakyThrows
public <U> SearchResponse<U> geoBoundingBoxSelect(String indexName, Class<U> classz, GeoBoundingBoxQuery geoBoundingBoxQuery) {
SearchResponse<U> searchResponse = elasticsearchClient.search(s ->
SearchResponse<U> searchResponse = esClient.search(s ->
s.index(indexName)
.query(q -> q.bool(t -> t.must(k -> k.geoBoundingBox(geoBoundingBoxQuery)).queryName("location")))
, classz);
return searchResponse;
}

@SneakyThrows
public void getDocs(String indexName) {
GetIndexResponse getIndexResponse = esClient.indices().get(item -> item.index(indexName));
System.out.println(getIndexResponse);
}

// 判断索引是否存在
@SneakyThrows
public void existsIndex(String indexName) {
BooleanResponse booleanResponse = esClient.indices().exists(item -> item.index(indexName));
log.info("booleanResponse:{}", booleanResponse.value());
}

@SneakyThrows
public void deleteIndex(String indexName) {
DeleteIndexResponse deleteIndexResponse = esClient.indices().delete(item -> item.index(indexName));
log.info("deleteIndexResponse:{}", deleteIndexResponse.acknowledged());
}
}
Binary file added common/src/main/resources/lib/lombok-1.8.36.jar
Binary file not shown.
11 changes: 5 additions & 6 deletions domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

<artifactId>domain</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
Expand Down Expand Up @@ -109,6 +103,11 @@
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-orchestration-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
2 changes: 1 addition & 1 deletion education-back/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<!-- <version>${lombok.version}</version>-->
</path>
<path>
<groupId>org.mapstruct</groupId>
Expand Down
Loading

0 comments on commit 4802deb

Please sign in to comment.