Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

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.

Important

Atlas Search won't index string fields that exceed 32766 bytes in size.

The following example index definition specifies an index on the title field in the sample_mflix.movies collection using the keyword analyzer. If you loaded the collection on your cluster, you can create the example index using the Atlas UI Visual Editor or the JSON Editor. 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.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.
  6. Click Add.

  7. Click Save Changes.

  8. Click Create Search Index.

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

    {
    "mappings": {
    "fields": {
    "title": {
    "type": "string",
    "analyzer": "lucene.keyword"
    }
    }
    }
    }
  2. Click Next.

  3. Click Create Search Index.

The following query searches for the phrase Class Action in the title field.

db.movies.aggregate([
{
"$search": {
"text": {
"query": "Class Action",
"path": "title"
}
}
},
{
"$project": {
"_id": 0,
"title": 1
}
}
])
[
{
title: 'Class Action'
}
]

Atlas 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, Atlas 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, Atlas 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
Class Action
X
Standard Analyzer Tokens
class, action
Simple Analyzer Tokens
class, action

Back

Whitespace

Next

Language