Is it possible to use exact match in Atlas Search?

Hello there,

It’s me again. I am running the following query in Mongo:

db.product.aggregate([
  {
    "$search": {
      "autocomplete": {
        "path": "description",
        "query": "product"
      }
    }
  },
  {
    "$match": {
      "tenantId": "bbb60d4e-212f-445e-97a7-ddad13395931",
      "isArchive": false,
      "isActive": true
    }
  },
  {
    "$sort": {
      "description": 1
    }
  },
  {
    "$skip": 0
  },
  {
    "$limit": 10
  }
])

So, the query is working fine, but when I query in autocomplete a word with lots of matches, it takes more than a minute to be completed.

So I was wondering if it is possible to use the $match of tenantId inside the search. To contextualize better: I am working on a system that you can have your online store, so when you search for a product, it must appear only products from that store (tenantId).

Searching, I read something about using compound inside the $search, but I couldn’t discover how to make an exact match in tenantId. I saw the $equal option, but it is only possible to use with ObjectId or boolean.

I know that in elasticsearch you have the term search, and it is possible to make an exact match.

In a hypothetical and a contextualization way, what I need is:

db.product.aggregate([
  {
    "$search": {
      "$match":  "tenantId": "bbb60d4e-212f-445e-97a7-ddad13395931",
      "autocomplete": {
        "path": "description",
        "query": "product"
      }
    }
  },
  {
    "$match": {
      "isArchive": false,
      "isActive": true
    }
  },
  {
    "$sort": {
      "description": 1
    }
  },
  {
    "$skip": 0
  },
  {
    "$limit": 10
  }
])

What I tried with compound that gives me an error:

db.product.aggregate([
  {
    "$search": {
      "index": "description-search-index",
      "compound": {
        "must": [
          {
            "equals": {
              "path": "description",
              "value": "bbb60d4e-212f-445e-97a7-ddad13395931"
            }
          },
          {
            "autocomplete": {
              "path": "description",
              "query": "produ"
            }
          }
        ]
      }
    }
  },
  {
    "$project": {
      "description": 1
    }
  }
])

If I have to create a specification in the search index for the tenantId field, it would be very helpful to understand the best way for exact match performance.

You could try using filter within Compound - see example here.

Curious what error you saw for the above. Will try to reproduce myself but let me know if this helps!

1 Like