Elasticsearch autocomplete suggester

2020-05-01 04:58发布

问题:

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"
            }
         }
      ]