From 0b74159526c9d2a7adbc8624e90e534ba12ce275 Mon Sep 17 00:00:00 2001 From: ylamgarchal Date: Thu, 7 Nov 2024 12:04:45 +0100 Subject: [PATCH] analytics: fix regexp generation on nested fields nrt added Change-Id: I1bbcf3561ad3f312e24bae911cd1344e8a5d5afa --- dci/analytics/query_es_dsl.py | 9 ++++-- tests/analytics/test_query_es_dsl.py | 48 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/dci/analytics/query_es_dsl.py b/dci/analytics/query_es_dsl.py index 92e8f4092..547a0c49f 100644 --- a/dci/analytics/query_es_dsl.py +++ b/dci/analytics/query_es_dsl.py @@ -17,7 +17,9 @@ import pyparsing as pp _field = pp.Word(pp.alphanums + "_" + ".") -_value = pp.Word(pp.alphanums + "_" + "-" + "%" + "." + ":" + "\\" + "*" + "?") +_value = pp.Word( + pp.alphanums + "_" + "-" + "%" + "." + ":" + "\\" + "*" + "?" + "+" + "{" + "}" +) _word = pp.Word(pp.alphanums + "_" + "-" + "." + " " + ":") _comma = pp.Suppress(pp.Literal(",")) _lp = pp.Suppress(pp.Literal("(")) @@ -91,7 +93,7 @@ def _generate_from_operators(parsed_query, handle_nested=False): handle_nested, operator, operand_1, operand_2 ) elif operator == "=~": - return { + _regexp = { "regexp": { operand_1: { "value": operand_2, @@ -100,6 +102,9 @@ def _generate_from_operators(parsed_query, handle_nested=False): } } } + if handle_nested and "." in operand_1: + return {"nested": {"path": operand_1.split(".")[0], "query": _regexp}} + return _regexp elif operator == "not_in": if handle_nested and "." in operand_1: return { diff --git a/tests/analytics/test_query_es_dsl.py b/tests/analytics/test_query_es_dsl.py index 8da95478a..d5d210ae2 100644 --- a/tests/analytics/test_query_es_dsl.py +++ b/tests/analytics/test_query_es_dsl.py @@ -383,3 +383,51 @@ def test_query_build_comparison_operator(): ] } } + + +def test_nrt_query_build_nested_regexp(): + ret = qed.build( + "((components.type=ocp) and (name=~.*upgrade.*) and (team.name=~Intel.+) and (topic.name=OCP-4.16) and (tags in [daily]))" + ) + assert ret == { + "bool": { + "filter": [ + { + "nested": { + "path": "components", + "query": {"term": {"components.type": "ocp"}}, + } + }, + { + "regexp": { + "name": { + "value": ".*upgrade.*", + "flags": "ALL", + "case_insensitive": True, + } + } + }, + { + "nested": { + "path": "team", + "query": { + "regexp": { + "team.name": { + "value": "Intel.+", + "flags": "ALL", + "case_insensitive": True, + } + } + }, + } + }, + { + "nested": { + "path": "topic", + "query": {"term": {"topic.name": "OCP-4.16"}}, + } + }, + {"terms": {"tags": ["daily"]}}, + ] + } + }