The keyword analyzer accepts a string or array of strings as a parameter and indexes them as a single term (token). Only exact matches on the field are returned. It leaves all text in its original letter case.
Tip
If you select Refine Your Index, the Atlas UI displays a section titled View text analysis of your selected index configuration within the Index Configurations section. If you expand this section, the Atlas UI displays the index and search tokens that the keyword analyzer generates for each sample string. You can see the tokens that the keyword analyzer creates for a built-in sample document and query string when you create or edit an index in the Atlas UI Visual Editor.
Important
MongoDB Search won't index string fields where analyzer tokens exceed 32766 bytes in size. If using the keyword analyzer, string fields which exceed 32766 bytes will not be indexed.
Example
The following example index definition specifies an index on the title field in the sample_mflix.movies collection using the keyword analyzer. To follow along with this example, load the sample data on your cluster and either use mongosh or navigate to the Create a Search
Index page in the Atlas UI following the steps in the Create
a MongoDB Search Index tutorial.
Then, using the movies collection as your data source, follow the example procedure to create an index from mongosh or the Atlas UI Visual Editor or JSON editor.
➤ Use the Select your language drop-down menu to set the language of the example on this page.
The following query searches for the phrase Class Action in the title field.
MongoDB Search returned the document because it matched the query term Class Action to the single token Class Action that it creates for the text in the field using the lucene.keyword analyzer. By contrast, MongoDB Search doesn't return any results for the following query:
db.cases.aggregate([ { "$search": { "text": { "query": "action", "path": "title" } } } ])
Many documents in the collection contain the string action, but the keyword analyzer only matches documents in which the search term matches the entire contents of the field exactly. For the preceding query, the keyword analyzer wouldn't return any results. However, if you indexed the field using the Standard Analyzer or Simple Analyzer, MongoDB Search would return multiple documents in the results, including the document with the title field value Class Action, because it would create tokens similar to the following, which it would then match to the query term:
Analyzer | Output Tokens | Matches action | Matches Class Action |
|---|---|---|---|
Keyword Analyzer Tokens |
| X | √ |
Standard Analyzer Tokens |
| √ | √ |
Simple Analyzer Tokens |
| √ | √ |