Docs Menu

Docs HomeLaunch & Manage MongoDBMongoDB Atlas

Multi Analyzer

You can use the multi object in your index definition to specify alternate analyzers with which to also index the field. When you index a field with alternate analyzers in addition to the default analyzer, you can search the collection with either the default or the alternate analyzer. This page demonstrates how to specify alternate analyzers in your index definition using multi. To learn more about searching with alternate analyzers, see Construct a Query Path.

Note

The multi path option is available only to fields of type string.

The following example index definition specifies an index on the title field in the sample_mflix.movies collection using the standard analyzer. The index definition also specifies keyword analyzer as an alternate analyzer for the title field, with the name keywordAnalyzer. The Keyword Analyzer analyzer indexes the entire field as a single term, so it returns results only if the search term and the specified field match exactly.

If you loaded the collection on your cluster, you can create the example index using the Visual Editor or the JSON Editor in the Atlas UI. After you select your preferred configuration method, select the database and collection.

The following query uses the alternate analyzer, named keywordAnalyzer, to search for exact matches on the string The Count of Monte Cristo.

1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 "query": "The Count of Monte Cristo",
6 "path": { "value": "title", "multi": "keywordAnalyzer" }
7 }
8 }
9 },
10 {
11 "$project": {
12 "title": 1,
13 "year": 1,
14 "_id": 0
15 }
16 }
17])
{ "title" : "The Count of Monte Cristo", "year" : 1934 }
{ "title" : "The Count of Monte Cristo", "year" : 1954 }
{ "title" : "The Count of Monte Cristo", "year" : 1998 }

By contrast, the same query using the standard analyzer would find all the movies with the word the or Count or of or Monte or Cristo in the title.

Atlas Search creates the following tokens (searchable terms) for the documents in the results using the analyzers:

Title
Standard Analyzer Tokens
Keyword Analyzer Tokens
The Count of Monte Cristo
the, count, of, monte, cristo
The Count of Monte Cristo
← Language Analyzers