想要使用 Elastic search 工具來實現搜索框的提示功能,能夠按照本文的步驟來進行設置。app
安裝好 jieba 插件,若是有 ik 也可,可是相應的下面的 analyzer 做出相應的改變便可。工具
定義 mappingspa
PUT games
{
"mappings": {
"properties" : {
"title" : {
"type": "completion"
}
}
}
}
複製代碼
增長數據插件
PUT /games/_doc/1
{
"title": "Nevermind"
}
PUT /games/_doc/2
{
"title": "Nirvana"
}
複製代碼
查找code
POST games/_search?pretty
{
"suggest": {
"song-suggest" : {
"prefix" : "nor",
"completion" : {
"field" : "title",
"fuzzy" : {
"fuzziness" : 2
}
}
}
}
}
複製代碼
結果ip
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"song-suggest" : [
{
"text" : "nor",
"offset" : 0,
"length" : 3,
"options" : [
{
"text" : "Nevermind",
"_index" : "games",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"title" : "Nevermind"
}
},
{
"text" : "Nirvana",
"_index" : "games",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "Nirvana"
}
}
]
}
]
}
}
複製代碼