[Python] Escaping special characters in a Vespa YQL `matches` query

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    By trial and error, I've found that the special characters requiring escapement in a Vespa matches query include more than just the quote " and backslash \ characters noted at https://docs.vespa.ai/en/reference/query-language-reference.html. Others (e.g. *) need double-escapement.

    For now, I'm using the following Python regex substitution:

    def escape_yql(text: str) -> str:
    subtext = re.sub(r'[\\"]', r"\\\g<0>", text)
    return re.sub(r'[*]', r"\\\\\g<0>", subtext)


    It's used in YQL construction for an exact match regex search like this:

    yql = f'select * from sources * where testfield matches "^{escape_yql(text)}$"'


    However, confusingly (to me, at least) single- or double-escapement of round brackets () causes failure, with a message in the form "Could not create query from YQL: query:L1:58 no viable alternative". No error is usually raised if no escapement is applied to these characters, but not if the number of opening- and closing-brackets is unequal: in that case it becomes evident that they are in fact being parsed, with a message like this:

    select * from sources * where name matches \"^TestText))$\" and bcp47_language matches \"^en$\" limit 1


    "yql: [{'code': 4, 'summary': 'Invalid query parameter', 'message': \"Could not create query from YQL: Unmatched closing ')' near index 8\\n^TestText))$\\n ^\"}]"


    So I wonder which of the other special regex characters might require single- or double-escapement, something else, or none?

    For various reasons I'm going to sidestep this problem by creating an additional field in my schema in which a copy of my tokenised testfield has an exact property, but it nevertheless seems important to figure this out . Any suggestions, please?

    Continue reading...

Compartilhe esta Página