$search mustNot with synonyms returning error

{

                        '$search': {

                            "index": "default",

                            "compound": {

                              "must": [{

                                "text" : {

                                  "query": "NY",

                                  "path" : "state",

                                  "synonyms": synonyms_state"

                                }

                              }],

                              "mustNot": [{

                                "text" : {

                                  "query": "New York City",

                                  "path" : "city",

                                  "synonyms": "synonyms_city"

                                }

                              }]

                          }

                        }

                    },

I am trying to build out a compound search using the must and mustNot keywords. I want to be able to use synonyms in both the must and mustNot but if I include synonyms in the mustNot I am hit with the following error: MongoServerError: Remote error from mongot :: caused by :: Cannot call docFreq() when needsStats=false

Any suggestions on how to make this work?

Hi Tom,

This is a bug which is scheduled to be fixed in the upcoming Atlas Search release. I understand that your use-case is to find documents where:

  • state = "NY" (or its synonyms)
  • city != "New York City" (or its synonyms)

One workaround that you could implement for this, is by modifying the text in the mustNot in your query here to not use synonyms and instead specify the query as an array of strings that are synonyms for New York City (defined in the synonym collection) . For example, consider this test collection:

MongoDB Enterprise HDFTS-shard-0:PRIMARY> db.syn_test_2222.find()
{ "_id" : 2, "item" : "Cap" }
{ "_id" : 1, "item" : "Naan" }
{ "_id" : 4, "item" : "Pasta" }
{ "_id" : 3, "item" : "Pita" }
{ "_id" : 7, "item" : "Hat" }
{ "_id" : 5, "item" : "Spaghetti" }
{ "_id" : 6, "item" : "Gnocchi" }

with this synonym mapping defined (in a separate collection called food_synonyms_2222 ) :

{ "_id" : 2, "mappingType" : "equivalent", "synonyms" : [ "pasta", "spaghetti", "gnocchi", "ravioli", "orzo" ] }

If I do a mustNot clause search that uses the synonym search, to find documents that don’t contain spaghetti (or its synonyms) then I get the error:

MongoDB Enterprise HDFTS-shard-0:PRIMARY> db.syn_test_2222.aggregate([
...   {
...     $search: {
...       "compound": {
...         "mustNot": [{
...           "text": {
...                 "path": "item",
...                 "query": "spaghetti",
...                 "synonyms": "food_synonyms_2222"
...               }
...         }]
...       }
...     }
...   }
... ])
uncaught exception: Error: command failed: {
	"operationTime" : Timestamp(1644443198, 1),
	"ok" : 0,
	"errmsg" : "Remote error from mongot :: caused by :: Cannot call docFreq() when needsStats=false",

...

However, I can simply modify my mustNot clause so that it:

  • does not use synonyms
    and
  • specifies the query as an array of strings that are synonyms for "spaghetti"
MongoDB Enterprise HDFTS-shard-0:PRIMARY> db.syn_test_2222.aggregate([
...   {
...     $search: {
...       "compound": {
...         "mustNot": [{
...           "text": {
...                 "path": "item",
...                 "query": [ "pasta", "spaghetti", "gnocchi", "ravioli", "orzo" ],
...               }
...         }]
...       }
...     }
...   }
... ])
{ "_id" : 1, "item" : "Naan" }
{ "_id" : 2, "item" : "Cap" }
{ "_id" : 3, "item" : "Pita" }
{ "_id" : 7, "item" : "Hat" }

This yields the same results as the mustNot + synonym search query would’ve. I understand that this workaround might be less than ideal, but you could use it temporarily until the bug-fix mentioned above comes out in the next release.

Regards,
Harshad

Thank you for the response, hopefully the release is soon.