Docs Menu

autocomplete

On this page

  • Definition
  • Syntax
  • Options
  • Examples
  • Basic Examples
  • Fuzzy Example
  • Token Order Example
  • Highlighting Example
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 the autocomplete operator must be indexed with the autocomplete data type in the collection's index definition.

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}
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 autocomplete operator does not support multi in the field path.

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

score assigned to matching search term results. Use one of the following options to modify the score:

boost
Multiply the result score by the given number.
constant
Replace the result score with the given number.
Note

autocomplete offers less fidelity in score in exchange for faster query execution.

no
tokenOrder
string

Order in which to search for tokens. Value can be one of the following:

any
Indicates tokens in the query can appear in any order in the documents. Results contain documents where the tokens appear sequentially and non-sequentially. However, results where the tokens appear sequentially score higher than other, non-sequential values.
sequential
Indicates tokens in the query must appear adjacent to each other or in the order specified in the query in the documents. Results contain only documents where the tokens appear sequentially.
no
any

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.

Tip

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.

The following tabs contain sample index definitions for the edgeGram, rightEdgeGram, and nGram tokenization strategies.

The following query searches for movies with the characters off in the title field. The query includes a:

  • $limit stage to limit the output to 10 results.
  • $project stage to exclude all fields except title.
1db.movies.aggregate([
2 {
3 $search: {
4 "autocomplete": {
5 "path": "title",
6 "query": "off"
7 }
8 }
9 },
10 {
11 $limit: 10
12 },
13 {
14 $project: {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])

Atlas Search returns different results depending on the tokenization strategy configured in the autocompletion field of the index definition:

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:

  • $limit stage to limit the output to 10 results.
  • $project stage to exclude all fields except title.
1db.movies.aggregate([
2 {
3 $search: {
4 "autocomplete": {
5 "path": "title",
6 "query": "pre",
7 "fuzzy": {
8 "maxEdits": 1,
9 "prefixLength": 1,
10 "maxExpansions": 256
11 }
12 }
13 }
14 },
15 {
16 $limit: 10
17 },
18 {
19 $project: {
20 "_id": 0,
21 "title": 1
22 }
23 }
24])

Atlas Search returns different results depending on the tokenization strategy configured in the autocompletion field of the index definition:

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:

  • $limit stage to limit the output to 4 results.
  • $project stage to exclude all fields except title.

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.
Important

To highlight the autocomplete indexed version of a path, the autocomplete operator must be the only operator that uses that path in the query.

db.movies.aggregate([
{
$search: {
"autocomplete": {
"path": "title",
"query": ["ger"]
},
"highlight": {
"path": "title"
}
}
},
{
$limit: 5
},
{
$project: {
"score": { $meta: "searchScore" },
"title": 1,
"_id": 0,
"highlights": { "$meta": "searchHighlights" }
}
}
])

The query returns the following results.

{
"title": "Gertie the Dinosaur",
"score": 6.0822906494140625,
"highlights": [
{
"score": 0.9227690100669861,
"path": "title",
"texts": [ { "value": "Gertie the Dinosaur", "type": "hit" } ]
}
]
},
{
"title": "Germany Year Zero",
"score": 6.0822906494140625,
"highlights": [
{
"score": 0.9180012345314026,
"path": "title",
"texts": [ { "value": "Germany Year Zero", "type": "hit" } ]
}
]
},
{
"title": "Germany in Autumn",
"score": 6.0822906494140625,
"highlights": [
{
"score": 0.9180012345314026,
"path": "title",
"texts": [ { "value": "Germany in Autumn", "type": "hit" } ]
}
]
},
{
"title": "Germany Pale Mother",
"score": 6.0822906494140625,
"highlights": [
{
"score": 0.9227690100669861,
"path": "title",
"texts": [ { "value": "Germany Pale Mother", "type": "hit" } ]
}
]
},
{
"title": "Gerhard Richter - Painting",
"score": 6.0822906494140625,
"highlights": [
{
"score": 0.9386774897575378,
"path": "title",
"texts": [ { "value": "Gerhard Richter - Painting", "type": "hit" } ]
}
]
}

Atlas Search returns these results because the title field in the movies collection contains the characters ger at the beginning of a word. Atlas Search matches a highlight hit more coarsely to your query terms when a highlighted path is referenced only in the autocomplete operators of the highlighted query.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.