Forward slashes not matching in regexp query

I've created a mapping and regexp query to search urls but these are failing when I add a forward slash to the regex query

"mappings":{
"document":{
"properties":{
"url":{
"type":"text",
"analyzer":"keyword"
}
}
}
}

Urls:
www.test.com/section1/page1
www.test.com/section1/page2
www.test.com/section1/

"query": {
"regexp": {
"content": {
"value": "www.test.com/.*"
}
}
}

I would expect all 3 results to be returned for the above urls that are indexed but I get no results returned.

searching without the / returns the correct results
"query": {
"regexp": {
"content": {
"value": "www.test.com.*"
}
}
}

Any help would be appreciated.

Thanks,
Simon

I believe you need to escape the dot and forward slash characters where they represent characters and not classes or delimiters. Your regular expression should be: www\.test\.com\/.* Refer to this page for more information regarding regular expressions in Elasticsearch.

I've tried it with these too but still no luck (double backslashes used as it was complaining about single escaping)

www\\.test\\.com\\/.* 
www\\.test\\.com/.*

Comparing your query to the example queries here, it appears to expect a field name in the position that you have denoted as "content". Does the regular expression match when you replace it with "url"?

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.