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:
Fields whose value is an array of strings. To learn more, see How to Index the Elements of an Array.
String fields inside an array of documents indexed as the embeddedDocuments type. For index build time considerations, see Index Build Time.
For dynamic mapping considerations, see Dynamic Mappings.
Define the Index for the autocomplete Type
Configure autocomplete Field Properties
The MongoDB Search autocomplete type takes the following parameters:
Option | Type | Necessity | Description | Default | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| string | required | Human-readable label that identifies this field type. Value must be | |||||||||||||||||||||||
| string | optional | Name of the analyzer to use with this autocomplete mapping. You can use any MongoDB Search analyzer except the |
| ||||||||||||||||||||||
| 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 For |
| ||||||||||||||||||||||
| int | optional | Minimum number of characters per indexed sequence. We recommend |
| ||||||||||||||||||||||
| enum | optional | Tokenization strategy to use when indexing the field for autocompletion. Value can be one of the following:
When tokenized with a For performance considerations, see Tokenization Performance. |
| ||||||||||||||||||||||
| 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:
|
| ||||||||||||||||||||||
| 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: To learn more about the available similarity algorithms, see Score Details. |
|
Try an Example for the autocomplete Type
Considerations
maxGrams Configuration
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
maxGramsto no more than15when possible to avoid unnecessary index growth.Align with query length. Set
maxGramsbased 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
maxGramsvalue, 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, andsuffixpatterns.
Tokenization Performance
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
minGramsbut less thanmaxGramsto the next tokens to create tokens up to the specified maximum number of characters in length.
Dynamic Mappings
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.
Index Build Time
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.
Learn More
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.