Skip to content

Commit 72e21f4

Browse files
authored
Fixed 217 Log-explorer-query-crashed-when-type-something (#398)
1 parent b5b18e8 commit 72e21f4

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

backend/src/main/java/com/park/utmstack/service/elasticsearch/SearchUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private static void buildIsInFields(BoolQuery.Builder bool, FilterType filter) {
229229
final String ctx = CLASSNAME + ".buildIsInFields";
230230
try {
231231
filter.validate();
232-
String value = CustomStringEscapeUtil.opensearchQueryStringEscape(String.valueOf(filter.getValue()));
232+
String value = CustomStringEscapeUtil.openSearchQueryStringEscap(String.valueOf(filter.getValue()));
233233
bool.filter(f -> f.queryString(q -> q
234234
.defaultField("*")
235235
.query("*" + value + "*")));
@@ -242,7 +242,7 @@ private static void buildIsNotInFields(BoolQuery.Builder bool, FilterType filter
242242
final String ctx = CLASSNAME + ".buildIsNotInFields";
243243
try {
244244
filter.validate();
245-
String value = CustomStringEscapeUtil.opensearchQueryStringEscape(String.valueOf(filter.getValue()));
245+
String value = CustomStringEscapeUtil.openSearchQueryStringEscap(String.valueOf(filter.getValue()));
246246
bool.filter(f -> f.bool(b -> b.mustNot(n -> n.queryString(q -> q
247247
.defaultField("*")
248248
.query("*" + value + "*")))));

backend/src/main/java/com/park/utmstack/util/CustomStringEscapeUtil.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
import java.util.Map;
88

99
public class CustomStringEscapeUtil {
10-
public static String opensearchQueryStringEscape(String str) {
11-
final String[] chars = new String[] {"+", "-", "=", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", "\\", "/"};
12-
final Map<CharSequence, CharSequence> escapeCustomMap = new HashMap<>();
13-
escapeCustomMap.put("<", "");
14-
escapeCustomMap.put(">", "");
15-
for (String s : chars)
16-
escapeCustomMap.put(s, "\\\\" + s);
17-
return new AggregateTranslator(new LookupTranslator(escapeCustomMap)).translate(str);
10+
11+
private static final String[] CHARS = new String[] {"+", "-", "=", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", "\\", "/"};
12+
13+
private static final Map<CharSequence, CharSequence> ESCAPE_CUSTOM_MAP = new HashMap<>();
14+
15+
static {
16+
ESCAPE_CUSTOM_MAP.put("<", "");
17+
ESCAPE_CUSTOM_MAP.put(">", "");
18+
for (String s : CHARS)
19+
ESCAPE_CUSTOM_MAP.put(s, "\\" + s);
20+
}
21+
22+
public static String openSearchQueryStringEscap(String str) {
23+
return new AggregateTranslator(new LookupTranslator(ESCAPE_CUSTOM_MAP)).translate(str);
1824
}
1925
}

0 commit comments

Comments
 (0)