Using synonyms stops fulltext search from working

I am having trouble with synonyms

When I apply the following query, I get search results.

    $search: {
      "index": "fulltext",
      "text": {
        "query": "His my foot hurts orthopedic",
        "path": ["locationName", "practicingSpecialty"],
        //"synonyms": "synonyms1"
      }
    }

However, when I apply the following query, I get no search results at all::

    $search: {
      "index": "fulltext",
      "text": {
        "query": "His my foot hurts orthopedic",
        "path": ["locationName", "practicingSpecialty"],
        "synonyms": "synonyms1"
      }
    }

I have one synonym in my collection:

  {
      "mappingType": "equivalent",
      "synonyms": ["knee", "Orthopedic", "Orthopedics", "Orthopedist"]
  }

My index definition looks like the following:

{
  "mappings": {
    "dynamic": true
  },
  "synonyms": [
    {
      "name": "synonyms1",
      "analyzer": "lucene.standard",
      "source": {
        "collection": "searchSynonyms"
      }
    }
  ]
}

When I apply a query the gets an “exact” match in a field then I get search results, but if there is not an exact match I get no results. I want to get search results where I have a fields that looks like this:

The ‘orthopedic’ term in the query above should match on Orthopedics in the practicingSpecialty field. Also, if I used the term ‘knee’, my synonym should apply ‘orthopedics’ and also get a match.

{
    "gender": "Female",
    "practicingSpecialty": "Orthopedics",
    "practiceType": "Medical",
}

Wondering what I might be doing wrong?

Hi @Shon_Schetnan and welcome to MongoDB community forums!!

Thats a very nice catch!!.

I’ve raised this with the team internally but in the mean time you can share your requirements and use case on our MongoDB Feedback Engine.

Meanwhile, as mentioned in the documentation for Atlas Search, the text queries work differently when used with synonym and without synonyms.
The text queries that use synonyms look for a conjunction (AND) of query tokens. text queries that don’t use synonyms search for a disjunction (OR) of query tokens. To run text queries that use synonyms and search for a disjunction (OR) of query tokens also, use the compound operator.

To resolve your issue, you can try the query below and let us know if the expectations are met.

[
  {
    $search: {
      index: "test",
      compound: {
        should: [
          {
            text: {
              query:
                "His my foot hurts orthopedic",
              path: "practicingSpecialty",
              synonyms: "mySynonyms",
            },
          },
          {
            text: {
              query:
                "His my foot hurts orthopedics",
              path: "practicingSpecialty",
            },
          },
        ],
      },
    },
  },
]

In the above query, inside the should operator, the first part of the query which uses synonyms, treats each word of the query with AND whereas in the second case, it finds the exact match word orthopedics and returns the results.

If I put the query as:

[
  {
    $search: {
      index: "test",
      compound: {
        should: [
          {
            text: {
              query: "orthopedic",
              path: "practicingSpecialty",
              synonyms: "mySynonyms",
            },
          },
          {
            text: {
              query:
                "His my foot hurts orthopedic",
              path: "practicingSpecialty",
            },
          },
        ],
      },
    },
  },
]

the results are obtained from the first part of the should operator.

Let us know if the above query helps.

Warm Regards
Aasawari