Search code examples
jsonregexsolrsolrcloud

How insert a single backslash in a string field - solr


In my SOLR collection I have a string field with regular expression, named "regex" for a date.

I need to insert a regular expression for date that contains a single backslash, but I'm unable to insert it using Documents update JSON interface:

if I insert a single backslash in regex field:

{   
    "id":"a0091da5-2e6c-41d8-97a4-a19fc3c43002",
    "attribute_name": "ArrivalTime",
    "allowed_values": "Date and Time",
    "regex": "\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?Z?"
}

in the query response I receive without :

"regex":"d{4}-[01]d-[0-3]dT[0-2]d:[0-5]d:[0-5]d(?:.d+)?Z?"

instead, if I insert 2 backslashes in regex field:

{   
    "id":"a0091da5-2e6c-41d8-97a4-a19fc3c43002",
    "attribute_name": "ArrivalTime",
    "allowed_values": "Date and Time",
    "regex": "\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d(?:\\.\\d+)?Z?"
}

I receive 2 backslashes :

"regex":"\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d(?:.\\d+)?Z?"

how insert only one backslash? Thank you


Solution

  • The response is just as it should be - since it's JSON, the backslash needs to be escaped in the response as well: otherwise the semantic meaning would be the example you gave (d{4}-[01]d...).

    When you parse the response as JSON, the string will be unescaped and the content will be what you expect (unless you escape it further on output again).