Elasticsearch autocomplete suggester

谁说我不能喝 提交于 2020-04-07 07:08:54

问题


I want to implement autocomplete feature in ES I have followed this tutorial from ES documentations

my Indexing is:

{
    "properties" : {
            "suggest" : {
                "type" : "completion"
            },
            "titleKeyword" : {
                "type": "keyword"
            }
        }
    }
}

I am putting text value in titleKeyword field

then I queried like follows

{
  "suggest" : {
    "my-suggestion" : {
      "text" : "iphone",
      "term" : {
        "field" : "titleKeyword"
      }
    }
  }
}

The result is:

...
    "suggest": {
        "my-suggestion": [
            {
                "text": "iphone",
                "offset": 0,
                "length": 6,
                "options": []
            }
        ]
    }

expected result is

options:[iphone x, iphone 11, iphone 11 pro, iphone 7]

Note: titles are complete sentences like "iPhone x 64gb black"

as far as I understood from the documentation that I have to give the suggestions in "advance" when creating the document which is impossible in my case is there any way to implement this feature without putting suggestions manually?

Thanks


回答1:


For this simple requirement, no need to use the Elasticsearch suggestor which is difficult to understand and some part of it is still under development, More info on blog and what all to take care when building advance autocomplete is in my this SO answer.

Coming to your requirements which can be easily solved using the text field(looks like you are using keyword type) and simple match query as shown below:

Index Def

{
    "mappings": {
        "properties": {
            "model_name": {
                "type": "text"
            }
        }
    }
}

Index all 4 types of iphone

{
   "model_name" : "iphone x"
}

{
   "model_name" : "iphone 11"
}

{
   "model_name" : "iphone 11 pro"
}

{
   "model_name" : "iphone 7"
}

Search query (iphone will bring all four docs)

{
    "query": {
        "match": {
            "model_name": {
                "query": "iphone"
            }
        }
    }
}

Search result

{
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.110377684,
            "_source": {
               "model_name": "iphone x"
            }
         },
         {
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.110377684,
            "_source": {
               "model_name": "iphone 11"
            }
         },
         {
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "4",
            "_score": 0.110377684,
            "_source": {
               "model_name": "iphone 7"
            }
         },
         {
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.09271726,
            "_source": {
               "model_name": "iphone 11 pro"
            }
         }
      ]


来源:https://stackoverflow.com/questions/60991499/elasticsearch-autocomplete-suggester

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!