For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

How to Index Fields for Autocompletion

You can use the MongoDB Search autocomplete type to index text values in string fields for autocompletion. You can query fields indexed as autocomplete type using the autocomplete operator.

You can also use the autocomplete type to index:

For dynamic mapping considerations, see Dynamic Mappings.

The MongoDB Search autocomplete type takes the following parameters:

Option
Type
Necessity
Description
Default

type

string

required

Human-readable label that identifies this field type. Value must be string.

analyzer

string

optional

Name of the analyzer to use with this autocomplete mapping. You can use any MongoDB Search analyzer except the lucene.kuromoji language analyzer and the following custom analyzer tokenizers and token filters:

lucene.standard

maxGrams

int

optional

Maximum number of characters per indexed sequence. The value limits the character length of indexed tokens. When you search for terms longer than the maxGrams value, MongoDB Search truncates the tokens to the maxGrams length.

For maxGrams best practices, see maxGrams Configuration.

15

minGrams

int

optional

Minimum number of characters per indexed sequence. We recommend 4 for the minimum value. A value that is less than 4 could impact performance because the size of the index can become very large. We recommend the default value of 2 for edgeGram only.

2

tokenization

enum

optional

Tokenization strategy to use when indexing the field for autocompletion. Value can be one of the following:

  • edgeGram - create indexable tokens, referred to as grams, from variable-length character sequences starting at the left side of the words as delimited by the analyzer used with this autocomplete mapping.

  • rightEdgeGram - create indexable tokens, referred to as grams, from variable-length character sequences starting at the right side of the words as delimited by the analyzer used with this autocomplete mapping.

  • nGram - create indexable tokens, referred to as grams, by sliding a variable-length character window over a word. MongoDB Search creates more tokens for nGram than edgeGram or rightEdgeGram. Therefore, nGram takes more space and time to index the field. nGram is better suited for querying languages with long, compound words or languages that don't use spaces.

edgeGram, rightEdgeGram, and nGram are applied at the letter-level. For example, consider the following sentence:

The quick brown fox jumps over the lazy dog.

When tokenized with a minGrams value of 2 and a maxGrams value of 5, MongoDB Search indexes the following sequence of characters based on the tokenization value you choose.

th
the
the{SPACE}
the q
qu
qui
quic
uick
...

og
dog
{SPACE}dog
y dog
zy
azy
lazy
{SPACE}lazy
he
the
{SPACE}the
r the
er
ver
over
{SPACE}over
...

th
the
the{SPACE}
the q
he
he{SPACE}
he q
he qu
e{SPACE}
e q
e qu
e qui
{SPACE}q
{SPACE}qu
{SPACE}qui
{SPACE}quic
qu
qui
quic
quick
...

For performance considerations, see Tokenization Performance.

edgeGram

foldDiacritics

boolean

optional

Flag that indicates whether to perform normalizations such as including or removing diacritics from the indexed text. Value can be one of the following:

  • true - perform normalizations such as ignoring diacritic marks in the index and query text. For example, a search for cafè returns results with the characters cafè and cafe because MongoDB Search returns results with and without diacritics.

  • false - don't perform normalizations such as ignoring diacritic marks in the index and query text. So, MongoDB Search returns only results that match the strings with or without diacritics in the query. For example, a search for cafè returns results only with the characters cafè. A search for cafe returns results only with the characters cafe.

true

similarity.type

string

optional

Name of the similarity algorithm to use with this string mapping when scoring with the autocomplete operator. Value can be one of the following: bm25, boolean, or stableTfl.

To learn more about the available similarity algorithms, see Score Details.

bm25

The maxGrams option specifies the maximum length of substrings generated during indexing. Increasing maxGrams improves matching for longer queries by generating more substrings. Setting it beyond what you need can increase index size and affect indexing performance.

Consider the following best practices when you configure maxGrams:

  • Default to no more than 15. Set maxGrams to no more than 15 when possible to avoid unnecessary index growth.

  • Align with query length. Set maxGrams based on the typical length of user queries, rather than indexing for worst-case scenarios.

  • Avoid over-indexing. If your queries are shorter than your current maxGrams value, you may be indexing more data than necessary.

  • Use an alternative for longer queries. If your queries regularly exceed 15 characters, use a custom analyzer for prefix, contains, and suffix patterns.

Indexing a field for autocomplete with an edgeGram, rightEdgeGram, or nGram tokenization strategy requires more computation and index storage than indexing a string field.

For the specified tokenization strategy, MongoDB Search concatenates sequential tokens before emitting them ("shingling"). MongoDB Search emits tokens between minGrams and maxGrams characters in length:

  • Keeps tokens less than minGrams.

  • Joins tokens greater than minGrams but less than maxGrams to the next tokens to create tokens up to the specified maximum number of characters in length.

The default field types that MongoDB Search uses for dynamic mappings do not include the autocomplete type. Using the autocomplete type in dynamic mappings can increase index size and resource usage, and produce unexpected scoring results. Use autocomplete in static mappings.

However, if you need to include autocomplete in dynamic mappings, you can add it to a custom typeSet definition. To learn more about autocomplete and custom typeSet configurations, see Index Size and Configuration.

If your dataset has many documents or a wide data range, building this index for the autocomplete operator can take some time. To reduce the impact on other indexes and queries while the new index builds, create a separate index with only the autocomplete type.

For index performance considerations, see Index Performance Considerations.

To learn more about the autocomplete operator and see example queries, see autocomplete.

For examples that demonstrate how to run case-insensitive, prefix, starts with, and contains queries using regex expressions, see Use MongoDB Search Instead of Regex Queries.