I have a case where I need to find suggestions based on a given text.
My basic need is to get suggestions, in an order that the ones matching exactly need to be first, and the the fuzzy ones. I have attached the solution i have opted.
For example searching for
'amazon ' returns text starting with amazon only,and not 'the amazon' or 'welcome to amazon'.
What can I do to get such results? Also , what about text such as 'theamazon' or 'cometoamazon'?I am using two different suggestors to obtain exact and fuzzy results. Any help is much appreciated. I am using completion suggestor and settings and mappings are as-
PUT search_queries_sug/
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 30
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
The mapping looks as-
PUT search_queries_sug/_mapping/realtime
{
"realtime": {
"properties": {
"suggest_exact" : {
"type" : "completion",
"analyzer":"autocomplete",
"preserve_position_increments":false,
"preserve_separators" : false,
"search_analyzer":"standard"
},
"suggest_fuzzy" : {
"type" : "completion",
"analyzer":"autocomplete",
"preserve_position_increments":false,
"preserve_separators" : false,
"search_analyzer":"standard"
},
"datetime": {
"type": "date"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"search_count": {
"type": "long"
}
}
}
}
This is how I am indexing suggestions-
POST /search_queries_sug/realtime/_update_by_query?
{
"query": {
"match_all":{}
},
"script": "ctx._source.suggest_exact = ctx._source.name;ctx._source.suggest_fuzzy = ctx._source.name "
}
This is how a document looks like before querying-
{
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "40d3b294ed2ef49f12185d698b2f9e3d",
"_score": 1,
"_source": {
"datetime": "2018-07-19T15:09:55Z",
"name": "Sing along with Goutam",
"suggest_fuzzy": "Sing along with Goutam",
"search_count": 1,
"suggest_exact": "Sing along with Goutam"
}
}
After querying, result is as--
"hits": {
"total": 0,
"max_score": 0,
"hits": []
},
"suggest": {
"text-suggest-exact": [
{
"text": "amazon",
"offset": 0,
"length": 6,
"options": [
{
"text": "Amazon",
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "b3b3a6ac74ecbd56bcdbefa4799fb9df",
"_score": 6,
"_source": {
"datetime": "2018-07-24T20:58:40Z",
"name": "Amazon",
"suggest_fuzzy": "Amazon",
"search_count": 1,
"suggest_exact": "Amazon"
}
],
"text-suggest-fuzzy": [
{
"text": "amazon",
"offset": 0,
"length": 6,
"options": [
{
"text": "Amazon",
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "b3b3a6ac74ecbd56bcdbefa4799fb9df",
"_score": 5,
"_source": {
"datetime": "2018-07-24T20:58:40Z",
"name": "Amazon",
"suggest_fuzzy": "Amazon",
"search_count": 1,
"suggest_exact": "Amazon"
}
},
{
"text": "Amazing dance",
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "6a32e5fb887e6ab362ab00c74ea62e07",
"_score": 4,
"_source": {
"datetime": "2018-07-27T09:48:41Z",
"name": "Amazing dance",
"suggest_fuzzy": "Amazing dance",
"search_count": 1,
"suggest_exact": "Amazing dance"
}
}