Full text search: multiple phrases and words

Hi there.

I’m trying to run a $text $search combining individual words and phrases, in an OR statement,
such as “first phrase” OR “second phrase” OR word1 OR word2.
The following syntax won’t work: {$text: { $search: ““first phrase” “second phrase” word1 word2” } }

According to the docs it’s not supposed to work because the $search operator given a string with a phrase and individual words searches only the phrase and ignores the rest, as explained here: https://docs.mongodb.com/manual/reference/operator/query/text/#phrases

Any idea how to achieve a “first phrase” OR “second phrase” OR word1 with mongodb?



EDIT: the below link and notes refer to aggregation so it seems unrelated to my above question. sorry about that.

However, I found this answer by @Doug_Tarr in the community forum from which I understand I may use an array instead of a string: /how-to-full-text-search-multiple-phrases/3832

Unfortunately that did not work for me either.
When a phrase is followed by a single word then the word is ignored (same behaviour as above)
{$text: { $search: [ ““first phrase””, “word1” } } // “word1” is ignored

and when a phrase is followed by another phrase it seems that MongoDB tries to search “first phrase” AND “second phrase”, rather than “phrase 1” OR “phrase 2”
{$text: { $search: [ ““first phrase””, ““second phrase”” } } // “first phrase” AND “second phrase” instead of OR.

Hi @Amit,
can you give us more precisions about the method you want to use ? Your title and first link refer to the $text operator, but your keywords and second link refer to the $search aggregation pipeline.
In case you want to use the latter, try to play around with the compound operator:

		$search: {
			'compound': {
				'should': [
						'phrase': {
							'path': '<field_path>',
							'query': '[<first_phrase>, <second_phrase>]'
						'text': {
							'path': '<field_path>',
							'query': '[<first_word>, <second_word>]'

Strictly speaking, compound operator doesn’t act as an OR logical operator; it classifies the returned documents with respect to their score.

1 Like

Thanks @Mounir_Farhat,
I’m interested in querying a collection by an OR combination of two-word phrases and single words, applied to a single String field (which is text indexed). Since regex will be too slow and lacks stemming I was trying to use a $text operator. It seems that it’s not supported, so I’m hoping there’s a workaround.
I’ll edit the original post because I see now that second link is unrelated like you said.

It seems like a good candidate for a $search aggregation pipeline. But as I understand it, your use case doesn’t allow this solution.
If you want any further help, I’d suggest to post here what your document schema looks like, with 2 or 3 documents representing the kind of data your are trying to retrieve (not your actual data, but a minimalist modified version with the fields we are interested in). You should also post the requests you tried so far, and the expected results.