Performance impact of using $match after $search in mongodb atlas

I want to know the complete request life cycle of request when we query mongodb atlas for full text search. I know that when we run some aggregation in atlas it goes to mongot and then to lucene. we can consider below query for reference :

db.example.aggregate([
  {
    "$search": {
      "index": "default",
      "compound": {
        "must": [
          ...some conditions
        ],
        "filter": [
          ...some clauses
        ]
      }
    }
  },
  {
    "$project": {
      ...some fields
    }
  },
  {
    "$skip": 0
  },
  {
    "$limit": 10
  },
  {
    "$sort": {
      "score": 1
    }
  }
])

I also read in docs that for full text search we have to put all our search criteria inside either : $search or $serachMeta.
What if i use $match after $search in atlas like so. :

db.example.aggregate([
  {
    "$search": {
      "index": "default",
      "compound": {
        "must": [
          ...some conditions
        ],
        "filter": [
          ...some clauses
        ]
      }
    }
  },
  {
    "$match": [...some other conditions]
  },
  {
    "$project": {
      ...some fields
    }
  },
  {
    "$skip": 0
  },
  {
    "$limit": 10
  },
  {
    "$sort": {
      "score": 1
    }
  }
])

what changes would come in request liefcycle of query compared to without $match clause and what would be the performance impact if any ?

Hi @pawan_saxena1

Regarding performance, you might want to check out these pages:

About your question:

What if i use $match after $search in atlas like so.

it’s discussed in the above link about Query Performance:

Using a $match aggregation pipeline stage after a $search stage can drastically slow down query results. If possible, design your $search query so that all necessary filtering occurs in the $search stage to remove the need for a $match stage.

Note that as the product is constantly being improved, this may or may not be relevant in the future. Please check out the links above for more details.

Best regards
Kevin

1 Like

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