国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Elasticsearch實(shí)現(xiàn)Mysql的Like效果

這篇具有很好參考價(jià)值的文章主要介紹了Elasticsearch實(shí)現(xiàn)Mysql的Like效果。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

在Mysql數(shù)據(jù)庫中,模糊搜索通常使用LIKE關(guān)鍵字。然而,隨著數(shù)據(jù)量的不斷增加,Mysql在處理模糊搜索時(shí)可能面臨性能瓶頸。因此,引入Elasticsearch作為搜索引擎,以提高搜索性能和用戶體驗(yàn)成為一種合理的選擇。

1、客戶的訴求

在ES中,影響搜索結(jié)果的因素多種多樣,包括分詞器、Match搜索、Term搜索、組合搜索等。有些用戶已經(jīng)養(yǎng)成了在Mysql中使用LIKE進(jìn)行模糊搜索的習(xí)慣。若ES返回的搜索結(jié)果不符合用戶的預(yù)期,可能會(huì)引發(fā)抱怨,甚至認(rèn)為系統(tǒng)存在Bug。

誰讓客戶是上帝,客戶是金主爸爸呢,客戶有訴求,我們就得安排上。下面我們就聊聊如何用ES實(shí)現(xiàn)Mysql的like模糊匹配效果。

如果對Elasticsearch不太熟悉的讀者,建議先閱讀我之前的文章:

《5000字詳說Elasticsearch入門》

《Springboot項(xiàng)目中使用Elasticsearch的RestClient》

《巧記Elasticsearch常用DSL語法》

2、短語匹配match_phrase

2.1、定義

為實(shí)現(xiàn)模糊匹配的搜索效果,通常有兩種方式,其中之一是match_phrase,先說說match_phrase。

match_phrase短語匹配會(huì)對檢索內(nèi)容進(jìn)行分詞,要求這些分詞在被檢索內(nèi)容中全部存在,并且順序必須一致。默認(rèn)情況下,這些詞必須是連續(xù)的。

2.2、實(shí)驗(yàn)

  • 場景1:創(chuàng)建一個(gè)mapping,采用默認(rèn)分詞器(即每個(gè)字都當(dāng)做分詞),然后插入兩條數(shù)據(jù)。注意:被搜索的字段先采用text類型。
# 創(chuàng)建mapping,這里的customerName先使用text類型
PUT /search_test
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "customerName": {
        "type": "text"
      }
    }
  },
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  }
}

# 插入2條數(shù)據(jù)
PUT /search_test/_create/1
{
  "id": "111",
  "customerName": "都是生產(chǎn)醫(yī)院的人"
}

PUT /search_test/_create/2
{
  "id": "222",
  "customerName": "家電清洗"
}

# match_phrase短語匹配查詢,可以查出結(jié)果
POST search_test/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "customerName": "醫(yī)院的"
          }
        }
      ]
    }
  }
}

以上操作結(jié)果顯示可以查詢到數(shù)據(jù)。如下圖:

  • 場景2:創(chuàng)建一個(gè)mapping,采用默認(rèn)分詞器,然后插入兩條數(shù)據(jù)。注意:被搜索的字段先采用keyword類型。
# 創(chuàng)建mapping,這里的customerName先使用text類型
PUT /search_test2
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "customerName": {
        "type": "keyword"
      }
    }
  },
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  }
}

# 插入2條數(shù)據(jù)
PUT /search_test2/_create/1
{
  "id": "111",
  "customerName": "都是生產(chǎn)醫(yī)院的人"
}

PUT /search_test2/_create/2
{
  "id": "222",
  "customerName": "家電清洗"
}

# match_phrase短語匹配查詢,可以查出結(jié)果
POST search_test2/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "customerName": "醫(yī)院的"
          }
        }
      ]
    }
  }
}

以上操作結(jié)果顯示查不到數(shù)據(jù)。如下圖:

2.3、小結(jié)

match_phrase短語匹配適用于text類型的字段,實(shí)現(xiàn)了類似Mysql的like模糊匹配。然而,它并不適用于keyword類型的字段。

3、通配符匹配Wildcard

為實(shí)現(xiàn)模糊匹配的搜索效果,Wildcard通配符匹配是另一種常見的方式。下面我們詳細(xì)介紹wildcard通配符查詢。下面接著說Wildcard通配符查詢。

3.1、定義

Wildcard Query 是使用通配符表達(dá)式進(jìn)行查詢匹配。Wildcard Query 支持兩個(gè)通配符:

  • ?,使用 ? 來匹配任意字符。
  • *,使用 * 來匹配 0 或多個(gè)字符。

使用示例:

POST search_test/_search
{
  "query": {
    "wildcard": {
      "customerName": "*測試*"
    }
  }
}

3.2、實(shí)驗(yàn)

  • 場景1:創(chuàng)建一個(gè)mapping,采用默認(rèn)分詞器,然后插入兩條數(shù)據(jù)。注意:被搜索的字段先采用text類型。使用上文已經(jīng)創(chuàng)建的索引search_test。
# wildcard查詢
POST search_test/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "customerName": {
              "value": "*醫(yī)院的*"
            }
          }
        }
      ]
    }
  }
}

以上操作結(jié)果顯示查不到數(shù)據(jù),如下圖:

注意:如果將DSL查詢語句改成只查“醫(yī)”,就可以查到數(shù)據(jù),這與分詞器有關(guān)。默認(rèn)分詞器將每個(gè)字都切成分詞。

# Wildcard查詢
POST search_test/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "customerName": {
              "value": "*醫(yī)*"
            }
          }
        }
      ]
    }
  }
}
  • 場景2:創(chuàng)建一個(gè)mapping,采用默認(rèn)分詞器,然后插入兩條數(shù)據(jù)。注意:被搜索的字段先采用keyword類型。使用上文已經(jīng)創(chuàng)建的索引search_test2。
POST search_test2/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "customerName": {
              "value": "*醫(yī)院的*"
            }
          }
        }
      ]
    }
  }
}

以上操作結(jié)果顯示可以查到數(shù)據(jù),如下圖:

3.3、小結(jié)

Wildcard通配符查詢適用于keyword類型的字段,實(shí)現(xiàn)了類似Mysql的like模糊匹配。然而,它不太適用于text類型的字段。

4、選擇分詞器

上述實(shí)驗(yàn)中均使用了默認(rèn)分詞器的結(jié)果。接下來,我們嘗試使用IK中文分詞器進(jìn)行實(shí)驗(yàn)。

4.1、實(shí)驗(yàn)

  • 創(chuàng)建一個(gè)名為search_test3的mapping,采用IK中文分詞器,然后插入兩條數(shù)據(jù)。注意:被搜索的字段先采用text類型。
PUT /search_test3
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "customerName": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  },
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  }
}

PUT /search_test3/_create/1
{
  "id": "111",
  "customerName": "都是生產(chǎn)醫(yī)院的人"
}

PUT /search_test3/_create/2
{
  "id": "222",
  "customerName": "家電清洗"
}
  • 執(zhí)行搜索,比如搜索“醫(yī)院的”,無論是match_phrase還是wildcard兩種方式都查不到數(shù)據(jù)。
POST search_test3/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "customerName": "醫(yī)院的"
          }
        }
      ]
    }
  }
}

POST search_test3/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "customerName": {
              "value": "*醫(yī)院的*"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 20
}
  • 執(zhí)行搜索,比如搜索“醫(yī)院”,match_phrase和wildcard兩種方式都可以查到數(shù)據(jù)。
POST search_test3/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "customerName": "醫(yī)院"
          }
        }
      ]
    }
  }
}

POST search_test3/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "customerName": {
              "value": "*醫(yī)院*"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 20
}

4.2、小結(jié)

無論是match_phrase還是wildcard兩種方式,它們的效果與選擇的分詞器密切相關(guān)。因?yàn)閮烧叨际菍Ψ衷~進(jìn)行匹配,只有匹配到了分詞,才能找到對應(yīng)的文檔。

如果搜索內(nèi)容正好命中了對應(yīng)的分詞,就可以查詢到數(shù)據(jù)。如果沒有命中分詞,則查不到。在遇到問題時(shí),可以使用DSL查詢查看ES的分詞情況:

POST _analyze
{  
    "analyzer": "ik_smart",
    "text": "院的人"  
}
POST _analyze
{  
    "analyzer": "ik_smart",
    "text": "醫(yī)院的"  
}

POST _analyze
{  
    "analyzer": "ik_max_word",
    "text": "都是生產(chǎn)醫(yī)院的人"  
}

5、總結(jié)

match_phrase和wildcard都能實(shí)現(xiàn)類似Mysql的like效果。然而,需要注意以下幾點(diǎn):

  • 如果要完全實(shí)現(xiàn)Mysql的like效果,最好使用默認(rèn)分詞器,即每個(gè)字都切成分詞。
  • match_phrase短語匹配,適合于text類型的字段。
  • Wildcard通配符查詢,適合于keyword類型的字段。

本篇完結(jié)!感謝你的閱讀,歡迎點(diǎn)贊 關(guān)注 收藏 私信?。?!

原文鏈接:https://mp.weixin.qq.com/s/pXGQsGs1l8msIvP2aJCfWQ

?文章來源地址http://www.zghlxwxcb.cn/news/detail-825064.html

到了這里,關(guān)于Elasticsearch實(shí)現(xiàn)Mysql的Like效果的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包