Skip to content

Commit

Permalink
fix(open_search): change the way range is added to filters (#400)
Browse files Browse the repository at this point in the history
* fix: change the way range is added to filters

* fix: add a way to manage multiple range filter

* typo

* typo
  • Loading branch information
maximepln authored Feb 15, 2024
1 parent 84d44eb commit c221464
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 17 deletions.
16 changes: 14 additions & 2 deletions slo_generator/backends/open_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,22 @@ def build_query(query, window, date_field):
}

if "filter" in body["query"]["bool"]:
body["query"]["bool"]["filter"]["range"] = range_query
if isinstance(body["query"]["bool"]["filter"], dict):
body["query"]["bool"]["filter"] = [body["query"]["bool"]["filter"]]

indexes = [
i
for i, x in enumerate(body["query"]["bool"]["filter"])
if "range" in x and date_field in x["range"]
]
body["query"]["bool"]["filter"] = [
i
for j, i in enumerate(body["query"]["bool"]["filter"])
if j not in indexes
]
body["query"]["bool"]["filter"].append({"range": range_query})
else:
body["query"]["bool"]["filter"] = {"range": range_query}

return body


Expand Down
84 changes: 69 additions & 15 deletions tests/unit/backends/test_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,29 @@ def test_build_query_with_simple_query_and_simple_filter(self):
"status": "200",
},
},
"filter": {
"term": {
"type": "get",
"filter": [
{
"term": {
"type": "get",
}
},
"range": {
"date": {
"gte": "now-3600s/s",
"lt": "now/s",
},
{
"range": {
"date": {
"gte": "now-3600s/s",
"lt": "now/s",
},
}
},
},
],
},
},
"track_total_hits": True,
}

assert OpenSearchBackend.build_query(query, 3600, "date") == enriched_query

def test_build_query_with_simple_query_and_existing_filter_with_range(self):
def test_build_query_with_simple_query_and_existing_filter_on_range_date(self):
query: dict = {
"must": {
"term": {
Expand All @@ -106,14 +110,64 @@ def test_build_query_with_simple_query_and_existing_filter_with_range(self):
"status": "200",
},
},
"filter": {
"range": {
"date": {
"gte": "now-3600s/s",
"lt": "now/s",
"filter": [
{
"range": {
"date": {
"gte": "now-3600s/s",
"lt": "now/s",
},
},
}
],
},
},
"track_total_hits": True,
}

assert OpenSearchBackend.build_query(query, 3600, "date") == enriched_query

def test_build_query_with_simple_query_and_existing_filter_with_range(self):
query: dict = {
"must": {
"term": {
"status": "200",
},
},
"filter": {
"range": {
"status": {
"lt": "299",
},
},
},
}

enriched_query: dict = {
"query": {
"bool": {
"must": {
"term": {
"status": "200",
},
},
"filter": [
{
"range": {
"status": {
"lt": "299",
}
}
},
{
"range": {
"date": {
"gte": "now-3600s/s",
"lt": "now/s",
},
}
},
],
},
},
"track_total_hits": True,
Expand Down

0 comments on commit c221464

Please sign in to comment.