如何以百分比形式获取mysql匹配结果?

发布于 2024-10-21 08:09:01 字数 216 浏览 7 评论 0原文

我在 mysql 中使用 Match (Col1) Against (Val)

select match(body) against(body_var) from articles;

现在,如果完全匹配,我将得到数字形式的结果(例如 14.43)。 这个数字是什么意思?主要问题是我可以得到百分比形式的结果(例如0.94)
感谢您的帮助

I am using Match (Col1) Against (Val) in mysql.

select match(body) against(body_var) from articles;

now in case of completely match i am getting result as a number (for example 14.43).
what does this number mean? and the main question is can i get the result in percentage form (for example 0.94)
thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2024-10-28 08:09:01

可能有一种更简单的方法来做到这一点..不知怎的,我掉进了这个兔子洞..但它经过测试并且有效(返回结果的百分比),

SELECT (mthCount / ttlCount) AS mPercent
FROM (
  SELECT COUNT( * ) AS mthCount
  FROM articles WHERE (
     MATCH(body) AGAINST(body_var) 
     )
) AS MCount JOIN (
  SELECT COUNT( * ) AS ttlCount
  FROM articles
) AS TCount;

它返回一条记录/结果,其中包含 mPercent 列

您也可以拥有它四舍五入到小数点后两位...

SELECT FORMAT((mthCount / ttlCount),2) AS mPercent
FROM (
  SELECT COUNT( * ) AS mthCount
  FROM articles WHERE (
     MATCH(body) AGAINST(body_var) 
     )
) AS MCount JOIN (
  SELECT COUNT( * ) AS ttlCount
  FROM articles
) AS TCount;

正如我所说..我对 358 行和 50 个匹配项进行了测试
50/350 = 0.1396648...(第一个结果)
舍入结果为 0.14


如果您希望将单个结果的相关性值转换为百分比 - 这实际上不会发生...

来自 MATCH/AGAINST 的相关性值并不是匹配百分比的良好指标。这是通过互联网深入介绍.. 搜索“将相关性值转换为百分比”...

如果您想按相关性匹配百分比对结果进行排序,并且第一个结果始终具有 100% 相关性,您可以这样做

...尝试获取像 PHP 的相似文本这样的值 - 你最好将这项工作转移给客户端...

全文搜索相关性是用什么来衡量的?

http://forums.mysql.com/read.php?107,125239,146610#msg-146610

http://seminex.blogspot.com/2005/06/mysql-relevance-in-fulltext-search.html

There is probably a MUCH easier way to do this.. Somehow i fell down the rabbit hole on this one.. But its tested and works (returns percentage of results)

SELECT (mthCount / ttlCount) AS mPercent
FROM (
  SELECT COUNT( * ) AS mthCount
  FROM articles WHERE (
     MATCH(body) AGAINST(body_var) 
     )
) AS MCount JOIN (
  SELECT COUNT( * ) AS ttlCount
  FROM articles
) AS TCount;

it returns one record/result with the column mPercent

You could also have it round to two decimal places...

SELECT FORMAT((mthCount / ttlCount),2) AS mPercent
FROM (
  SELECT COUNT( * ) AS mthCount
  FROM articles WHERE (
     MATCH(body) AGAINST(body_var) 
     )
) AS MCount JOIN (
  SELECT COUNT( * ) AS ttlCount
  FROM articles
) AS TCount;

As i said.. I tested it against 358 rows with 50 matches
50/350 = 0.1396648... (for first result)
0.14 for rounded result


If you are looking to convert the relevance value to a percent for a single result - it isnt really going to happen...

The relevance value from the MATCH/AGAINST is not a good indicator of percent match.. This is covered in depth throught the internet.. Search for "Convert Relevance value to percent" ...

If you wanted to order your results by relevence match percent, with the first result always having 100% relevence, you can do that...

As for trying to get a value like PHP's similar_text - you are better to offload that work to the client...

Full-text search relevance is measured in?

http://forums.mysql.com/read.php?107,125239,146610#msg-146610

http://seminex.blogspot.com/2005/06/mysql-relevance-in-fulltext-search.html

千秋岁 2024-10-28 08:09:01

我想出的一个解决方法是找到最佳匹配,并使用它们来获得相对于这些最大值的百分比值,这在这种情况下可能并不完全有用,但它确实给了你一个想法。
我使用这种方法来查找重复项,首先插入行,然后运行此查询,最佳匹配当然是同一行。

首先,我必须选择最佳匹配:

SELECT
MAX(MATCH (table.col1)  AGAINST ('text 1'  IN NATURAL LANGUAGE MODE)) AS  bscore_col1 ,
MAX(MATCH (table.col2) AGAINST ('text 2'                          
IN NATURAL LANGUAGE MODE)) AS bscore_col2
FROM table
ORDER BY bscore_name col1 DESC, bscore_col2 DESC) AS bests

您当然可以添加多个列,但必须首先创建适当的全文搜索索引。

完整查询取第一次查询的结果作为参考,可以将比例更改为0.5,0.5意味着获得的分数必须>最佳分数的50%,如果你想获得所有结果,删除比较表达式。

SELECT *,
MATCH (table.col1) AGAINST ('text 1' IN NATURAL LANGUAGE MODE)/bests.bscore_col1 AS score_col1 ,
MATCH (table.col2) AGAINST ('text 2' IN NATURAL LANGUAGE MODE)/bests.bscore_col2 AS score_col2
FROM (table,
(SELECT
MAX(MATCH (table.col1)  AGAINST ('text 1'  IN NATURAL LANGUAGE MODE)) AS bscore_col1 ,
MAX(MATCH (table.col2) AGAINST ('text 2' IN NATURAL LANGUAGE MODE)) AS bscore_col2
FROM table
ORDER BY bscore_col2 DESC, bscore_col1 DESC) AS bests)
WHERE
MATCH (table.col1)  AGAINST ('text 1'  IN NATURAL LANGUAGE MODE)/bests.bscore_col1 > 0.5 AND
MATCH (table.col2) AGAINST ('text 2'IN NATURAL LANGUAGE MODE)/bests.bscore_col2 > 0.5
ORDER BY score_col2 DESC, score_col1 DESC

我不认为这是最好的解决方案,但它在我的情况下效果很好。

A work around i came up with is to find the best matches, and use them to have percentage values relative to these maxes, this may not be fully useful in this case, but it sure gives you an idea.
I use this method to look for duplicates, first i insert the row and then i run this query, the best match of course is the same row.

First i have to select the best match :

SELECT
MAX(MATCH (table.col1)  AGAINST ('text 1'  IN NATURAL LANGUAGE MODE)) AS  bscore_col1 ,
MAX(MATCH (table.col2) AGAINST ('text 2'                          
IN NATURAL LANGUAGE MODE)) AS bscore_col2
FROM table
ORDER BY bscore_name col1 DESC, bscore_col2 DESC) AS bests

You can of course add multiple columns, but you must create the appropriate full text search indexes first.

The full query takes the result from the first query and use them as references, you can change the ratio of 0.5, 0.5 means that the score obtained must be >50% of the best score, if you want to get all the results, remove the comparison expression.

SELECT *,
MATCH (table.col1) AGAINST ('text 1' IN NATURAL LANGUAGE MODE)/bests.bscore_col1 AS score_col1 ,
MATCH (table.col2) AGAINST ('text 2' IN NATURAL LANGUAGE MODE)/bests.bscore_col2 AS score_col2
FROM (table,
(SELECT
MAX(MATCH (table.col1)  AGAINST ('text 1'  IN NATURAL LANGUAGE MODE)) AS bscore_col1 ,
MAX(MATCH (table.col2) AGAINST ('text 2' IN NATURAL LANGUAGE MODE)) AS bscore_col2
FROM table
ORDER BY bscore_col2 DESC, bscore_col1 DESC) AS bests)
WHERE
MATCH (table.col1)  AGAINST ('text 1'  IN NATURAL LANGUAGE MODE)/bests.bscore_col1 > 0.5 AND
MATCH (table.col2) AGAINST ('text 2'IN NATURAL LANGUAGE MODE)/bests.bscore_col2 > 0.5
ORDER BY score_col2 DESC, score_col1 DESC

I don't believe this is the best solution, but it does well in my case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文