How to full text search multiple phrases

I’m using Atlas and have read that Lucene supports AND and OR operators.

Can these be used somehow with multiple phrases?

Something like:

    { $text:
      { $search: "\\"my cool phrase\\" OR "\\"my even cooler phrase\\"" }
    }

Hi Corey -

Yes, you can. For OR there are several different ways to do this. The easiest is simply to pass an array instead of a string (phrase1 OR phrase2). Also see the path construction docs.

{
  $search: {
     text: { path: "name", query: ["phrase1", "phrase2"]}
  }
}

You can also use the compound: should: operator, which can handle more sophisticated use cases. Also see the compound docs.

    {
      $search: {
         compound: {
             should: [
                { text: { path: "name", query: "phrase1"} },
                { text: { path: "name", query: "phrase2"} },
             ]
      }
    }

You can use compound: filter: for AND (Phrase1 AND Phrase2):

    {
      $search: {
         compound: {
             filter: [
                { text: { path: "name", query: "phrase1"} },
                { text: { path: "name", query: "phrase2"} },
             ]
      }
    }

Compound clauses can also be nested to arbitrary depths which will let you build more sophisticated boolean logic.

3 Likes

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