mirror of
https://gitee.com/dromara/easy-es.git
synced 2025-12-07 09:39:04 +08:00
feat: 用户可以自定义是否在source中存储id列
This commit is contained in:
parent
bdc279fe54
commit
aa7c1cf4a6
@ -29,4 +29,9 @@ public @interface IndexId {
|
|||||||
* @return 默认为未设置
|
* @return 默认为未设置
|
||||||
*/
|
*/
|
||||||
IdType type() default IdType.NONE;
|
IdType type() default IdType.NONE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否将主键写入到source中
|
||||||
|
*/
|
||||||
|
boolean writeToSource() default false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,6 +71,10 @@ public class GlobalConfig {
|
|||||||
* es id generate type. es id生成类型 默认由es自动生成
|
* es id generate type. es id生成类型 默认由es自动生成
|
||||||
*/
|
*/
|
||||||
private IdType idType = IdType.NONE;
|
private IdType idType = IdType.NONE;
|
||||||
|
/**
|
||||||
|
* es id是否写入到source中
|
||||||
|
*/
|
||||||
|
private boolean id2Source = false;
|
||||||
/**
|
/**
|
||||||
* Field update strategy default nonNull 字段更新策略,默认非null
|
* Field update strategy default nonNull 字段更新策略,默认非null
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -39,6 +39,10 @@ public class EntityInfo {
|
|||||||
* id数据类型 如Long.class String.class
|
* id数据类型 如Long.class String.class
|
||||||
*/
|
*/
|
||||||
private Class<?> idClass;
|
private Class<?> idClass;
|
||||||
|
/**
|
||||||
|
* id列是否需要写入到source
|
||||||
|
*/
|
||||||
|
private boolean id2Source;
|
||||||
/**
|
/**
|
||||||
* 索引名称(原索引名)
|
* 索引名称(原索引名)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import org.dromara.easyes.core.toolkit.FieldUtils;
|
|||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -36,9 +37,7 @@ public class JacksonCache {
|
|||||||
|
|
||||||
// 当前类
|
// 当前类
|
||||||
if (!JacksonCustomConfig.jacksonConfigMap.containsKey(clz)) {
|
if (!JacksonCustomConfig.jacksonConfigMap.containsKey(clz)) {
|
||||||
JacksonCustomConfig config = init(clz, e.getMappingColumnMap(), e.getClassDateFormatMap().get(clz), e.getFieldList());
|
JacksonCustomConfig config = init(clz, e.isId2Source(), e.getKeyProperty(), e.getMappingColumnMap(), e.getClassDateFormatMap().get(clz), e.getFieldList());
|
||||||
config.includeMap.put(e.getKeyProperty(), NON_NULL);
|
|
||||||
config.includeMap.put(getterMethod(clz, e.getKeyProperty()), NON_NULL);
|
|
||||||
JacksonCustomConfig.jacksonConfigMap.put(clz, config);
|
JacksonCustomConfig.jacksonConfigMap.put(clz, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +46,7 @@ public class JacksonCache {
|
|||||||
if (JacksonCustomConfig.jacksonConfigMap.containsKey(clz)) {
|
if (JacksonCustomConfig.jacksonConfigMap.containsKey(clz)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JacksonCustomConfig c = init(nestedClz, nestedMappingColumnMap,
|
JacksonCustomConfig c = init(nestedClz, null, null, nestedMappingColumnMap,
|
||||||
e.getClassDateFormatMap().get(nestedClz),
|
e.getClassDateFormatMap().get(nestedClz),
|
||||||
e.getNestedOrObjectFieldListMap().get(nestedClz)
|
e.getNestedOrObjectFieldListMap().get(nestedClz)
|
||||||
);
|
);
|
||||||
@ -58,6 +57,8 @@ public class JacksonCache {
|
|||||||
|
|
||||||
private static JacksonCustomConfig init(
|
private static JacksonCustomConfig init(
|
||||||
Class<?> clz,
|
Class<?> clz,
|
||||||
|
Boolean id2Source,
|
||||||
|
String idJavaFieldName,
|
||||||
Map<String, String> mappingColumnMap,
|
Map<String, String> mappingColumnMap,
|
||||||
Map<String, String> formatMap,
|
Map<String, String> formatMap,
|
||||||
List<EntityFieldInfo> fieldList
|
List<EntityFieldInfo> fieldList
|
||||||
@ -69,7 +70,11 @@ public class JacksonCache {
|
|||||||
config.allJsonField = JsonIncludeProperties.Value.from(new JsonIncludeProperties() {
|
config.allJsonField = JsonIncludeProperties.Value.from(new JsonIncludeProperties() {
|
||||||
@Override
|
@Override
|
||||||
public String[] value() {
|
public String[] value() {
|
||||||
return config.javaJsonFieldNameMap.values().toArray(new String[0]);
|
Collection<String> values = config.javaJsonFieldNameMap.values();
|
||||||
|
if (id2Source != null && !id2Source) {
|
||||||
|
values.remove(idJavaFieldName);
|
||||||
|
}
|
||||||
|
return values.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -112,6 +117,10 @@ public class JacksonCache {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (id2Source != null && id2Source) {
|
||||||
|
config.includeMap.put(idJavaFieldName, NON_NULL);
|
||||||
|
config.includeMap.put(getterMethod(clz, idJavaFieldName), NON_NULL);
|
||||||
|
}
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -620,6 +620,7 @@ public class EntityInfoHelper {
|
|||||||
} else {
|
} else {
|
||||||
entityInfo.setIdType(tableId.type());
|
entityInfo.setIdType(tableId.type());
|
||||||
}
|
}
|
||||||
|
entityInfo.setId2Source(tableId.writeToSource());
|
||||||
// 字段
|
// 字段
|
||||||
field.setAccessible(Boolean.TRUE);
|
field.setAccessible(Boolean.TRUE);
|
||||||
entityInfo.setClazz(field.getDeclaringClass())
|
entityInfo.setClazz(field.getDeclaringClass())
|
||||||
@ -660,6 +661,7 @@ public class EntityInfoHelper {
|
|||||||
String mappingColumn = getMappingColumn(dbConfig, field);
|
String mappingColumn = getMappingColumn(dbConfig, field);
|
||||||
entityInfo.getMappingColumnMap().putIfAbsent(column, mappingColumn);
|
entityInfo.getMappingColumnMap().putIfAbsent(column, mappingColumn);
|
||||||
entityInfo.getColumnMappingMap().putIfAbsent(mappingColumn, column);
|
entityInfo.getColumnMappingMap().putIfAbsent(mappingColumn, column);
|
||||||
|
entityInfo.setId2Source(dbConfig.isId2Source());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -437,11 +437,13 @@ public class IndexUtils {
|
|||||||
private static Map<String, Property> initInfo(EntityInfo entityInfo, GlobalConfig.DbConfig dbConfig,
|
private static Map<String, Property> initInfo(EntityInfo entityInfo, GlobalConfig.DbConfig dbConfig,
|
||||||
Map<String, Property> properties, List<EsIndexParam> indexParamList) {
|
Map<String, Property> properties, List<EsIndexParam> indexParamList) {
|
||||||
// 主键
|
// 主键
|
||||||
String idFieldName = entityInfo.getKeyProperty();
|
if (entityInfo.isId2Source()) {
|
||||||
if (dbConfig.isMapUnderscoreToCamelCase()) {
|
String idFieldName = entityInfo.getKeyProperty();
|
||||||
idFieldName = StringUtils.camelToUnderline(idFieldName);
|
if (dbConfig.isMapUnderscoreToCamelCase()) {
|
||||||
|
idFieldName = StringUtils.camelToUnderline(idFieldName);
|
||||||
|
}
|
||||||
|
properties.put(idFieldName, KeywordProperty.of(a -> a)._toProperty());
|
||||||
}
|
}
|
||||||
properties.put(idFieldName, KeywordProperty.of(a -> a)._toProperty());
|
|
||||||
|
|
||||||
// 其他字段
|
// 其他字段
|
||||||
indexParamList.forEach(indexParam -> {
|
indexParamList.forEach(indexParam -> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user