mysql score match_mysql 全文索引 ranking

这个示例展示了如何在MySQL中创建全文索引并进行搜索。通过`MATCH AGAINST`函数,我们可以对文章的标题和内容进行全文检索,并根据匹配度获取相关结果。实验部分还演示了布尔模式下搜索的评分计算方式,其中IDF和TF-IDF的概念被用来衡量关键词的重要性。
摘要由CSDN通过智能技术生成

mysql> CREATE TABLE articles (

-> id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,

-> title VARCHAR(200),

-> body TEXT,

-> FULLTEXT (title,body)

-> ) ENGINE=InnoDB;

Query OK, 0 rows affected (2.48 sec)

mysql> select * from articles;

Empty set (0.00 sec)

mysql> INSERT INTO articles (title,body) VALUES

-> (‘MySQL Tutorial‘,‘This database tutorial ...‘),

-> ("How To Use MySQL",‘After you went through a ...‘),

-> (‘Optimizing Your Database‘,‘In this database tutorial ...‘),

-> (‘MySQL vs. YourSQL‘,‘When comparing databases ...‘),

-> (‘MySQL Security‘,‘When configured properly, MySQL ...‘),

-> (‘Database, Database, Database‘,‘database database database‘),

-> (‘1001 MySQL Tricks‘,‘1. Never run mysqld as root. 2. ...‘),

-> (‘MySQL Full-Text Indexes‘, ‘MySQL fulltext indexes use a ..‘);

Query OK, 8 rows affected (0.06 sec)

Records: 8 Duplicates: 0 Warnings: 0

mysql> select * from articles;

+----+------------------------------+-------------------------------------+

| id | title | body |

+----+------------------------------+-------------------------------------+

| 1 | MySQL Tutorial | This database tutorial ... |

| 2 | How To Use MySQL | After you went through a ... |

| 3 | Optimizing Your Database | In this database tutorial ... |

| 4 | MySQL vs. YourSQL | When comparing databases ... |

| 5 | MySQL Security | When configured properly, MySQL ... |

| 6 | Database, Database, Database | database database database |

| 7 | 1001 MySQL Tricks | 1. Never run mysqld as root. 2. ... |

| 8 | MySQL Full-Text Indexes | MySQL fulltext indexes use a .. |

+----+------------------------------+-------------------------------------+

8 rows in set (0.00 sec)

mysql> select id,title,body,match(title,body) against("databas fulltext") from articles where match(title,body) against("databas fulltext");

+----+-------------------------+---------------------------------+-----------------------------------------------+

| id | title | body | match(title,body) against("databas fulltext") |

+----+-------------------------+---------------------------------+-----------------------------------------------+

| 8 | MySQL Full-Text Indexes | MySQL fulltext indexes use a .. | 0.8155715465545654 |

+----+-------------------------+---------------------------------+-----------------------------------------------+

1 row in set (0.04 sec)

实验:

mysql> select id,title,body,match(title,body) against("database" in boolean mode) as score from articles where match(title,body) against("database fulltext") order by score desc;

+----+------------------------------+---------------------------------+---------------------+

| id | title | body | score |

+----+------------------------------+---------------------------------+---------------------+

| 6 | Database, Database, Database | database database database | 1.0886961221694946 |

| 3 | Optimizing Your Database | In this database tutorial ... | 0.36289870738983154 |

| 1 | MySQL Tutorial | This database tutorial ... | 0.18144935369491577 |

| 8 | MySQL Full-Text Indexes | MySQL fulltext indexes use a .. | 0 |

+----+------------------------------+---------------------------------+---------------------+

4 rows in set (0.00 sec)

score的计算方法:

以id=6的行做例:

总记录数为:8

所有匹配到database的行数为:3

该行一共有database个数为6

IDF = log10(8/3)

TF = 6

score= TF*IDF*IDF

mysql> select (log10(8/3)*6*log10(8/3));

+---------------------------+

| (log10(8/3)*6*log10(8/3)) |

+---------------------------+

| 1.088696164686938 |

+---------------------------+

1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值