I have this pipeline in my Spring Boot backend:
@Aggregation(pipeline = {
"{ '$search': { 'index': 'default', 'regex': { 'query': '.*?1.*', 'path': [ { 'wildcard': 'config.*' }, 'name' ], 'allowAnalyzedField': true }, 'highlight': { 'path': [ { 'wildcard': 'config.*' }, 'name' ] } } }",
"{ '$match': { 'orgId': ?0 } }"
})
ConfigData findConfigsBySearch(Integer orgId, String search);
The goal is to make the pipeline return all the documents that contains the searched string in either the name
field or any of the fields inside the config
object. I’m using .*
around the search string to still make it match if there is anything before and after it.
This pipeline works great as long as the search string only contains one word, but as soon as I search using more than one word – meaning a search that contains whitespace(s) – it returns no result even though the database have documents that should match.
What have I tried?
I have tried to use an array instead of 'query': '.*?1.*'
, however that just returns all the documents that has any of the words. I only want documents that has all the words, preferably in the same order as they are in, in the string.
I have also tried to replace all whitespaces in the string with .*
but that still gives no results even though the database have documents that should match.
How do I solve this?