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

@ -72,7 +72,7 @@ public interface Index<Children, R> extends Serializable {
/**
* 设置mapping信息
*
* @param column
* @param column
* @param fieldType es中的类型
* @param boost 权重
* @return wrapper

View File

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

View File

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

View File

@ -57,6 +57,17 @@ public class PrintUtils {
// 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();
if (globalConfig.isPrintDsl()) {
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语句
*
@ -131,7 +155,7 @@ public class PrintUtils {
while (values.hasNext()) {
Object item = values.next();
if (item == null) {
// skip
// skip
} else if (item instanceof NdJsonpSerializable nd && item != value) { // do not recurse on the item itself
collectNdJsonLines(body, nd, mapper);
} else {

View File

@ -35,7 +35,7 @@ public class Document extends BaseJoin {
* es中的唯一id,字段名随便起,我这里演示用esId,你也可以用id(推荐),bizId等.
* 如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize或直接在全局配置文件中指定,如此id便支持任意数据类型)
*/
@IndexId(type = IdType.CUSTOMIZE)
@IndexId(type = IdType.CUSTOMIZE, writeToSource = true)
private String esId;
/**
* 文档标题,不指定类型默认被创建为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.json.JsonData;
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.EsPageInfo;
import org.dromara.easyes.core.biz.OrderByParam;
@ -675,7 +676,7 @@ public class AllTest {
wrapper.match(Document::getCreator, "老汉");
List<OrderByParam> orderByParams = new ArrayList<>();
OrderByParam orderByParam = new OrderByParam();
orderByParam.setOrder("star_num");
orderByParam.setOrder("starNum");
orderByParam.setSort("DESC");
orderByParams.add(orderByParam);
wrapper.orderBy(orderByParams);
@ -940,40 +941,40 @@ public class AllTest {
Assertions.assertTrue(success);
}
// // 4.删除
// @Test
// @Order(76)
// public void testDeleteById() {
// int count = documentMapper.deleteById("1");
// Assertions.assertEquals(1, count);
// }
//
// @Test
// @Order(77)
// public void testDeleteBatchIds() {
// List<String> idList = Arrays.asList("2", "3", "4");
// int count = documentMapper.deleteBatchIds(idList);
// Assertions.assertEquals(3, count);
// }
//
// @Test
// @Order(78)
// public void testDeleteByWrapper() {
// LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
// wrapper.match(Document::getCreator, "老汉");
//
// int count = documentMapper.delete(wrapper);
// Assertions.assertEquals(18, count);
// }
//
// @Test
// @Order(80)
// public void testDeleteIndex() {
// boolean deleted = documentMapper.deleteIndex(EntityInfoHelper.getEntityInfo(Document.class).getIndexName());
// boolean lockDeleted = documentMapper.deleteIndex(BaseEsConstants.LOCK_INDEX);
// Assertions.assertTrue(deleted);
// Assertions.assertTrue(lockDeleted);
// }
// 4.删除
@Test
@Order(76)
public void testDeleteById() {
int count = documentMapper.deleteById("1");
Assertions.assertEquals(1, count);
}
@Test
@Order(77)
public void testDeleteBatchIds() {
List<String> idList = Arrays.asList("2", "3", "4");
int count = documentMapper.deleteBatchIds(idList);
Assertions.assertEquals(3, count);
}
@Test
@Order(78)
public void testDeleteByWrapper() {
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getCreator, "老汉");
int count = documentMapper.delete(wrapper);
Assertions.assertEquals(18, count);
}
@Test
@Order(80)
public void testDeleteIndex() {
boolean deleted = documentMapper.deleteIndex(EntityInfoHelper.getEntityInfo(Document.class).getIndexName());
boolean lockDeleted = documentMapper.deleteIndex(BaseEsConstants.LOCK_INDEX);
Assertions.assertTrue(deleted);
Assertions.assertTrue(lockDeleted);
}
@Test
@Order(79)

View File

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

View File

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