Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev-add-lucene-regex-compliance'…
Browse files Browse the repository at this point in the history
… into dev-add-lucene-regex-compliance
  • Loading branch information
MoessnerFabian(Group) committed Oct 28, 2024
2 parents d0d9253 + a354dc9 commit aca1681
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions logprep/filter/expression/filter_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,22 @@ def does_match(self, document: dict) -> bool:
return self._lower_bound <= value <= self._upper_bound


class LuceneRegexExpression(KeyValueBasedFilterExpression):
"""Lucene compliant filter expression that matches a value using regex."""

def __init__(self, key: List[str], regex: str):
self._regex = regex
self._matcher = re.compile(self._regex)
super().__init__(key, f"/{self._regex.strip('^$')}/")

def does_match(self, document: dict) -> bool:
value = self._get_value(self.key, document)

if isinstance(value, list):
return any(filter(self._matcher.match, value))
return self._matcher.match(str(value)) is not None


class RegExFilterExpression(KeyValueBasedFilterExpression):
"""Filter expression that matches a value using regex."""

Expand Down
5 changes: 5 additions & 0 deletions logprep/filter/lucene_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,13 @@ def _parse_tree(self, tree: luqum.tree) -> FilterExpression:
if self._last_search_field:
return self._create_field_group_expression(tree)
return self._create_value_expression(tree)
if isinstance(tree, Regex):
return self._create_regex_expression(tree)
raise LuceneFilterError(f'The expression "{str(tree)}" is invalid!')

def _create_regex_expression(self, tree: luqum.tree) -> LuceneRegexExpression:
pass

def _create_field_group_expression(self, tree: luqum.tree) -> FilterExpression:
"""Creates filter expression that is resulting from a field group.
Expand Down

0 comments on commit aca1681

Please sign in to comment.