Search code examples
mongodbmongoosenosqlfull-text-searchmongodb-atlas

whats the difference between basic and filter text search in mongodb atlas?


I have a simple usecase where given a term i want to initiate a search query in atlas searching that keyword in mongo document and i have my search index created with stored source fields attached.

Now I have 2 kind of queries that I am trying to run listed below:

1st:

    {
    text: {
      query: searchKey,
      path: ['title', 'description'], // Search on both title and description
    },
    returnStoredSource: true,
    }

2nd:

{
compound: {
  filter: [
    {
      text: {
        query: searchKey,
        path: 'title', // Search on title
      },
    },
    {
      text: {
        query: searchKey,
        path: 'description', // Search on description
      },
    },
  ],
},
returnStoredSource: true,
}

Both are getting executed but 2nd one is taking more time and I am not able to understand the difference in both,I just want to have a simple keyword match search in both the fields title and description.

can someone help?


Solution

  • Both of your queries are performing text searches on the fields 'title' and 'description' in a MongoDB Atlas Search index. However, the first query uses the text query type directly, while the second query uses the compound query type with two separate text queries.

    The key difference is that the first query searches for the searchKey in either the 'title' or 'description' fields, whereas the second query searches for the searchKey in the 'title' field and separately in the 'description' field.

    Here's a breakdown of the differences and why the second query might take more time:

    1: Your first query uses the text query type to search for the searchKey in both the 'title' and 'description' fields simultaneously. It will return documents where the searchKey appears in either of the fields.

    2: Your second query uses the compound query type with two separate text queries inside. Each text query searches for the searchKey in either the 'title' or 'description' field. The compound query combines the results of these two queries. This approach might be less efficient as it performs two separate searches and then combines the results.

    If your goal is to have a simple keyword match search in both the 'title' and 'description' fields, the first query (direct use of text query) is more appropriate. It searches for the keyword in either field and should be more efficient compared to the second query.