2019年7月2日 星期二

[elasticsearch] 關於 Autocomplete using Elasticsearch Completion Suggester

關於 Autocomplete using Elasticsearch Completion Suggester

在搜尋功能中,有時候會使用 auto complete 來建議一些搜尋詞,校正使用者的輸入,或是提供熱門的搜尋詞。
透過 Elasticsearch 提供的
Completion Suggester | Elasticsearch Reference [7.2] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
就可以方便的達到這個功能。
透過一個簡單的的例子來說明,

建立 index mapping

curl -X PUT “localhost:9200/music” -H ‘Content-Type: application/json’ -d’
{
“mappings”: {
“properties” : {
“suggest” : {
“type” : “completion”
},
“title” : {
“type”: “keyword”
}
}
}
}

索引資料

curl -X PUT “localhost:9200/music/_doc/1?refresh” -H ‘Content-Type: application/json’ -d’
{
“suggest” : {
“input”: [ “Nevermind”, “Nirvana” ],
“weight” : 34
}
}

怎麼搜尋

curl -X POST “localhost:9200/music/_search?pretty” -H ‘Content-Type: application/json’ -d’
{
“suggest”: {
“song-suggest” : {
“prefix” : “nir”,
“completion” : {
“field” : “suggest”
}
}
}
}

response
{ “_shards” : { “total” : 1, “successful” : 1, “skipped” : 0, “failed” : 0 }, “hits”: … “took”: 2, “timed_out”: false, “suggest”: { “song-suggest” : [ { “text” : “nir”, “offset” : 0, “length” : 3, “options” : [ { “text” : “Nirvana”, “_index”: “music”, “_type”: “_doc”, “_id”: “1”, “_score”: 1.0, “_source”: { “suggest”: [“Nevermind”, “Nirvana”] } } ] } ] } }
  • 可以透過weight 的調整
  • 可以使用 skip_duplicates
    Whether duplicate suggestions should be filtered out (defaults to false).

怎麼做模糊搜尋 Fuzzy queries

curl -X POST “localhost:9200/music/_search?pretty” -H ‘Content-Type: application/json’ -d’
{
“suggest”: {
“song-suggest” : {
“prefix” : “nor”,
“completion” : {
“field” : “suggest”,
“fuzzy” : {
“fuzziness” : 2
}
}
}
}
}

  • 可以使用
    If you want to stick with the default values, but still use fuzzy, you can either use fuzzy: {} or fuzzy: true.
    讓 fuzzy 的參數根據字數調整

怎麼加入條件類別做 filter 呢

可以使用
Context Suggester | Elasticsearch Reference [7.2] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/suggester-context.html
把某個欄位變成 context 的 filter 條件

建立index mapping

curl -X PUT “localhost:9200/place” -H ‘Content-Type: application/json’ -d’
{
“mappings”: {
“properties” : {
“suggest” : {
“type” : “completion”,
“contexts”: [
{
“name”: “place_type”,
“type”: “category”
},
{
“name”: “location”,
“type”: “geo”,
“precision”: 4
}
]
}
}
}
}

  • contexts 的 type 可以使用 category 與 geo

索引文件

curl -X PUT “localhost:9200/place/_doc/1” -H ‘Content-Type: application/json’ -d’
{
“suggest”: {
“input”: [“timmy\u0027s”, “starbucks”, “dunkin donuts”],
“contexts”: {
“place_type”: [“cafe”, “food”]
}
}
}

或是直接索引該欄位
curl -X PUT “localhost:9200/place_path_category/_doc/1” -H ‘Content-Type: application/json’ -d’
{
“suggest”: [“timmy\u0027s”, “starbucks”, “dunkin donuts”],
“cat”: [“cafe”, “food”]
}

怎麼搜尋類別

curl -X POST “localhost:9200/place/_search?pretty” -H ‘Content-Type: application/json’ -d’
{
“suggest”: {
“place_suggestion” : {
“prefix” : “tim”,
“completion” : {
“field” : “suggest”,
“size”: 10,
“contexts”: {
“place_type”: [ “cafe”, “restaurants” ]
}
}
}
}
}

使用 Elasticsearch Completion Suggester 就可以方便達到 Autocomplete 模糊建議與根據類別來限制條件了。

沒有留言:

張貼留言