3-Elasticsearch深入搜索-近似匹配
一起來(lái)玩Elasticsearch,加我微信:wx1250134974
Elasticsearch認(rèn)證復(fù)習(xí)準(zhǔn)備
https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html
##近似匹配概念:
舉例說(shuō)明:對(duì)于“eat food” ,不僅僅能區(qū)分是否包含eat或者food這一層,還能區(qū)分eat food這個(gè)關(guān)系(這個(gè)關(guān)系也僅僅是分詞之間臨近)。可以用這個(gè)功能做自動(dòng)機(jī)器人應(yīng)答,把歷史的問(wèn)與答存儲(chǔ)好,通過(guò)檢索更符合的問(wèn)題,找到歷史應(yīng)答。
?
?
1、近似匹配
GET /my_index/my_type/_search
{
????"query": {
????????"match_phrase": {
????????????"title": "quick brown fox"
????????}
????}
}
注:所有的term必須都出現(xiàn),并且位置順序也必須一樣
?
?
GET /my_index/my_type/_search
{
????"query": {
????????"match_phrase": {
????????????"title": {
???????????? "query": "quick fox",
???????????? "slop": ?1
????????????}
????????}
????}
}
注:所有term必須全部出現(xiàn),位置約束可以放寬一些(slop 控制位置約束嚴(yán)格的靈活度)
?
?
?
?
?
?
?
?
GET /my_index/my_type/_search
{
??"query": {
????"bool": {
??????"must": {
????????"match": {
??????????"title": {
????????????"query": ???????????????"quick brown fox",
????????????"minimum_should_match": "30%"
??????????}
????????}
??????},
??????"should": {
????????"match_phrase": {
??????????"title": {
????????????"query": "quick brown fox",
????????????"slop": ?50
??????????}
????????}
??????}
????}
??}
}
注:使用鄰近度來(lái)提高相關(guān)度,這樣可以返回較多的結(jié)果,且鄰近度高的會(huì)排在前面。
?
?
GET /my_index/my_type/_search
{
????"query": {
????????"match": { ?
????????????"title": {
????????????????"query": ???????????????"quick brown fox",
????????????????"minimum_should_match": "30%"
????????????}
????????}
????},
????"rescore": {
????????"window_size": 50,
????????"query": { ????????
????????????"rescore_query": {
????????????????"match_phrase": {
????????????????????"title": {
????????????????????????"query": "quick brown fox",
????????????????????????"slop": ?50
????????????????????}
????????????????}
????????????}
????????}
????}
}
注:
match 查詢決定哪些文檔將包含在最終結(jié)果集中,并通過(guò) TF/IDF 排序。
window_size 是每一分片進(jìn)行重新評(píng)分的頂部文檔數(shù)量。
?
?
?
PUT /my_index2
{
????"settings": {
????????"number_of_shards": 1, ?
????????"analysis": {
????????????"filter": {
????????????????"my_shingle_filter": {
????????????????????"type": ????????????"shingle",
????????????????????"min_shingle_size": 2,
????????????????????"max_shingle_size": 2,
????????????????????"output_unigrams": ?false ??
????????????????}
????????????},
????????????"analyzer": {
????????????????"my_shingle_analyzer": {
????????????????????"type": ????????????"custom",
????????????????????"tokenizer": ???????"standard",
????????????????????"filter": [
????????????????????????"lowercase",
????????????????????????"my_shingle_filter"
????????????????????]
????????????????}
????????????}
????????}
????}
}
注:
?
?
A、"min_shingle_size"和"max_shingle_size" 默認(rèn)最小/最大的 shingle 大小是 2 ,所以實(shí)際上不需要設(shè)置。
B、"output_unigrams" shingle 語(yǔ)匯單元過(guò)濾器默認(rèn)輸出 unigrams ,但是我們想讓 unigrams 和 bigrams 分開(kāi)。
C、my_shingle_analyzer 使用我們常規(guī)的 my_shingles_filter 語(yǔ)匯單元過(guò)濾器。
?
?
GET /my_index2/_analyze?analyzer=my_shingle_analyzer
{
??"text":"Sue ate the alligator"
}
注:看下分詞效果,使用的話就直接用即可,最好把這個(gè)分詞的效果單獨(dú)存放使用。
?
?
?
?
一起來(lái)玩Elasticsearch,加我微信:wx1250134974
?
總結(jié)
以上是生活随笔為你收集整理的3-Elasticsearch深入搜索-近似匹配的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Kilav】 数据库复习资料 其一
- 下一篇: VMC(VMware on AWS)分析