I am trying to create autocomplete with similar functionality as Apollo graphql has. Basically it:
- searches as I type
- handles typos
- gives the most weigh to headers then subheaders and at last to content
While in Apollo graphql doc this functionality is provided by algolia I was pretty sure that I can built it with elasticsearch.
I started with Search-as-you-type field type and easily made it work as follow.
Mapping:
PUT /article
{
"mappings": {
"properties": {
"title": {
"type": "search_as_you_type"
}
}
}
}
Some dummy data:
PUT /article/_bulk
{ "index": {"_id": "1"} }
{ "title": "Authentication and authorization", "subtitle": "Putting authenticated user info on the context" }
And searching:
GET /article/_search
{
"query": {
"multi_match": {
"query": "auth",
"type": "bool_prefix",
"fuzziness" : "AUTO",
"prefix_length" : 2,
"fields": [
"title",
"title._2gram",
"title._3gram",
"title._index_prefix"
]
}
}
}
Now I am able to get following:
a -> Authentication and authorization
au -> Authentication and authorization
aut -> Authentication and authorization
...
But when I misspell a word to autentication ES return nothing.
After some research I found out that fuzziness does'n work with bool_prefix. See:
- https://github.com/elastic/elasticsearch/issues/56229
- https://discuss.elastic.co/t/fuzziness-not-work-with-bool-prefix-multi-match-search-as-you-type/229602/3
So please is there some other way how to achieve this desired behavior? Or elasticsearch technology is just incapable to achieve this functionality?