keyword
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset
Available tabs: other tabs: vib, jsonib -->
Keyword Analyzer
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:
For exact matching, instead of the keyword analyzer, index the field as the MongoDB Search [token](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-bson-data-types-token type. Use the [equals](https://www.mongodb.com/docs/search/query/operators-collectors/equals.md#std-label-equals-ref operator to search the field.
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](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix collection using the keyword analyzer. To follow along with this example, [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-sample-data on your cluster and either use [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh or navigate to the Create a Search
Index page in the Atlas UI following the steps in the [Create a MongoDB Search Index](https://www.mongodb.com/docs/search/tutorial.md#std-label-create-fts-index tutorial.
Then, using the movies collection as your data source, follow the example procedure to create an index from [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh or the Atlas UI Visual Editor or JSON editor.
Visual Editor
-
Click Refine Your Index to configure your index.
-
In the Index Configurations section, toggle Dynamic Mapping to off.
-
In the Field Mappings section, click Add Field to open the Add Field Mapping window.
-
Click Customized Configuration.
-
Select
titlefrom 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.keyword from the dropdown. |
|---|---|
| Search Analyzer | Select lucene.keyword from the dropdown. |
| 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.
db.movies.createSearchIndex(
"default",
{
"mappings": {
"fields": {
"title": {
"type": "string",
"analyzer": "lucene.keyword"
}
}
}
}
)
The following query searches for the phrase Class Action in the title field.
-
Click the Query button for your index.
-
Click Edit Query to edit the query.
-
Click on the query bar and select the database and collection.
-
Replace the default query with the following and click Find:
json
[
{
"$search": {
"text": {
"query": "Class Action",
"path": "title"
}
}
}
]
Output:
js
SCORE: 4.346973419189453 _id: "573a1399f29313caabcec6b7"
awards: Object
cast: Array (4)
countries: Array (1)
directors: Array (1)
fullplot: "Jeb Ward is an attorney who specializes in whistle blower, David vs. G…"
genres: Array (2)
imdb: Object
languages: Array (1)
lastupdated: "2015-09-06 00:42:51.167000000"
metacritic: 58
num_mflix_comments: 2
plot: "Jeb Ward is an attorney who specializes in whistle blower, David vs. G…"
poster: "https://m.media-amazon.com/images/M/MV5BNWY5Mjk4ZmItMTAzYS00NWE3LWEzYz…"
rated: "R"
released: 1991-03-15T00:00:00.000+00:00
runtime: 110
title: "Class Action"
tomatoes: Object
type: "movie"
writers: Array (3)
year: 1991
db.movies.aggregate([
{
"$search": {
"text": {
"query": "Class Action",
"path": "title"
}
}
},
{
"$project": {
"_id": 0,
"title": 1
}
}
])
Output:
[
{
title: 'Class Action'
}
]
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. The keyword analyzer returns no results for this query. It only matches documents where the search term matches the entire field contents. If you index the field using the [Standard Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/standard.md#std-label-ref-standard-analyzer or [Simple Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/simple.md#std-label-ref-simple-analyzer, MongoDB Search returns many results. The results include the document with the title Class Action. Those analyzers create tokens similar to the following and match them to the query term:
| Analyzer | Output Tokens | Matches action |
Matches Class Action |
|---|---|---|---|
| Keyword Analyzer Tokens | Class Action |
X | √ |
| Standard Analyzer Tokens | class, action |
√ | √ |
| Simple Analyzer Tokens | class, action |
√ | √ |
