Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

Multi Analyzer

On this page

  • Limitations
  • Example

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.

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

Atlas Search does not support nesting multiple layers of multi objects.

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.

  1. Click Refine Your Index to configure your index.

  2. In the Field Mappings section, click Add Field to open the Add Field Mapping window.

  3. Select title from the Field Name dropdown.

  4. Click the Data Type dropdown and select String if it isn't already selected.

  5. Expand String Properties and make the following changes:

    Index Analyzer
    Select lucene.standard from the dropdown if it isn't already selected.
    Search Analyzer
    Select lucene.standard from the dropdown if it isn't already selected.
    Index Options
    Use the default offsets.
    Store
    Use the default true.
    Ignore Above
    Keep the default setting.
    Norms
    Use the default include.
  6. Click Add Multi Field to configure another analyzer on the title field.

  7. Enter keywordAnalyzer in the Multi Field Name field.

  8. Make the following changes to the Multi Field Properties :

    Index Analyzer
    Select lucene.keyword from the dropdown if it isn't already selected.
    Search Analyzer
    Select lucene.keyword from the dropdown if it isn't already selected.
    Index Options
    Use the default offsets.
    Store
    Use the default true.
    Ignore Above
    Keep the default setting.
    Norms
    Use the default include.
  9. Click Add.

  10. Click Save Changes.

  11. Click Create Search Index.

  1. Replace the default index definition with the following index definition.

    1{
    2 "mappings": {
    3 "dynamic": false,
    4 "fields": {
    5 "title": {
    6 "type": "string",
    7 "analyzer": "lucene.standard",
    8 "multi": {
    9 "keywordAnalyzer": {
    10 "type": "string",
    11 "analyzer": "lucene.keyword"
    12 }
    13 }
    14 }
    15 }
    16 }
    17}
  2. Click Next.

  3. Click Create Search Index.

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

Back

Language

On this page