Skip to content

Commit

Permalink
Adding lucene compliance for filter parsing of a rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-moessner committed Oct 2, 2024
1 parent e149a02 commit c7388c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 10 additions & 0 deletions logprep/filter/lucene_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,18 @@ def __init__(self, tree: luqum.tree, special_fields: dict = None):
for key in self._special_fields_map:
self._special_fields[key] = special_fields.get(key) if special_fields.get(key) else []

if not self._special_fields['regex_fields']:
self.recognize_regex_and_add_special_fields()

self._last_search_field = None

def recognize_regex_and_add_special_fields(self):
for child in self._tree.children:
value = child.children[0].value[1:-1]
if value.startswith('/') and value.endswith('/'):
self._special_fields['regex_fields'].append(child.name)
child.children[0].value = f'"{value[1:-1]}"'

def build_filter(self) -> FilterExpression:
"""Transform luqum tree into FilterExpression
Expand Down
9 changes: 1 addition & 8 deletions tests/unit/filter/test_lucene_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,6 @@ def test_create_filter_error(self, testcase, input_str, message):
def test_creates_lucene_compliance_filter_two_matching_regex_keys_of_two(self):
lucene_filter = LuceneFilter.create(
'regex_key_one: "/.*value.*/" AND regex_key_two: "/.*value.*/"',
special_fields={"regex_fields": ["regex_key_one", "regex_key_two"]},
)

a = And(
RegExFilterExpression(["regex_key_one"], ".*value.*"),
RegExFilterExpression(["regex_key_two"], ".*value.*"),
)

assert lucene_filter == And(
Expand All @@ -470,8 +464,7 @@ def test_creates_lucene_compliance_filter_two_matching_regex_keys_of_two(self):

def test_creates_lucene_compliance_filter_with_one_matching_and_one_missmatching_regex_key_of_two(self):
lucene_filter = LuceneFilter.create(
'regex_key_one: ".*value.*" AND key_two: "value"',
special_fields={"regex_fields": ["regex_key_one", "i_dont_exist"]},
'regex_key_one: "/.*value.*/" AND key_two: "value"',
)

assert lucene_filter == And(
Expand Down

0 comments on commit c7388c5

Please sign in to comment.