Merge branch 'feature-3.0.1' of gitee.com:dromara/easy-es into feature-3.0.1

Signed-off-by: liaozhehao <2427800517@qq.com>
This commit is contained in:
liaozhehao 2025-09-23 12:29:07 +00:00 committed by Gitee
commit 976378e47d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 191 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport; import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpRequestInterceptor;
@ -126,7 +127,10 @@ public class EsClientUtils {
.ifPresent(requestConfigBuilder::setConnectionRequestTimeout); .ifPresent(requestConfigBuilder::setConnectionRequestTimeout);
return requestConfigBuilder; return requestConfigBuilder;
}); });
// 如果是驼峰转下划线, 则增加序列化器
if (easyEsConfigProperties.getGlobalConfig().getDbConfig().isMapUnderscoreToCamelCase()) {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
}
return new ElasticsearchClient(new RestClientTransport(builder.build(), return new ElasticsearchClient(new RestClientTransport(builder.build(),
new JacksonJsonpMapper(objectMapper))); new JacksonJsonpMapper(objectMapper)));
} }

View File

@ -1398,18 +1398,33 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
private IndexRequest.Builder<T> buildIndexRequest(T entity, String routing, String parentId, String indexName) { private IndexRequest.Builder<T> buildIndexRequest(T entity, String routing, String parentId, String indexName) {
IndexRequest.Builder<T> indexRequest = new IndexRequest.Builder<T>() IndexRequest.Builder<T> indexRequest = new IndexRequest.Builder<T>()
.routing(routing) .routing(routing)
.index(indexName) .index(indexName);
.document(entity);
// id预处理,除下述情况,其它情况使用es默认的id // id预处理,除下述情况,其它情况使用es默认的id
EntityInfo entityInfo = EntityInfoHelper.getEntityInfo(entityClass); EntityInfo entityInfo = EntityInfoHelper.getEntityInfo(entityClass);
if (IdType.UUID.equals(entityInfo.getIdType())) { if (IdType.UUID.equals(entityInfo.getIdType())) {
indexRequest.id(UUID.randomUUID().toString()); String uuid = UUID.randomUUID().toString();
} else if (IdType.CUSTOMIZE.equals(entityInfo.getIdType())) { indexRequest.id(uuid);
indexRequest.id(getIdValue(entity)); // 如果id需要存入_source
if (entityInfo.isId2Source()) {
try {
entityInfo.getKeyField().set(entity, uuid);
} catch (IllegalAccessException e) {
throw ExceptionUtils.eee(e);
} }
}
} else if (IdType.CUSTOMIZE.equals(entityInfo.getIdType())) {
String idValue = getIdValue(entity);
indexRequest.id(idValue);
if (entityInfo.isId2Source()) {
try {
entityInfo.getKeyField().set(entity, idValue);
} catch (IllegalAccessException e) {
throw ExceptionUtils.eee(e);
}
}
}
indexRequest.document(entity);
// 针对父子类型-追加joinField信息 // 针对父子类型-追加joinField信息
if (StringUtils.isNotBlank(entityInfo.getJoinAlias())) { if (StringUtils.isNotBlank(entityInfo.getJoinAlias())) {
if (!(entity instanceof BaseJoin)) { if (!(entity instanceof BaseJoin)) {

View File

@ -0,0 +1,57 @@
package org.dromara.easyes.test.entity;
import lombok.Data;
import org.dromara.easyes.annotation.IndexField;
import org.dromara.easyes.annotation.IndexId;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.annotation.rely.IdType;
/**
* @author MoJie
* @since 2025-09-19
*/
@Data
public class BaseEsEntity {
/**
* 文档ID使用UUID策略自动生成
*/
@IndexId(type = IdType.UUID)
private String id;
/**
* 创建时间
*/
@IndexField(fieldType = FieldType.KEYWORD)
private String createTime;
/**
* 创建人
*/
@IndexField(fieldType = FieldType.KEYWORD)
private String createBy;
/**
* 更新时间
*/
@IndexField(fieldType = FieldType.KEYWORD)
private String updateTime;
/**
* 更新人
*/
@IndexField(fieldType = FieldType.KEYWORD)
private String updateBy;
/**
* 是否删除逻辑删除标记
*/
@IndexField(fieldType = FieldType.BOOLEAN)
private Boolean deleted = false;
/**
* 版本号用于乐观锁
*/
@IndexField(fieldType = FieldType.LONG)
private Long version = 1L;
}

View File

@ -0,0 +1,66 @@
package org.dromara.easyes.test.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.dromara.easyes.annotation.IndexField;
import org.dromara.easyes.annotation.IndexName;
import org.dromara.easyes.annotation.rely.Analyzer;
import org.dromara.easyes.annotation.rely.FieldType;
/**
* @author MoJie
* @since 2025-09-19
*/
@Data
@IndexName(value = "user")
@EqualsAndHashCode(callSuper = true)
public class UserEntity extends BaseEsEntity {
/**
* 用户名精确匹配
*/
@IndexField(fieldType = FieldType.KEYWORD)
private String username;
/**
* 姓名支持分词搜索
*/
@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
private String name;
/**
* 年龄
*/
@IndexField(fieldType = FieldType.INTEGER)
private Integer age;
/**
* 邮箱精确匹配
*/
@IndexField(fieldType = FieldType.KEYWORD)
private String email;
/**
* 地址支持分词搜索
*/
@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
private String address;
/**
* 用户状态0-禁用1-启用
*/
@IndexField(fieldType = FieldType.INTEGER)
private Integer status = 1;
/**
* 用户标签支持多值
*/
@IndexField(fieldType = FieldType.KEYWORD)
private String[] tags;
/**
* 用户描述支持分词搜索
*/
@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
private String description;
}

View File

@ -0,0 +1,11 @@
package org.dromara.easyes.test.mapper;
import org.dromara.easyes.core.kernel.BaseEsMapper;
import org.dromara.easyes.test.entity.UserEntity;
/**
* @author MoJie
* @since 2025-09-19
*/
public interface UserMapper extends BaseEsMapper<UserEntity> {
}

View File

@ -58,6 +58,7 @@ public class AllTest {
@Test @Test
@Order(0) @Order(0)
public void testCreateIndex() { public void testCreateIndex() {
documentMapper.deleteIndex();
// 0.前置操作 创建索引 需确保索引托管模式处于manual手动挡,若为自动挡则会冲突. // 0.前置操作 创建索引 需确保索引托管模式处于manual手动挡,若为自动挡则会冲突.
boolean success = documentMapper.createIndex(); boolean success = documentMapper.createIndex();
Assertions.assertTrue(success); Assertions.assertTrue(success);

View File

@ -1,8 +1,11 @@
package org.dromara.easyes.test.insert; package org.dromara.easyes.test.insert;
import org.dromara.easyes.common.utils.DateUtil;
import org.dromara.easyes.test.TestEasyEsApplication; import org.dromara.easyes.test.TestEasyEsApplication;
import org.dromara.easyes.test.entity.Document; import org.dromara.easyes.test.entity.Document;
import org.dromara.easyes.test.entity.UserEntity;
import org.dromara.easyes.test.mapper.DocumentMapper; import org.dromara.easyes.test.mapper.DocumentMapper;
import org.dromara.easyes.test.mapper.UserMapper;
import org.elasticsearch.geometry.Point; import org.elasticsearch.geometry.Point;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
@ -25,6 +28,8 @@ import java.util.List;
public class InsertTest { public class InsertTest {
@Resource @Resource
private DocumentMapper documentMapper; private DocumentMapper documentMapper;
@Resource
private UserMapper userMapper;
@Test @Test
public void testInsert() { public void testInsert() {
@ -42,7 +47,7 @@ public class InsertTest {
document.setGeoLocation(point.toString()); document.setGeoLocation(point.toString());
document.setStarNum(0); document.setStarNum(0);
int successCount = documentMapper.insert(document); int successCount = documentMapper.insert(document);
Assertions.assertEquals(successCount, 1); Assertions.assertEquals(1, successCount);
} }
@Test @Test
@ -112,4 +117,26 @@ public class InsertTest {
System.out.println("更新后的数据:" + document1); System.out.println("更新后的数据:" + document1);
} }
@Test
public void testCreateUser() {
// 创建测试用户
UserEntity user = new UserEntity();
user.setUsername("tester001");
user.setName("张三");
user.setAge(25);
user.setEmail("zhangsan@example.com");
user.setAddress("北京市朝阳区");
user.setStatus(1);
user.setTags(new String[]{"Java", "Spring", "Elasticsearch"});
user.setDescription("这是一个测试用户专门用于演示Easy-ES的功能");
user.setVersion(2L);
user.setDeleted(true);
user.setCreateBy("system");
user.setUpdateBy("system");
user.setCreateTime(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
user.setUpdateTime(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
// 创建用户
Assertions.assertEquals(1, userMapper.insert(user));
}
} }

View File

@ -1,6 +1,6 @@
easy-es: easy-es:
# enable: true # enable: true
address: 127.0.0.1:9200 address: 192.168.0.18:9200
compatible: true compatible: true
schema: https schema: https
username: elastic username: elastic
@ -8,7 +8,7 @@ easy-es:
keep-alive-millis: 18000 keep-alive-millis: 18000
global-config: global-config:
i-kun-mode: false i-kun-mode: false
process-index-mode: manual process-index-mode: smoothly
async-process-index-blocking: true async-process-index-blocking: true
print-dsl: true print-dsl: true
db-config: db-config: