2

I am trying to search for multiple words in text inclusively(AND operation) without losing word stemming.

For example:

db.supplies.runCommand("text", {search:"printers inks"}) should return results with (printer and ink) or (printers ink) or (printers ink) or (printers inks) , instead of all results with either printer or ink.

This post covers the search for multiple words as an AND operation, but the solution doesn't search for stemmed words ->MongoDB Text Search AND multiple search words.

The only way I could think of is creating a permutation of all the words and then running the search for the number of permutations(which could be large)

This may not be an effective way to search on a large collection.

Is there a better and smarter way to do it ?

Joanita Dsouza
  • 103
  • 1
  • 1
  • 10

1 Answers1

1

So is there a reason you have to use a text search? If it were me i would use a regular expression.

https://docs.mongodb.com/manual/reference/operator/query/regex/

Off the top of my head something like this.

db.collection.find({products:/printers inks|printers|inks/})

Now i suppose you can do the same thing with a text search too.

db.collection.find({$text:{$search : "\"printers inks\" printers inks"}})

note the escaped quotes.

Scott
  • 174
  • 1
  • 13
  • The OP wants all terms to exist in each matched document. Both of your answers are too permissive, they match documents with only one of the terms. – David Lotts Mar 08 '18 at 17:21