Search code examples
mongodbmongodb-atlasmongodb-atlas-search

Mongodb atlas search: which analyzer is the best for searching multiple terms with AND condition


I'm trying to build MongoDB Atlas full text search index. However, I'm struggle a bit with the analyzers.

Here is my index:

{
  "analyzer": "lucene.standard",
  "searchAnalyzer": "lucene.standard",
  "mappings": {
    "dynamic": false,
    "fields": {
      "description": {
        "type": "string"
      }
    }
  }
}

My problem is when I perform the search over the "description" field.

This is the way how I perform the search:

{
  index: 'description_index',
  text: {
    query: 'chicken alfredo',
    path: 'description'
  }
}

This returns documents where in description field there is either "chicken" or "alfredo" or both. But I need it to return document(s) where in description field there are both "chicken" and "alfredo". Note that, the string in description field could contain other words, for example "Roasted Chicken Alfredo with Chips". In this example my desired solution should return "Roasted Chicken Alfredo with Chips" document but not the document with the following description "Chicken Roberto".

Any ideas how can I solve this problem?


Solution

  • Trying using the phrase operator, which would change your query to this:

    {
       index: 'description_index',
       phrase: {
         query: 'chicken alfredo',
         path: 'description'
       }
     }