I use the regex operator in my MongoDB Atlas search because the data is indexed using a keyword analyzer, meaning that the whole string of a field is indexed as one single “word”.
Because of this, exact word matches don’t work like they would with the default analyzer. If the title of a document is JavaScript beginner tutorial
then a search for JavaScript
will not match. Instead, I use regex wildcards to find single words within this larger string. Which looks like this:
'(.*)' + 'JavaScript' + '(.*)'
This works. However, I want to give exact word matches an extra boost in the search score. Hence I want to run an additional regex query that looks for either only whitespace before or after the word, or the word being at the beginning and/or end of the string.
For example, when I search for Java
, I want Java beginner tutorial
to rank higher than JavaScript beginner tutorial
. Currently, this is not the case, because the wildcards look for any characters.
The normal Regex operators don’t seem to work with the Atlas regex search. I tried things like word boundaries \bJava\b
or others, but none of them had an effect.