Skip to content

Commit

Permalink
analytics: fix regexp generation on nested fields
Browse files Browse the repository at this point in the history
nrt added

Change-Id: I1bbcf3561ad3f312e24bae911cd1344e8a5d5afa
  • Loading branch information
ylamgarchal committed Nov 7, 2024
1 parent ab91744 commit 0b74159
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
9 changes: 7 additions & 2 deletions dci/analytics/query_es_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("("))
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions tests/analytics/test_query_es_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}},
]
}
}

0 comments on commit 0b74159

Please sign in to comment.