autocomplete
On this page
Definition
autocomplete
The autocomplete operator performs a search for a word or phrase that contains a sequence of characters from an incomplete input string. You can use the
autocomplete
operator with search-as-you-type applications to predict words with increasing accuracy as characters are entered in your application's search field.autocomplete
returns results that contain predicted words based on the tokenization strategy specified in the index definition for autocompletion. The fields that you intend to query with theautocomplete
operator must be indexed with the autocomplete data type in the collection's index definition.NoteAtlas Search might return inaccurate results for queries with more than three words in a single string.
Syntax
autocomplete
has the following syntax:
1 { 2 $search: { 3 "index": "<index name>", // optional, defaults to "default" 4 "autocomplete": { 5 "query": "<search-string>", 6 "path": "<field-to-search>", 7 "tokenOrder": "any|sequential", 8 "fuzzy": <options>, 9 "score": <options> 10 } 11 } 12 }
Options
Field | Type | Description | Necessity | Default | ||||||
---|---|---|---|---|---|---|---|---|---|---|
query | string or array of strings | String or strings to search for. If there are multiple
terms in a string, Atlas Search also looks for a match for each term
in the string separately. | yes | |||||||
path | string | Indexed autocomplete type of field to search. Tip See also: Note The | yes | |||||||
fuzzy | object | Enable fuzzy search. Find strings which are similar to the
search term or terms. | no | |||||||
fuzzy .maxEdits | integer | Maximum number of single-character edits required to match the
specified search term. Value can be 1 or 2 . | no | 2 | ||||||
fuzzy .prefixLength | integer | Number of characters at the beginning of each term in the result
that must exactly match. | no | 0 | ||||||
fuzzy .maxExpansions | integer | Maximum number of variations to generate and search for. This
limit applies on a per-token basis. | no | 50 | ||||||
score | object | no | ||||||||
tokenOrder | string | Order in which to search for tokens. Value can be one of the following:
| no | any |
Limitations
The autocomplete
operator query results that are exact matches
receive a lower score than results that aren't exact matches. Atlas Search
can't determine if a query string is an exact match for an indexed text
if you specify just the autocomplete-indexed token substrings. To score
exact matches higher, try the following workaround:
The following workaround doesn't guarantee higher scores for exact matches in all cases.
Index the field as both autocomplete and string types.
Atlas Search
autocomplete
boosts exact matches when anautocomplete
field is also indexed as astring
, thereby increasing the score of exact matches.- Query using the compound operator.
For a demonstration of this workaround, see Compound Example.
Examples
The following examples use the movies
collection in the
sample_mflix
database. If you loaded the sample dataset on your cluster, you can create the
static index for autocompletion
and run the example queries on your cluster.
If you've already loaded the sample dataset, follow the Get Started with Atlas Search tutorial to create an index definition and run Atlas Search queries.
Index Definition
The following tabs contain sample index definitions for the
edgeGram
, rightEdgeGram
, and nGram
tokenization strategies.
➤ Use the Select your language drop-down menu to set the language of the example on this page.
Basic Examples
The following query searches for movies with the characters off
in
the title
field. The query includes a:
Atlas Search returns different results depending on the tokenization strategy configured in the autocompletion field of the index definition.
Fuzzy Example
The following query searches for movies with the characters pre
in
the title
field. The query uses:
Field | Description |
---|---|
maxEdits | Indicates that only one character variation is allowed in the
query string pre to match the query to a word in the
documents. |
prefixLength | Indicates that the first character in the query string pre
can't change when matching the query to a word in the documents. |
maxExpansions | Indicates that up to two hundred and fifty six similar terms for
pre can be considered when matching the query string to a
word in the documents. |
The query also includes a:
Atlas Search returns different results depending on the tokenization strategy configured in the autocompletion field of the index definition.
Atlas Search returns different results depending on the tokenization strategy configured in the autocompletion field of the index definition.
Token Order Example
The following queries search for movies with the characters men
with
in the title
field. The queries also use the tokenOrder
field,
which specifies whether the query searches for tokens in any
order
or in sequential
order.
Each query includes a:
Highlighting Example
The following query searches for the characters ger
in the
title
field of the movies
collection, with the
highlight
option enabled for the title
field.
The query includes a:
$limit
stage to limit the output to 5 results.$project
stage to:- return the document's score.
- exclude all fields except
title
. - add a new field called
highlights
, which contains highlighting information.
To highlight the autocomplete indexed version of a path, the autocomplete operator must be the only operator that uses that path in the query.
Atlas Search returns different results depending on the tokenization strategy configured in the autocompletion field of the index definition.
Compound Example
The following index definition indexes the title
field as both
autocomplete and string
types.
The following query searches for the characters ball
in the
title
field of the movies
collection using the
compound operator. The query specifies a preference for
documents that contain ballroom
in the title
field.
The query includes a:
$limit
stage to limit the output to 10 results.$project
stage to:- return the document's score.
- exclude all fields except
title
.