-
Type:
New Feature
-
Resolution: Fixed
-
Priority:
Minor - P4
-
Affects Version/s: None
-
Component/s: Laravel
-
None
Autocompletion is a common use-case for the search engine. The following method will be added to MongoDB\Laravel\Query\Builder:
class Builder {
/**
* @param bool|array{maxEdits?:int, prefixLength?:int, maxExpansions?:int} $fuzzy
* @return string[]
*/
function autocomplete(string $fieldPath, string $query, int $limit = 20, bool|array $fuzzy = false, bool $sequential = false, string $index = 'default'): array;
}
This method uses the autocomplete operator and returns a list of suggested strings.
$sequential is a boolean that translates tokenOrder = any|sequential.
This method can be used on any DocumentModel class
Movie::autocomplete('title', 'fam')
// return ['The Family That Preys', 'My Family and Other Animals', ...]
Runs the following aggregation:
[
{
$search: {
autocomplete: {
query:'fam',
path:'title',
}
}
},
{
$project: {
title: 1
}
}
]
color: Color value is invalid
If there is no search index on the given collection, no results are returned.
We will not add a helper to create an “autocompletion” index. Relying on the documentation to create indexes with more use-cases.
{
"mappings": {
"dynamic": false,
"fields": {
"title": {
"foldDiacritics": true,
"maxGrams": 15,
"minGrams": 2,
"tokenization": "edgeGram",
"type": "autocomplete"
}
}
}
}