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

Time:09-23

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?

CodePudding user response:

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

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

CodePudding user response:

I've come up the solution. However, the one suggested by @Nice-Guy is better and cleaner.

Here is mine:

{
  index: 'description_index',
  regex: {
    query: '.*chicken alfredo.*',
    path: 'description'
  }
}

This query ideally requires lucene.keyword analyzer instead of lucene.standard and also the query is case sensitive.

  • Related