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.
Limitations
The multi
path option is available only to fields of
type string.
Atlas Search does not support nesting multiple layers of multi
objects.
Examples
The following examples demonstrate index definitions on the fields in the
sample_mflix.movies
collection. If you load the collection on your
cluster, you can create the example indexes using the Visual Editor or
the JSON Editor in the Atlas UI. After you select your preferred
configuration method, select the database and collection to define the
index.
Single Field Example
The following example index definition specifies an index on the
title
field using the standard
analyzer. The index definition
also specifies lucene.french analyzer as
an alternate analyzer for the title
field, with the name
frenchAnalyzer
.
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 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
.Click Add Multi Field to configure another analyzer on the
title
field.Enter
frenchAnalyzer
in the Multi Field Name field.Make the following changes to the Multi Field Properties :
Index Analyzer
Select
lucene.french
from the dropdown if it isn't already selected.Search Analyzer
Select
lucene.french
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
.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 "frenchAnalyzer": { 10 "type": "string", 11 "analyzer": "lucene.french" 12 } 13 } 14 } 15 } 16 } 17 } Click Next.
Click Create Search Index.
The following query uses the alternate analyzer, named
frenchAnalyzer
, to search for the string liberte
.
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "text": { 5 "query": "liberte", 6 "path": { "value": "title", "multi": "frenchAnalyzer" } 7 } 8 } 9 }, 10 { 11 "$project": { 12 "title": 1, 13 "year": 1, 14 "_id": 0 15 } 16 } 17 ])
[ { title: 'è Nous la Libertè', year: 1931 } ]
The document in the results is a match because the frenchAnalyzer
reduced both the indexed string è Nous la Libertè
and query term
liberte
to the base word libert
. Atlas Search won't return this
document for a search with the standard
analyzer because it doesn't
remove diacritics. Each analyzer generates the following tokens
(searchable terms) for the index and query:
Analyzer | Index Tokens | Query Tokens |
---|---|---|
Standard Analyzer |
|
|
French Analyzer |
|
|
Multiple Fields Example
The following example index definition specifies an index on the
title
and plot
fields using the standard
analyzer. The index
definition also specifies lucene.french as an alternate analyzer for the title
and the plot
fields.
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 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
.Click Add Multi Field to configure another analyzer on the
title
field.Enter
frenchAnalyzer
in the Multi Field Name field.Make the following changes to the Multi Field Properties :
Index Analyzer
Select
lucene.french
from the dropdown if it isn't already selected.Search Analyzer
Select
lucene.french
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
.Click Add.
Click Add Field Mapping to open the Add Field Mapping window.
Select
plot
from the Field Name dropdown.Repeat steps 4 to 9.
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 "frenchAnalyzer": { 10 "type": "string", 11 "analyzer": "lucene.french" 12 } 13 } 14 }, 15 "plot": { 16 "type": "string", 17 "analyzer": "lucene.standard", 18 "multi": { 19 "frenchAnalyzer": { 20 "type": "string", 21 "analyzer": "lucene.french" 22 } 23 } 24 } 25 } 26 } 27 } Click Next.
Click Create Search Index.
The following query searches for matches for the string revolution
in the
title
and the plot
fields by using the alternate analyzer named
frenchAnalyzer
.
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "text": { 5 "query": "revolution", 6 "path": ["title", "plot", 7 { "value": "title", "multi": "frenchAnalyzer" }, 8 { "value": "plot", "multi": "frenchAnalyzer" } 9 ] 10 } 11 } 12 }, 13 { 14 "$limit": 5 15 }, 16 { 17 "$project": { 18 "title": 1, 19 "plot": 1, 20 "year": 1, 21 "_id": 0 22 } 23 } 24 ])
[ { year: 2012, plot: 'REVOLUTION is a film about changing the world, going for it, taking a stand, and fighting for something. A true-life adventure following Director, Rob Stewart (SHARKWATER) over four years ...', title: 'Revolution' }, { plot: 'New York trapper Tom Dobb becomes an unwilling participant in the American Revolution after his son Ned is drafted into the Army by the villainous Sergeant Major Peasy. Tom attempts to find...', title: 'Revolution', year: 1985 }, { plot: "Together with five Soviet avant-garde artists, hero of the Russian revolution Polina Schneider travels to Siberia to 'civilize' the native Khanty and Nenets tribes, for whom interaction ...", title: 'Angels of Revolution', year: 2014 }, { plot: 'Two mismatched sets of identical twins - one aristocrat, one peasant - mistakenly exchange identities on the eve of the French Revolution.', title: 'Start the Revolution Without Me', year: 1970 }, { plot: "A history of the French Revolution from the decision of the king to convene the Etats-Generaux in 1789 in order to deal with France's debt problem. The first part of the movie tells the ...", title: 'La rèvolution franèaise', year: 1989 } ]
The documents in the results contain the query term in both the
title
and the plot
fields. Each analyzer creates the following
tokens (searchable terms) for the title
field for each document in
the results:
Year | Analyzer | Index Tokens | Query Tokens |
2012 | Standard Analyzer French Analyzer | revolution revolution | revolution revolution |
1985 | Standard Analyzer French Analyzer | revolution revolution | revolution revolution |
2014 | Standard Analyzer French Analyzer | angels , of , revolution angels , of , revolution | revolution revolution |
1970 | Standard Analyzer French Analyzer | start , the , revolution , without , me start , the , revolution , without | revolution revolution |
1989 | Standard Analyzer French Analyzer | la , rèvolution , franèaise revolution , franeais | revolution revolution |
For the fifth document in the results, the frenchAnalyzer
removed
the diacritics in the title
field to match the document to the query
term.