文章目錄
- 概述
- match_phrase_prefix
- 官方說明
- 例子
- 總結

概述
繼續跟中華石杉老師學習ES,第22篇
課程位址: https://www.roncoo.com/view/55
match_phrase_prefix
官方說明
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html
搜尋推薦,search as you type,搜尋提示,解釋一下什麼意思
假設有這麼幾個doc 如下
hello world
hello we
hello win
hello wind
hello dog
hello cat
搜尋 hello w
hello world
hello we
hello win
hello wind
會給出提示 如何上 ,搜尋推薦的功能
這種效果
例子
造點資料
PUT /my_index1/my_type1/1
{
"content":"hello Jack"
}
PUT /my_index1/my_type1/2
{
"content":"hello John"
}
PUT /my_index1/my_type1/3
{
"content":"hello Jose"
}
PUT /my_index1/my_type1/4
{
"content":"hello Dave"
}
查詢
GET /my_index1/my_type1/_search
{
"query": {
"match_phrase_prefix": {
"content": "hello J"
}
}
}
傳回
{
"took": 38,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.7509375,
"hits": [
{
"_index": "my_index1",
"_type": "my_type1",
"_id": "2",
"_score": 1.7509375,
"_source": {
"content": "hello John"
}
},
{
"_index": "my_index1",
"_type": "my_type1",
"_id": "1",
"_score": 1.1507283,
"_source": {
"content": "hello Jack"
}
},
{
"_index": "my_index1",
"_type": "my_type1",
"_id": "3",
"_score": 1.1507283,
"_source": {
"content": "hello Jose"
}
}
]
}
}
總結
match_phrase_prefix原理跟match_phrase類似,唯一的差別,就是把最後一個term作為字首去搜尋
- hello就是去進行match,搜尋對應的doc
- w,會作為字首,去掃描整個反向索引,找到所有w開頭的doc
- 然後找到所有doc中,即包含hello,又包含w開頭的字元的doc
- 根據你的slop去計算,看在slop範圍内,能不能讓hello w,正好跟doc中的hello和w開頭的單詞的position相比對
- 也可以指定slop,但是隻有最後一個term會作為字首
- max_expansions:指定prefix最多比對多少個term,超過這個數量就不繼續比對了,限定性能
- 預設情況下,字首要掃描所有的反向索引中的term,去查找w打頭的單詞,但是這樣性能太差。可以用max_expansions限定,w字首最多比對多少個term,就不再繼續搜尋反向索引了。