1

So the problem statement is - I am using atlas search for searching product data and have product details in multiple languages. For which, I have used multi analyzer and defined standard and corresponding language analyzer on the same field, so as to get the matching products in any of the case. The requirement is, since it's a product search I need to return atleast some suggestions for users to have a look at and get to the product via returned search results. Here is the mapping I have used after having researched for how to apply multiple analyzers on a single field :

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "type": "string",
        "analyzer": "lucene.standard",
        "multi": {
          "german": {                //german is the name that I have given to this analyzer
            "analyzer": "lucene.german",
            "type": "string"
          },
          "french": {                //french is the name that I have given to this analyzer
            "analyzer": "lucene.french",
            "type": "string"
          }
        }
      }
    }
  }
}

Now, what I want is - if I am searching for a word in english or german or french, I must get a result. Which also is fine with this mapping, I am getting results for all the three languages. But the usecase is, I want result in any of these cases:

  • Search string is in English, German or French
  • Search string is fuzzy (has spelling mistakes)
  • Search string has any special characters or whitespaces
  • Search string can be considered as a whole term and results can be returned on the basis of that.

All the above requirements makes me to use all analyzers - standard, keyword, language, whitespace and simple. But this is increasing index size to more than 3KB (which is an upper limit) plus I am not getting result even for the mapping I have currently where I have applied only language and standard analyzer.

Avani Khabiya
  • 1,207
  • 2
  • 15
  • 34
  • 3
    For all those 3 queries that you have working in different analyzers, you can issue one query that would return documents if they match any of the 3. You can do this using the [compound](https://docs.atlas.mongodb.com/reference/atlas-search/compound/) operator, essentially placing your 3 queries under the `should` key as an array. – Oren Oct 09 '20 at 17:49
  • @Oren The compound operator you suggested is good when we have to search for strings only that the result must or must not contain or should contain a specific string. How to use it for fuzzy string and for different language analyzers? – Avani Khabiya Oct 12 '20 at 05:58
  • @Oren I tried what you suggested, using compound operator with multiple analyzers and should but it is slowing down the query too much and not returning any results even after that. – Avani Khabiya Oct 13 '20 at 09:32

1 Answers1

4

The Mongodb Atlas search documentation

Path Construction

helped me to get how I can do that without complexing the query and without use of compound operator. From the documentation:

The path parameter is used by the Atlas Search operators to specify the field or fields to be searched. It may contain:

  • A string
  • An array of strings
  • A multi analyzer specification
  • An array containing a combination of strings and multi analyzer specifications

This is what I modified in my query :

{
    $search: {
               text: {
                        query: event.queryStringParameters.q,
                        path: ['name',{"value": 'name', "multi": "german"},{"value": 'name', "multi": "french"}],
                        fuzzy: {
                                maxEdits: 2
                        }
               }
      }
}
Avani Khabiya
  • 1,207
  • 2
  • 15
  • 34