feat: 完善打印sql和dsl

This commit is contained in:
jaime 2025-02-27 16:34:27 +08:00
parent 2feafdef30
commit 2533cb4014
8 changed files with 87 additions and 56 deletions

View File

@ -150,6 +150,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
public Integer refresh(String... indexNames) { public Integer refresh(String... indexNames) {
RefreshRequest request = RefreshRequest.of(x -> x.index(Arrays.asList(indexNames))); RefreshRequest request = RefreshRequest.of(x -> x.index(Arrays.asList(indexNames)));
try { try {
PrintUtils.printDsl(request, client);
RefreshResponse refresh = client.indices().refresh(request); RefreshResponse refresh = client.indices().refresh(request);
if (refresh.shards() != null) { if (refresh.shards() != null) {
return refresh.shards().successful().intValue(); return refresh.shards().successful().intValue();
@ -164,6 +165,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
@Override @Override
@SneakyThrows @SneakyThrows
public String executeSQL(String sql) { public String executeSQL(String sql) {
PrintUtils.printSql(sql);
QueryResponse response = client.sql().query(x -> x.query(sql).format(SqlFormat.Json)); QueryResponse response = client.sql().query(x -> x.query(sql).format(SqlFormat.Json));
return JsonpUtils.toString(response, new StringBuilder()).toString(); return JsonpUtils.toString(response, new StringBuilder()).toString();
} }
@ -182,6 +184,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
Assert.notNull(endpoint, "endpoint must not null"); Assert.notNull(endpoint, "endpoint must not null");
Request request = new Request(method, endpoint); Request request = new Request(method, endpoint);
request.setJsonEntity(dsl); request.setJsonEntity(dsl);
PrintUtils.printDsl(method, endpoint, dsl);
Response response = ((RestClientTransport) client._transport()).restClient().performRequest(request); Response response = ((RestClientTransport) client._transport()).restClient().performRequest(request);
return EntityUtils.toString(response.getEntity()); return EntityUtils.toString(response.getEntity());
} }
@ -194,6 +197,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
@Override @Override
public ScrollResponse<T> scroll(ScrollRequest searchScrollRequest, TransportOptions requestOptions) throws IOException { public ScrollResponse<T> scroll(ScrollRequest searchScrollRequest, TransportOptions requestOptions) throws IOException {
PrintUtils.printDsl(searchScrollRequest, client);
ScrollResponse<T> response = client.withTransportOptions(requestOptions).scroll(searchScrollRequest, entityClass); ScrollResponse<T> response = client.withTransportOptions(requestOptions).scroll(searchScrollRequest, entityClass);
printResponseErrors(response); printResponseErrors(response);
return response; return response;
@ -679,6 +683,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
); );
try { try {
PrintUtils.printDsl(putMappingRequest, client);
AcknowledgedResponse acknowledgedResponse = client.withTransportOptions(getTransportOptions()).indices().putMapping(putMappingRequest); AcknowledgedResponse acknowledgedResponse = client.withTransportOptions(getTransportOptions()).indices().putMapping(putMappingRequest);
Assert.isTrue(acknowledgedResponse.acknowledged(), String.format("update index failed, index: %s", indexName)); Assert.isTrue(acknowledgedResponse.acknowledged(), String.format("update index failed, index: %s", indexName));
} catch (IOException e) { } catch (IOException e) {

View File

@ -455,7 +455,7 @@ public class IndexUtils {
if (FieldType.BYTE.getType().equals(indexParam.getFieldType())) { if (FieldType.BYTE.getType().equals(indexParam.getFieldType())) {
ByteNumberProperty property = ByteNumberProperty.of(a -> { ByteNumberProperty property = ByteNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -465,7 +465,7 @@ public class IndexUtils {
} }
if (FieldType.SHORT.getType().equals(indexParam.getFieldType())) { if (FieldType.SHORT.getType().equals(indexParam.getFieldType())) {
ShortNumberProperty property = ShortNumberProperty.of(a -> { ShortNumberProperty property = ShortNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -475,7 +475,7 @@ public class IndexUtils {
} }
if (FieldType.INTEGER.getType().equals(indexParam.getFieldType())) { if (FieldType.INTEGER.getType().equals(indexParam.getFieldType())) {
IntegerNumberProperty property = IntegerNumberProperty.of(a -> { IntegerNumberProperty property = IntegerNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -485,7 +485,7 @@ public class IndexUtils {
} }
if (FieldType.LONG.getType().equals(indexParam.getFieldType())) { if (FieldType.LONG.getType().equals(indexParam.getFieldType())) {
LongNumberProperty property = LongNumberProperty.of(a -> { LongNumberProperty property = LongNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -495,7 +495,7 @@ public class IndexUtils {
} }
if (FieldType.FLOAT.getType().equals(indexParam.getFieldType())) { if (FieldType.FLOAT.getType().equals(indexParam.getFieldType())) {
FloatNumberProperty property = FloatNumberProperty.of(a -> { FloatNumberProperty property = FloatNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -505,7 +505,7 @@ public class IndexUtils {
} }
if (FieldType.DOUBLE.getType().equals(indexParam.getFieldType())) { if (FieldType.DOUBLE.getType().equals(indexParam.getFieldType())) {
DoubleNumberProperty property = DoubleNumberProperty.of(a -> { DoubleNumberProperty property = DoubleNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -515,7 +515,7 @@ public class IndexUtils {
} }
if (FieldType.HALF_FLOAT.getType().equals(indexParam.getFieldType())) { if (FieldType.HALF_FLOAT.getType().equals(indexParam.getFieldType())) {
HalfFloatNumberProperty property = HalfFloatNumberProperty.of(a -> { HalfFloatNumberProperty property = HalfFloatNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -527,7 +527,7 @@ public class IndexUtils {
Double scalingFactor = Optional.ofNullable(indexParam.getScalingFactor()) Double scalingFactor = Optional.ofNullable(indexParam.getScalingFactor())
.map(NumericUtils::formatNumberWithOneDecimal).orElse(DEFAULT_SCALING_FACTOR); .map(NumericUtils::formatNumberWithOneDecimal).orElse(DEFAULT_SCALING_FACTOR);
ScaledFloatNumberProperty property = ScaledFloatNumberProperty.of(a -> { ScaledFloatNumberProperty property = ScaledFloatNumberProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
a.scalingFactor(scalingFactor); a.scalingFactor(scalingFactor);
@ -538,7 +538,7 @@ public class IndexUtils {
} }
if (FieldType.BOOLEAN.getType().equals(indexParam.getFieldType())) { if (FieldType.BOOLEAN.getType().equals(indexParam.getFieldType())) {
BooleanProperty property = BooleanProperty.of(a -> { BooleanProperty property = BooleanProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -548,7 +548,7 @@ public class IndexUtils {
} }
if (FieldType.DATE.getType().equals(indexParam.getFieldType())) { if (FieldType.DATE.getType().equals(indexParam.getFieldType())) {
DateProperty property = DateProperty.of(a -> { DateProperty property = DateProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
a.format(indexParam.getDateFormat()); a.format(indexParam.getDateFormat());
@ -568,7 +568,7 @@ public class IndexUtils {
} }
if (FieldType.KEYWORD.getType().equals(indexParam.getFieldType())) { if (FieldType.KEYWORD.getType().equals(indexParam.getFieldType())) {
KeywordProperty property = KeywordProperty.of(a -> { KeywordProperty property = KeywordProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
a.normalizer(indexParam.isIgnoreCase() ? LOWERCASE_NORMALIZER : null); a.normalizer(indexParam.isIgnoreCase() ? LOWERCASE_NORMALIZER : null);
@ -580,7 +580,8 @@ public class IndexUtils {
if (FieldType.TEXT.getType().equals(indexParam.getFieldType())) { if (FieldType.TEXT.getType().equals(indexParam.getFieldType())) {
int ignoreAbove = Optional.ofNullable(indexParam.getIgnoreAbove()).orElse(DEFAULT_IGNORE_ABOVE); int ignoreAbove = Optional.ofNullable(indexParam.getIgnoreAbove()).orElse(DEFAULT_IGNORE_ABOVE);
TextProperty property = TextProperty.of(a -> { TextProperty property = TextProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
Optional.ofNullable(indexParam.getAnalyzer()).map(String::toLowerCase).ifPresent(a::analyzer); Optional.ofNullable(indexParam.getAnalyzer()).map(String::toLowerCase).ifPresent(a::analyzer);
@ -594,7 +595,7 @@ public class IndexUtils {
if (FieldType.KEYWORD_TEXT.getType().equals(indexParam.getFieldType())) { if (FieldType.KEYWORD_TEXT.getType().equals(indexParam.getFieldType())) {
int ignoreAbove = Optional.ofNullable(indexParam.getIgnoreAbove()).orElse(DEFAULT_IGNORE_ABOVE); int ignoreAbove = Optional.ofNullable(indexParam.getIgnoreAbove()).orElse(DEFAULT_IGNORE_ABOVE);
TextProperty property = TextProperty.of(a -> { TextProperty property = TextProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
a.fields(FieldType.KEYWORD.getType(), c -> c a.fields(FieldType.KEYWORD.getType(), c -> c
@ -675,7 +676,7 @@ public class IndexUtils {
} }
if (FieldType.IP.getType().equals(indexParam.getFieldType())) { if (FieldType.IP.getType().equals(indexParam.getFieldType())) {
IpProperty property = IpProperty.of(a -> { IpProperty property = IpProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;
@ -696,7 +697,7 @@ public class IndexUtils {
} }
if (FieldType.TOKEN.getType().equals(indexParam.getFieldType())) { if (FieldType.TOKEN.getType().equals(indexParam.getFieldType())) {
TokenCountProperty property = TokenCountProperty.of(a -> { TokenCountProperty property = TokenCountProperty.of(a -> {
Optional.ofNullable(indexParam.getBoost()).ifPresent(a::boost); a.boost(indexParam.getBoost());
buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList()); buildCopyTo(a, entityInfo.isIndexEqualStage(), indexParam.getCopyToList());
buildInnerFields(a, indexParam); buildInnerFields(a, indexParam);
return a; return a;

View File

@ -57,6 +57,17 @@ public class PrintUtils {
// No endpoint, ignore // No endpoint, ignore
} }
printDsl(method, fullUrl, dsl);
}
/**
* 打印dsl
*
* @param method 请求方法
* @param fullUrl 请求路径
* @param dsl dsl
*/
public static void printDsl(String method, String fullUrl, String dsl) {
GlobalConfig globalConfig = GlobalConfigCache.getGlobalConfig(); GlobalConfig globalConfig = GlobalConfigCache.getGlobalConfig();
if (globalConfig.isPrintDsl()) { if (globalConfig.isPrintDsl()) {
String prefix = globalConfig.isIKunMode() ? I_KUN_PREFIX : DSL_PREFIX; String prefix = globalConfig.isIKunMode() ? I_KUN_PREFIX : DSL_PREFIX;
@ -67,6 +78,19 @@ public class PrintUtils {
} }
} }
/**
* 打印dsl
*
* @param sql sql
*/
public static void printSql(String sql) {
GlobalConfig globalConfig = GlobalConfigCache.getGlobalConfig();
if (globalConfig.isPrintDsl()) {
String prefix = globalConfig.isIKunMode() ? I_KUN_PREFIX : DSL_PREFIX;
LogUtils.info(prefix + "\n" + sql);
}
}
/** /**
* 根据全局配置决定是否控制台打印DSL语句 * 根据全局配置决定是否控制台打印DSL语句
* *

View File

@ -35,7 +35,7 @@ public class Document extends BaseJoin {
* es中的唯一id,字段名随便起,我这里演示用esId,你也可以用id(推荐),bizId等. * es中的唯一id,字段名随便起,我这里演示用esId,你也可以用id(推荐),bizId等.
* 如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize或直接在全局配置文件中指定,如此id便支持任意数据类型) * 如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize或直接在全局配置文件中指定,如此id便支持任意数据类型)
*/ */
@IndexId(type = IdType.CUSTOMIZE) @IndexId(type = IdType.CUSTOMIZE, writeToSource = true)
private String esId; private String esId;
/** /**
* 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询 * 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询

View File

@ -10,6 +10,7 @@ import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.json.JsonData; import co.elastic.clients.json.JsonData;
import co.elastic.clients.transport.rest_client.RestClientOptions; import co.elastic.clients.transport.rest_client.RestClientOptions;
import org.dromara.easyes.common.constants.BaseEsConstants;
import org.dromara.easyes.core.biz.EntityInfo; import org.dromara.easyes.core.biz.EntityInfo;
import org.dromara.easyes.core.biz.EsPageInfo; import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.biz.OrderByParam; import org.dromara.easyes.core.biz.OrderByParam;
@ -675,7 +676,7 @@ public class AllTest {
wrapper.match(Document::getCreator, "老汉"); wrapper.match(Document::getCreator, "老汉");
List<OrderByParam> orderByParams = new ArrayList<>(); List<OrderByParam> orderByParams = new ArrayList<>();
OrderByParam orderByParam = new OrderByParam(); OrderByParam orderByParam = new OrderByParam();
orderByParam.setOrder("star_num"); orderByParam.setOrder("starNum");
orderByParam.setSort("DESC"); orderByParam.setSort("DESC");
orderByParams.add(orderByParam); orderByParams.add(orderByParam);
wrapper.orderBy(orderByParams); wrapper.orderBy(orderByParams);
@ -940,40 +941,40 @@ public class AllTest {
Assertions.assertTrue(success); Assertions.assertTrue(success);
} }
// // 4.删除 // 4.删除
// @Test @Test
// @Order(76) @Order(76)
// public void testDeleteById() { public void testDeleteById() {
// int count = documentMapper.deleteById("1"); int count = documentMapper.deleteById("1");
// Assertions.assertEquals(1, count); Assertions.assertEquals(1, count);
// } }
//
// @Test @Test
// @Order(77) @Order(77)
// public void testDeleteBatchIds() { public void testDeleteBatchIds() {
// List<String> idList = Arrays.asList("2", "3", "4"); List<String> idList = Arrays.asList("2", "3", "4");
// int count = documentMapper.deleteBatchIds(idList); int count = documentMapper.deleteBatchIds(idList);
// Assertions.assertEquals(3, count); Assertions.assertEquals(3, count);
// } }
//
// @Test @Test
// @Order(78) @Order(78)
// public void testDeleteByWrapper() { public void testDeleteByWrapper() {
// LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>(); LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
// wrapper.match(Document::getCreator, "老汉"); wrapper.match(Document::getCreator, "老汉");
//
// int count = documentMapper.delete(wrapper); int count = documentMapper.delete(wrapper);
// Assertions.assertEquals(18, count); Assertions.assertEquals(18, count);
// } }
//
// @Test @Test
// @Order(80) @Order(80)
// public void testDeleteIndex() { public void testDeleteIndex() {
// boolean deleted = documentMapper.deleteIndex(EntityInfoHelper.getEntityInfo(Document.class).getIndexName()); boolean deleted = documentMapper.deleteIndex(EntityInfoHelper.getEntityInfo(Document.class).getIndexName());
// boolean lockDeleted = documentMapper.deleteIndex(BaseEsConstants.LOCK_INDEX); boolean lockDeleted = documentMapper.deleteIndex(BaseEsConstants.LOCK_INDEX);
// Assertions.assertTrue(deleted); Assertions.assertTrue(deleted);
// Assertions.assertTrue(lockDeleted); Assertions.assertTrue(lockDeleted);
// } }
@Test @Test
@Order(79) @Order(79)

View File

@ -51,7 +51,7 @@ public class IndexTest {
wrapper.indexName(Document.class.getSimpleName().toLowerCase()); wrapper.indexName(Document.class.getSimpleName().toLowerCase());
// 此处将文章标题映射为keyword类型(不支持分词),文档内容映射为text类型(支持分词查询) // 此处将文章标题映射为keyword类型(不支持分词),文档内容映射为text类型(支持分词查询)
wrapper.mapping(Document::getTitle, FieldType.KEYWORD, 2.0) wrapper.mapping(Document::getTitle, FieldType.KEYWORD)
.mapping(Document::getLocation, FieldType.GEO_POINT) .mapping(Document::getLocation, FieldType.GEO_POINT)
.mapping(Document::getGeoLocation, FieldType.GEO_SHAPE) .mapping(Document::getGeoLocation, FieldType.GEO_SHAPE)
.mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD); .mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);

View File

@ -6,12 +6,12 @@ easy-es:
password: mg123456 password: mg123456
keep-alive-millis: 18000 keep-alive-millis: 18000
global-config: global-config:
i-kun-mode: true i-kun-mode: false
process-index-mode: manual process-index-mode: manual
async-process-index-blocking: true async-process-index-blocking: true
print-dsl: true print-dsl: true
db-config: db-config:
map-underscore-to-camel-case: true map-underscore-to-camel-case: false
id-type: customize id-type: customize
field-strategy: not_empty field-strategy: not_empty
refresh-policy: immediate refresh-policy: immediate