Navigation

autocomplete

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 "autocomplete": {
4 "query": "<search-string>",
5 "path": "<field-to-search>",
6 "tokenOrder": "any|sequential",
7 "fuzzy": <options>,
8 "score": <options>
9 }
10 }
11}
FieldTypeDescriptionNecessityDefault
querystring or array of stringsString 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
pathstring

Indexed autocomplete type of field to search.

Bulb IconTip
See Also:
Info With Circle IconCreated with Sketch.Note

The autocomplete operator does not support multi in the field path.

yes
fuzzyobjectEnable fuzzy search. Find strings which are similar to the search term or terms.no
fuzzy
.maxEdits
integerMaximum number of single-character edits required to match the specified search term. Value can be 1 or 2.no2
fuzzy
.prefixLength
integerNumber of characters at the beginning of each term in the result that must exactly match.no0
fuzzy
.maxExpansions
integerMaximum number of variations to generate and search for. This limit applies on a per-token basis.no50
scoreobject

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

boostMultiply the result score by the given number.
constantReplace the result score with the given number.
Info With Circle IconCreated with Sketch.Note

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

no
tokenOrderstring

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

anyIndicates 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.
sequentialIndicates 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.
noany

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 queries on your cluster.

Click on your preferred tokenization strategy to view a sample index definition that you can use for the queries in the following examples:

Info With Circle IconCreated with Sketch.Note

To learn more about edgeGram and nGram, see autocomplete.

1{
2 "mappings": {
3 "dynamic": false,
4 "fields": {
5 "title": [
6 {
7 "type": "autocomplete",
8 "tokenization": "edgeGram",
9 "minGrams": 3,
10 "maxGrams": 7,
11 "foldDiacritics": false
12 }
13 ]
14 }
15 }
16}

You can follow the steps in the Tutorial: Create and Query an Atlas Search Index to load the sample dataset, create an index definition, and run Atlas Search queries.

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])

Click on tokenization strategy to view the results:

1{ "title" : "Off the Map" }
2{ "title" : "Off and Running" }
3{ "title" : "Benji: Off the Leash!" }
4{ "title" : "An Officer and a Gentleman" }
5{ "title" : "A Spell to Ward Off the Darkness" }
6{ "title" : "Office Romance" }
7{ "title" : "Office Killer" }
8{ "title" : "Office Space" }
9{ "title" : "Off Beat" }
10{ "title" : "Official Rejection" }

In the above results, the characters off appears at the beginning of a word in all the titles.

The following query searches for movies with the characters pre in the title field. The query uses:

FieldDescription
maxEditsIndicates that only one character variation is allowed in the query string pre to match the query to a word in the documents.
prefixLengthIndicates that the first character in the query string pre can't change when matching the query to a word in the documents.
maxExpansionsIndicates 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 $project: {
17 "_id": 0,
18 "title": 1
19 }
20 }
21])

Click on tokenization strategy to view the results:

1{ "title" : "Prelude to War" }
2{ "title" : "Sitting Pretty" }
3{ "title" : "Gentlemen Prefer Blondes" }
4{ "title" : "The Parent Trap" }
5{ "title" : "Premature Burial" }
6{ "title" : "The President's Analyst" }
7{ "title" : "Pretty Poison" }
8{ "title" : "El castillo de la pureza" }
9{ "title" : "Premiya" }
10{ "title" : "All the President's Men" }

These results show the words that are predicted for the query string with one character modification and with the first character constant at the beginning of the word in all the titles.

The following queries search for movies with the characters men with in any and sequential order in the title field. The query includes a:

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

This query returns the following results for the edgeGram tokenization strategy:

{ "title" : "Men Without Women" }
{ "title" : "Men with Guns" }
{ "title" : "Men with Brooms" }
{ "title" : "Without Men" }

This query returns the following results for the nGram tokenization strategy:

{ "title" : "Men Without Women" }
{ "title" : "Men with Guns" }
{ "title" : "Men with Brooms" }
{ "title" : "Women Without Men" }
Give Feedback