Multi Analyzer
On this page
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.
Limitations
The multi
path option is available only to fields of
type string.
Atlas Search does not support nesting multiple layers of multi
objects.
Example
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.
Click Refine Your Index to configure your index.
In the Field Mappings section, click Add Field to open the Add Field Mapping window.
Select
title
from the Field Name dropdown.Click the Data Type dropdown and select String if it isn't already selected.
Expand String Properties and make the following changes:
Index AnalyzerSelectlucene.standard
from the dropdown if it isn't already selected.Search AnalyzerSelectlucene.standard
from the dropdown if it isn't already selected.Index OptionsUse the defaultoffsets
.StoreUse the defaulttrue
.Ignore AboveKeep the default setting.NormsUse the defaultinclude
.Click Add Multi Field to configure another analyzer on the
title
field.Enter
keywordAnalyzer
in the Multi Field Name field.Make the following changes to the Multi Field Properties :
Index AnalyzerSelectlucene.keyword
from the dropdown if it isn't already selected.Search AnalyzerSelectlucene.keyword
from the dropdown if it isn't already selected.Index OptionsUse the defaultoffsets
.StoreUse the defaulttrue
.Ignore AboveKeep the default setting.NormsUse the defaultinclude
.Click Add.
Click Save Changes.
Click Create Search Index.
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 } Click Next.
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
.
1 db.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 |