MySQL full text search equivalents to Google search

From LemonWiki共筆
Jump to navigation Jump to search

AND[edit]

Google search: keyword1 keyword2 same as keyword1 AND keyword2 or keyword1 +keyword2. Icon_exclaim.gif (1) The following is exact words search. (2) Replace column_name with your column name

  • Google: 易筋經 AND 吸星大法
  • MySQL: column_name REGEXP '易筋經' AND column_name REGEXP '吸星大法'
  • MySQL: column_name LIKE '%易筋經%' AND column_name LIKE '%吸星大法%' (online demo[1])
  • MySQL: IF(LOCATE('易筋經', column_name) > 0) AND IF(LOCATE('吸星大法, column_name) > 0)
  • MySQL: column_name LIKE '%易筋經%吸星大法%' AND column_name LIKE '%吸星大法%易筋經%' Icon_exclaim.gif Trivial for multiple keywords

OR[edit]

Google search: keyword1 OR keyword2

  • Google: 易筋經 OR 吸星大法
  • MySQL: column_name REGEXP '易筋經' OR column_name REGEXP '吸星大法'
  • MySQL: IF(LOCATE('易筋經', column_name) > 0) OR IF(LOCATE('吸星大法, column_name) > 0)
  • MySQL: column_name LIKE '%易筋經%' OR column_name LIKE '%吸星大法%' (online demo[2])

NOT[edit]

Google search: keyword1 NOT keyword2 same as keyword1 -keyword2

  • Google: 易筋經 NOT 吸星大法
  • MySQL: column_name REGEXP '易筋經' AND column_name NOT REGEXP '吸星大法' (online demo)
  • MySQL: IF(LOCATE('易筋經', column_name) > 0) AND IF(LOCATE('吸星大法, column_name) = 0)
  • MySQL: column_name LIKE '%易筋經%' AND column_name NOT LIKE '%吸星大法%'

* wildcard operator[edit]

Google * wildcard operator. "Use *, an asterisk character, known as a wildcard, to match one or more words in a phrase" [1] (online demo)

  • Google: 狐狸*叫
  • MySQL: column_name LIKE '狐狸%叫'[2]

English issue[edit]

When the keyword is short and written in English e.g. AI, the query result using column_name LIKE '%AI%' may NOT what you want e.g. Tainan, main, hair and so on.

  • (1) Remove all non-alpha-numeric-characters[3] (2) REGEXP word boundaries[4] e.g. (REPLACE(CONVERT(column_name USING ascii), '?', ' ') REGEXP '([[:<:]])AI([[:>:]])')


Cited from MySQL :: MySQL 5.7 Reference Manual :: 12.5.2 Regular Expressions

[[:<:]], [[:>:]]

These markers stand for word boundaries. They match the beginning and end of words, respectively. A word is a sequence of word characters that is not preceded by or followed by word characters. A word character is an alphanumeric character in the alnum class or an underscore (_).

教學文章:解決簡短英文單字的 MySQL 查詢:搜尋 app 而不是 apple

Ignore special characters[edit]

Ignore return symbol and span tag

  • Example:
    • Searched the keywords e.g. "意法" site:ptt.cc on Google and found the search result contains 意 & 法 located in the nearest but different rows. 意 is at the end of the n-th row. 法 is at the beginning of n+1-th row [3].
  • Approach: (1) remove the html tag (2) remove the return symbol (Carriage return).

Ignore white spaces, Halfwidth and fullwidth symbol (半形字元和全形字元)

  • Examples:
    • Searched the keywords e.g. "嗎有" on Google and found the search result contains 嗎? 有 & 嗎- 有.
    • Searched the keywords e.g. "人物誌Persona" on Google and found the search result contains 人物誌(Persona), 人物誌(Persona) & 「人物誌」(persona).
  • Approach: (1) remove the space symbol (2) remove the Halfwidth and fullwidth symbol.
  • References: PHP remove symbols from string - Stack Overflow

Highlight search query keywords on resulting pages[edit]

Returned result: Show 10 characters before or after the search keywords. (cf: Total 130 ~ 240 characters on Google resulting pages.)

MySQL approach[edit]

SQL syntax[edit]

Input search keywords, and returned the the first occurrence of matched paragraph. Using MySQL SUBSTRING() function, POSITION() function & CHAR_LENGTH() function.

SET @term := "吸星大法";
SET @message := "笑傲江湖中嵩山派掌門左冷禪所創掌法,可發出至陰至寒的真氣。左冷禪與任我行比武時,以此功對付吸星大法,使其全身凍僵、天池穴被封;與岳不群比劍奪帥時,左又使出寒冰神掌,與紫霞神功旗鼓相當、不分勝敗。

原文網址:https://kknews.cc/zh-tw/culture/xzaxbq.html";


SELECT 
@message

, CASE
  WHEN POSITION(@term IN @message) > 0 THEN SUBSTRING(@message
        , IF(
            POSITION(@term IN @message) > 0 &&
            POSITION(@term IN @message) -10 < 0
            , 1
            , POSITION(@term IN @message) -10)
        , CHAR_LENGTH(@term) + 20
      )
  ELSE ''
END AS `scrapbook`

-- Returned result of scrapbook column: Show 10 characters before or after the search keywords.
-- 行比武時,以此功對付吸星大法,使其全身凍僵、天池

Run on sqlfiddle

Instruction of SQL syntax[edit]

(1) MySQL POSITION() function - w3resource "MySQL POSITION() returns the position of a substring within a string."

SET @term := "吸星大法";
SET @message := "笑傲江湖中嵩山派掌門左冷禪所創掌法,可發出至陰至寒的真氣。左冷禪與任我行比武時,以此功對付吸星大法,使其全身凍僵、天池穴被封;與岳不群比劍奪帥時,左又使出寒冰神掌,與紫霞神功旗鼓相當、不分勝敗。

原文網址:https://kknews.cc/zh-tw/culture/xzaxbq.html";
SELECT POSITION(@term IN @message)

-- > returns 46

(2) Avoid the the start position is 0 or negative. Minimum start position of each paragraph is 1.

SELECT IF(
            POSITION(@term IN @message) > 0 &&
            POSITION(@term IN @message) -10 < 0
            , 1
            , POSITION(@term IN @message) -10)

-- > returns 36 = 46 - 10

(3) Show 10 characters before or after the search keywords. MySQL SUBSTRING() function - w3resource"returns a specified number of characters from a particular position of a given string."

SELECT 
@message

, CASE
  WHEN POSITION(@term IN @message) > 0 THEN SUBSTRING(@message
        , IF(
            POSITION(@term IN @message) > 0 &&
            POSITION(@term IN @message) -10 < 0
            , 1
            , POSITION(@term IN @message) -10)
        , CHAR_LENGTH(@term) + 20
      )
  ELSE ''
END AS `scrapbook`;

-- > returns 行比武時,以此功對付吸星大法,使其全身凍僵、天池
SET @term := "吸星大法";
SET @message := "原文網址:https://kknews.cc/zh-tw/culture/xzaxbq.html";


SELECT 
@message

, CASE
  WHEN POSITION(@term IN @message) > 0 THEN SUBSTRING(@message
        , IF(
            POSITION(@term IN @message) > 0 &&
            POSITION(@term IN @message) -10 < 0
            , 1
            , POSITION(@term IN @message) -10)
        , CHAR_LENGTH(@term) + 20
      )
  ELSE ''
END AS `scrapbook`

-- Returned result of scrapbook column: Show 10 characters before or after the search keywords.
-- [EMPTY]

Google sheet approach[edit]

Using REGEXEXTRACT function Icon_exclaim.gif case-sensitive!:

A B
1 文章 笑傲江湖中嵩山派掌門左冷禪所創掌法,可發出至陰至寒的真氣。左冷禪與任我行比武時,以此功對付吸星大法,使其全身凍僵、天池穴被封;與岳不群比劍奪帥時,左又使出寒冰神掌,與紫霞神功旗鼓相當、不分勝敗。 原文網址:https://kknews.cc/zh-tw/culture/xzaxbq.html
2 關鍵字 吸星大法
3 搜尋結果摘要 =IF(ISERROR(REGEXEXTRACT(LOWER(B1), "(.{10}"&B2&".{10})")), "", REGEXEXTRACT(LOWER(B1), "(.{10}"&B2&".{10})"))

Microsoft Spreadsheet approach[edit]

Using FIND, MID & CONCATENATE functions. Icon_exclaim.gif FIND function is case-sensitive!

A B
1 文章 笑傲江湖中嵩山派掌門左冷禪所創掌法,可發出至陰至寒的真氣。左冷禪與任我行比武時,以此功對付吸星大法,使其全身凍僵、天池穴被封;與岳不群比劍奪帥時,左又使出寒冰神掌,與紫霞神功旗鼓相當、不分勝敗。 原文網址:https://kknews.cc/zh-tw/culture/xzaxbq.html
2 關鍵字 吸星大法
3 搜尋結果摘要 =IF(ISERROR(FIND(B2, B1)), "", CONCATENATE(MID(B1, IF(FIND(B2, B1)-10 >= 1, FIND(B2, B1)-10, 1), 10), MID(B1, FIND(B2, B1), 10+LEN(B2))))

Try it online

PHP approach[edit]

PHP solution: php - highlight multiple keywords in search - Stack Overflow Unverified

Ranking factors[edit]

Possibile factors

References or related articles[edit]

to explore strange new worlds / related articles:

other search cases: if the column ... (inspired by OutWit)

  • contains ____
  • does not contain ____
  • begins with ____
  • does not begins with ____
  • ends with ____
  • does not ends with ____
  • equals to ____
  • does not equal ____

references

Related news[edit]

MySQL OR nosql related news
甲骨文宣佈推出MySQL HeatWave on AWS 使用者現可透過MySQL使用單一服務執行交易處理、即時分析和機器學習 - 新頭條-Thehubnews
微軟NoSQL資料庫Azure Cosmos DB現可添加快取加速讀取、降低成本 - iThome Online
数据库(mysql)主从复制与读写分离_芒地狠的博客 - CSDN
对话 MySQL 之父Monty:超越 MySQL 很难,但我做到了!_《新程序员》编辑部的博客 - CSDN
[Spring Boot 3] 整合NoSQL与构建RESTful服务_三金C_C的博客 - CSDN
美团三面:一直追问我, MySQL 幻读被彻底解决了吗?_肥肥技术宅的博客 - CSDN
一条sql 了解MYSQL 的架构设计_奋斗的工程师的博客 - CSDN
研究發現有360萬臺MySQL伺服器曝露於公開網路 - iThome Online
MySQL数据库基本操作_Drw_Dcm的博客 - CSDN
Google Cloud的NoSQL資料庫服務新增自動擴展功能 - iThome Online
使用docker-compose搭建mysql主从复制_小小白鸽的博客 - CSDN
【Camunda 二】Springboot集成Camunda工程(使用H2,Mysql, Postgresql数据库)_风情客家__的博客 - CSDN
【資安日報】2022年7月29日,駭客竊取SQL Server與MySQL伺服器網路頻寬牟利、臺廠Moxa修補工控裝置伺服器設備零時差漏洞 - iThome Online
英特尔oneAPI工具大幅提升腾讯云数据库MySQL的性能-面包板社区 - 电子工程专辑
【Mysql】同生共死的sql语句之——事务_鸡兄长高了的博客 - CSDN
MySQL高级篇知识点——MySQL 事务日志_小城老街的博客 - CSDN
MySQL主从复制读写分离_小影~的博客 - CSDN
猿创征文|MYSQL主从复制_踏风彡的博客 - CSDN
MySQL高级篇_渣渣苏的博客 - CSDN
阿里云:加大NoSQL数据库软硬件一体化技术自研_阿里云开发者的博客 - CSDN
日志系统:一条更新SQL是如何执行的?_XHHP的博客 - CSDN
甲骨文推出MySQL HeatWave ML,新增機器學習功能 - T客邦 Techbang
【資安日報】2022年6月1日,逾360萬臺MySQL伺服器曝露於網際網路、中國駭客利用Office零時差漏洞Follina發動攻擊 - iThome Online
甲骨文以機器學習強化MySQL Heatwave,叫戰Amazon Aurora、Google BigQuery - iThome Online
Google釋出供用戶稽核企業資料庫的Cloud SQL for MySQL擴充套件 - iThome Online
《MySQL》增删查改(进阶)_小连~的博客 - CSDN
Cloud SQL for MySQL正式支援IAM資料庫身分驗證 - iThome Online
甲骨文讓MySQL同時結合OLTP及OLAP能力 - iThome Online
劍與魔法RPG《DB工程師騎士與Query魔女》揭新PV 支援28種程式語言MySQL - udn 遊戲角落
技术分享| 客户说insert 慢,我该怎么办_ActionTech的博客 - CSDN
花2个月面过华为测开岗,拿个30K不过分吧?_小梧敲代码的博客 - CSDN
Lua脚本在Redis事务中的应用实践_京东云开发者的博客 - CSDN

Powered by Google News