From fa58662e5b0c084382bc701a3963e025425e9651 Mon Sep 17 00:00:00 2001 From: wtt <1136220284@qq.com> Date: Wed, 9 Aug 2023 20:10:56 +0800 Subject: [PATCH] feat:update es query field --- .../docean-plugin-es-antlr4/README.md | 28 +++++++++++++++++-- .../es/antlr4/common/enums/ValueTypeEnum.java | 1 + .../es/antlr4/impl/EsQueryTransfer.java | 4 +++ .../docean/plugin/es/antlr4/EsQueryTest.java | 27 ++++++++++++++++-- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/jcommon/docean-plugin/docean-plugin-es-antlr4/README.md b/jcommon/docean-plugin/docean-plugin-es-antlr4/README.md index 3ae5e4c1e..5185a179b 100644 --- a/jcommon/docean-plugin/docean-plugin-es-antlr4/README.md +++ b/jcommon/docean-plugin/docean-plugin-es-antlr4/README.md @@ -1,6 +1,30 @@ -支持大多数kibana语法,只需要写语法即可生成SearchRequest去查询,具体可当单元测试 +支持大多数kibana语法,只需要写语法即可生成SearchRequest去查询,具体可看单元测试 支持多种语法,使用括号可以支持优先级 需要需要新加语法或者减少语法,可以直接修改g4包下[EsQuery.g4]文件,然后在idea中 右击 Generate Antlr Recongnizer重新生成生成语法类(在query包下) -直接使用[EsQueryUtils.java]使用 \ No newline at end of file +直接使用[EsQueryUtils.java]使用 + +表达式一: 变量 运算符 值 +变量: 非数字开头的数字、字母、下划线组合 +运算符: + +":" :等于 +"!=" :不等于 +">" :大于 +"<" :小于 +"<=" :小于等于 +">=" :大于等于 +"=~" : 正则 +"!~" : 正则取反 +"IN" :包含在 +"NOT_IN" :不包含在 +"CONTAINS" +"NOTCONTAINS" + +a>=2022-07-11 16:00:00.000 AND b<=2022-07-11 17:00:00.000 +包含关系 name IN ["张三","李四","王五"] + +":" "!=" ">" "<" "<=" ">=" "IN" "NOT_IN" "EXIST" "NOT_EXIST" 以及逻辑AND OR NOT + +聚合效果后期支持 \ No newline at end of file diff --git a/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/common/enums/ValueTypeEnum.java b/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/common/enums/ValueTypeEnum.java index 015b5531f..69a25e60f 100644 --- a/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/common/enums/ValueTypeEnum.java +++ b/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/common/enums/ValueTypeEnum.java @@ -21,6 +21,7 @@ public enum ValueTypeEnum { NULL("null", "NULL"), ARRAY("array", "ARRAY"), EQUAL(":", "equal"), + IDENTIFY("identifier", "identifier"), REGEX("regex", "REGEX"); @Getter diff --git a/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/impl/EsQueryTransfer.java b/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/impl/EsQueryTransfer.java index 0873536d4..7165f3c5a 100644 --- a/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/impl/EsQueryTransfer.java +++ b/jcommon/docean-plugin/docean-plugin-es-antlr4/src/main/java/com/xiaomi/youpin/docean/plugin/es/antlr4/impl/EsQueryTransfer.java @@ -509,6 +509,10 @@ public void exitIdentifierValue(EsQueryParser.IdentifierValueContext ctx) { if (ctx.getParent() instanceof EsQueryParser.EqExprContext) { //属于等于下的值 valueProperty.put(ctx, new ValueContext(ValueTypeEnum.EQUAL, ctx.getChild(0).getText())); + } else if (ctx.getParent() instanceof EsQueryParser.NeExprContext) { + valueProperty.put(ctx, new ValueContext(ValueTypeEnum.IDENTIFY, ctx.getChild(0).getText())); + } else if (ctx.getParent() instanceof EsQueryParser.ArrayContext) { + valueProperty.put(ctx, new ValueContext(ValueTypeEnum.STRING, ctx.getChild(0).getText())); } else { String value = ctx.getChild(0).getText(); if (null == value) { diff --git a/jcommon/docean-plugin/docean-plugin-es-antlr4/src/test/java/com/xiaomi/youpin/docean/plugin/es/antlr4/EsQueryTest.java b/jcommon/docean-plugin/docean-plugin-es-antlr4/src/test/java/com/xiaomi/youpin/docean/plugin/es/antlr4/EsQueryTest.java index 2deb60d06..9194dfcc7 100644 --- a/jcommon/docean-plugin/docean-plugin-es-antlr4/src/test/java/com/xiaomi/youpin/docean/plugin/es/antlr4/EsQueryTest.java +++ b/jcommon/docean-plugin/docean-plugin-es-antlr4/src/test/java/com/xiaomi/youpin/docean/plugin/es/antlr4/EsQueryTest.java @@ -41,7 +41,7 @@ public void init() { @After public void end() { - if(null != searchResponse){ + if (null != searchResponse) { SearchHit[] hits = searchResponse.getHits().getHits(); if (hits == null || hits.length == 0) { return; @@ -145,16 +145,37 @@ public void test10() throws IOException { } @Test - public void test11(){ + public void test11() { String str = " (not http) and 8088"; String esQuery = EsQueryUtils.getEsQuery(str); System.out.println(esQuery); } @Test - public void test12(){ + public void test12() { String str = "10.38.201.233"; String esQuery = EsQueryUtils.getEsQuery(str); System.out.println(esQuery); } + + /** + * 某个key的值不等于value + */ + @Test + public void test13(){ +// String str = "level != INFO"; + String str = "level != \"INFO\""; + String esQuery = EsQueryUtils.getEsQuery(str); + System.out.println(esQuery); + } + @Test + public void test14(){ +// String str = "linenumber< 128"; +// String str = "level in [\"ERROR\",\"INFO\"]"; + String str = "level in [ERROR,INFO]"; + String esQuery = EsQueryUtils.getEsQuery(str); + System.out.println(esQuery); + } + + }