How to use Multifield on defaultPath of a queryString search query

I have an Atlas Search index where I’ve used multi option to index a field Content with lucene.standard and multi en with english. This is a thing common in Elasticsearch. It seems that it not possible to specify multi on defaultPath part of queryString query, because it gives me the error: “queryString.defaultPath” must be a string

How can I specify a multifield as defaultPath in queryString?

 {
    $search: {
      index: "segments",
      queryString: {
         "query" : "foo AND bar", 
         defaultPath : { "value": "Content", "multi": "en" }
        
        
      }
    }
  }

Also this query is executed perfectly

  {
    $search: {
      index: "segments",
      text: {
         "query" : "Completed mediation", 
         path :"Content"
        
        
      },
      "highlight": {
        "path": "Content"
      }
    }
  }

But I’m not able to have highlight in the multifield because I got “unexpected error”…

[
  {
    $search: {
      index: "segments",
      text: {
         "query" : "Completed mediation", 
         path : { "value": "Content", "multi": "en" }
        
        
      },
      "highlight": {
        "path": { "value": "Content", "multi": "en" }
      }
    }
  }
]

Clearly if I specify highlight only on Content field it does not work because lucene is not capable to match the field.

It seems that the whole experience of multifield is somewhat difficult to use…

Gian Maria.

Hello, @Gian_Maria_Ricci, and welcome to the MongoDB community forums!

As mentioned in the queryString documentation, it’s worth noting that the defaultPath is a required parameter, and the field must be of string data type. As a result, the first and third queries would not work correctly with the parameters specified.

Based on the query you’ve shared, you can utilise multiField indexes, as shown in the following example:

Suppose we have the sample data:

Atlas atlas-cihc7e-shard-0 [primary] test> db.multipleFiledSearch.find()
[
  {
    _id: ObjectId("652e2cba1a3f40941660c3ba"),
    content: 'Completed mediation'
  },
  { _id: ObjectId("652e2da81a3f40941660c3bb"), content: 'foo AND bar' }
]

I have defined the index as:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "content": {
        "analyzer": "lucene.standard",
        "multi": {
          "mySecondaryAnalyzer": {
            "analyzer": "lucene.english",
            "type": "string"
          }
        },
        "type": "string"
      }
    }
  }
}

And the following query:

[
  {
    $search: {
      text: {
        query: "Completed mediation",
        path: {
          value: "content",
          multi: "mySecondaryAnalyzer",
        },
      },
    },
  },
]

will give you the desired output.

For more details, please refer to the Construct the Query Path documentation.

In case if you have further questions or concerns, please feel free to share additional info such as sample documents and the expected results, so community can help you better.

Warm regards,
Aasawari