diff --git a/common/metadata_utils.py b/common/metadata_utils.py index aab00df8a..8403dd9e0 100644 --- a/common/metadata_utils.py +++ b/common/metadata_utils.py @@ -24,7 +24,10 @@ def convert_conditions(metadata_condition): metadata_condition = {} op_mapping = { "is": "=", - "not is": "≠" + "not is": "≠", + ">=": "≥", + "<=": "≤", + "!=": "≠" } return [ { @@ -44,17 +47,59 @@ def meta_filter(metas: dict, filters: list[dict], logic: str = "and"): for input, docids in v2docs.items(): if operator in ["=", "≠", ">", "<", "≥", "≤"]: - try: - if isinstance(input, list): - input = input[0] - input = ast.literal_eval(input) - value = ast.literal_eval(value) - except Exception: - pass - if isinstance(input, str): - input = input.lower() - if isinstance(value, str): - value = value.lower() + # Check if input is in YYYY-MM-DD date format + input_str = str(input).strip() + value_str = str(value).strip() + + # Strict date format detection: YYYY-MM-DD (must be 10 chars with correct format) + is_input_date = ( + len(input_str) == 10 and + input_str[4] == '-' and + input_str[7] == '-' and + input_str[:4].isdigit() and + input_str[5:7].isdigit() and + input_str[8:10].isdigit() + ) + + is_value_date = ( + len(value_str) == 10 and + value_str[4] == '-' and + value_str[7] == '-' and + value_str[:4].isdigit() and + value_str[5:7].isdigit() and + value_str[8:10].isdigit() + ) + + if is_value_date: + # Query value is in date format + if is_input_date: + # Data is also in date format: perform date comparison + input = input_str + value = value_str + else: + # Data is not in date format: skip this record (no match) + continue + else: + # Query value is not in date format: use original logic + try: + if isinstance(input, list): + input = input[0] + input = ast.literal_eval(input) + value = ast.literal_eval(value) + except Exception: + pass + + # Convert strings to lowercase + if isinstance(input, str): + input = input.lower() + if isinstance(value, str): + value = value.lower() + else: + # Non-comparison operators: maintain original logic + if isinstance(input, str): + input = input.lower() + if isinstance(value, str): + value = value.lower() matched = False try: