优化ES搜索,修复关键词如果包含特殊字符导致被分词问题

This commit is contained in:
2026-07-13 16:46:06 +08:00
parent bcf2f1fa88
commit 0fd45a3c70
2 changed files with 51 additions and 14 deletions
@@ -5,6 +5,8 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType; import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField;
import xtools.app.common.config.AppCommonConfig; import xtools.app.common.config.AppCommonConfig;
/** /**
@@ -42,7 +44,12 @@ public class EsSysLog {
/** /**
* 日志追踪ID * 日志追踪ID
*/ */
@Field(type = FieldType.Text) @MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String traceId; private String traceId;
/** /**
@@ -72,25 +79,45 @@ public class EsSysLog {
/** /**
* 服务名称 * 服务名称
*/ */
@Field(type = FieldType.Text) @MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String appName; private String appName;
/** /**
* 本机IP * 本机IP
*/ */
@Field(type = FieldType.Text) @MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String localIp; private String localIp;
/** /**
* 运行端口 * 运行端口
*/ */
@Field(type = FieldType.Text) @MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String port; private String port;
/** /**
* 日志标题 * 日志标题
*/ */
@Field(type = FieldType.Text) @MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String title; private String title;
/** /**
@@ -108,13 +135,23 @@ public class EsSysLog {
/** /**
* 请求IP * 请求IP
*/ */
@Field(type = FieldType.Text) @MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String ip; private String ip;
/** /**
* 请求URI * 请求URI
*/ */
@Field(type = FieldType.Text) @MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String uri; private String uri;
/** /**
@@ -121,19 +121,19 @@ public class EsSysLogServiceImpl implements SysLogService, BaseParams {
JSONArray must = new JSONArray(); JSONArray must = new JSONArray();
EsQueryUtils.setTimeRange(must, "logTime", req.getLogTimeRange()); EsQueryUtils.setTimeRange(must, "logTime", req.getLogTimeRange());
EsQueryUtils.setMatchPhrase(must, "traceId", req.getTraceId()); EsQueryUtils.setWildcard(must, "traceId", req.getTraceId(), true);
EsQueryUtils.setMatchPhrase(must, "logType", req.getLogType()); EsQueryUtils.setMatchPhrase(must, "logType", req.getLogType());
EsQueryUtils.setMatchPhrase(must, "logLevel", req.getLogLevel()); EsQueryUtils.setMatchPhrase(must, "logLevel", req.getLogLevel());
EsQueryUtils.setWildcard(must, "title", req.getTitle()); EsQueryUtils.setWildcard(must, "title.keyword", req.getTitle(), true);
EsQueryUtils.setWildcard(must, "ip", req.getIp()); EsQueryUtils.setWildcard(must, "ip.keyword", req.getIp(), true);
EsQueryUtils.setWildcard(must, "uri", req.getUri()); EsQueryUtils.setWildcard(must, "uri.keyword", req.getUri(), true);
EsQueryUtils.setMatchPhrase(must, "threadType", req.getThreadType()); EsQueryUtils.setMatchPhrase(must, "threadType", req.getThreadType());
EsQueryUtils.setWildcard(must, "appName", req.getAppName()); EsQueryUtils.setWildcard(must, "appName.keyword", req.getAppName(), true);
EsQueryUtils.setWildcard(must, "localIp", req.getLocalIp()); EsQueryUtils.setWildcard(must, "localIp.keyword", req.getLocalIp(), true);
EsQueryUtils.setWildcard(must, "port", req.getPort()); EsQueryUtils.setWildcard(must, "port.keyword", req.getPort(), true);
JSONObject query = JSONObject.of("bool", JSONObject.of("must", must)); JSONObject query = JSONObject.of("bool", JSONObject.of("must", must));