What you are looking for is called infix search and can be easily accomplished using the ngram token filter, Please see below complete working example, which is better than doing the wildcard searches and doesn't use the query string
which is not recommended for search boxes, as mentioned in official docs.
Because it returns an error for any invalid syntax, we don’t recommend
using the query_string query for search boxes.
Index def
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "ngram",
"min_gram": 1,
"max_gram": 10
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
},
"index.max_ngram_diff": 10
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
Index your sample doc
{
"title":"This is a sample text to show how the search results works"
}
** Search for your text**
{
"query": {
"match": {
"title": {
"query": "mple tex"
}
}
}
}
Search result with score
"max_score": 0.9168506,
"hits": [
{
"_index": "my-index",
"_type": "_doc",
"_id": "1",
"_score": 0.9168506,
"_source": {
"title": "This is a sample text to show how the search results works"
}
}
]
Note:Please refer my detailed answer on how to choose best autocomplete approach based on functional and non-functional requirements and with their trade-off