根据字段的组合权重对结果进行排序

发布于 2024-11-28 06:39:44 字数 1166 浏览 1 评论 0原文

我有一个非常有趣的问题想问各位 SQL 大师。

众所周知,任何基于电子商务的购物车的首要要求之一就是良好的搜索。

几天前,我了解了 MySQL 全文搜索,我必须说它非常有趣。

我想在搜索查询中前进一步,并且需要为字段提供权重。这些权重将允许对搜索结果进行排序。

例如:我在产品表

productid
name
description
price

“名称”和“描述”字段中有以下字段是全文索引的。

对于任何搜索关键字,查询应查看两个字段:名称、描述。但与描述字段相比,名称字段应该具有更大的权重。

如果您还没有很好地理解我的问题,那么下面的示例应该会有所帮助。

 productid             name                   description                price
 1                  nice keyboard      this is a nice nice keyboard     10                
 2                  nice nice keyboard this is a nice nice keyboard     10                
 3                  keyboard           this is a nice keyboard     10                

当搜索关键字为“nice”,且“名称”字段的权重为:10,且“描述”字段的权重为:1

(产品 ID:1)时,在名称中找到 1(一次),在描述中找到 2(两次)。因此,对于productid:2,组合权重为(1 * 10 + 2 * 1 = 10 + 2 = 12),

在名称中找到了2(两次),在描述中找到了2(两次)。因此,对于productid:3,组合权重为(2 * 10 + 2 * 1 = 20 + 2 = 22),

在名称中发现了很好的0(无),在描述中发现了1(一次)。因此,组合权重为 (0*10 + 1*1 = 0 + 1 = 1)

2 个问题

(1) 如何为特定字段提供权重?是否需要重新创建全文索引来提供权重?

(2)我需要查询,可以输出desc中的结果。联合重量的顺序。

I have a very interesting question for all you SQL masters out there.

As you all know that one of the prime requirement of any e-commerce based shopping cart is a good search.

Few days back, I got to know about MySQL full-text search, and I must say it's quiet interesting.

I want to go one step ahead in search query, and need to provide weightage to fields. These weightage would allow sorting of search result.

For example: I have following fields in product table

productid
name
description
price

'name' and 'description' fields are full-text indexed.

For any search keyword, query should look into both fields: name, description. But name field should have more weightage compared to description field.

Incase, if you haven't understand my question well, then following example should help.

 productid             name                   description                price
 1                  nice keyboard      this is a nice nice keyboard     10                
 2                  nice nice keyboard this is a nice nice keyboard     10                
 3                  keyboard           this is a nice keyboard     10                

when search keyword is 'nice', and 'name' field have weightage: 10, and 'description' field have weightage: 1

for productid: 1, nice was found 1 (once) in name, 2 (twice) in description. so combine weightage is (1*10 + 2*1 = 10 + 2 = 12)

for productid: 2, nice was found 2 (twice) in name, 2 (twice) in description. so combine weightage is (2*10 + 2*1 = 20 + 2 = 22)

for productid: 3, nice was found 0 (none) in name, 1 (once) in description. so combine weightage is (0*10 + 1*1 = 0 + 1 = 1)

2 questions

(1) How can I provide weightage to a particular field? Do I need to create full-text index again to provide weightage?

(2) I need query, which can output results in desc. order of combine weightage.

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

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

发布评论

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

评论(1

耳根太软 2024-12-05 06:39:44

全文搜索可以为您提供搜索的相对相关性:

SELECT * FROM
(
    SELECT id,
    (MATCH(name) AGAINST ('nice')*10 ) + MATCH(description) AGAINST('nice') AS relevance
    FROM products
    WHERE MATCH(name) AGAINST ('nice') OR
    MATCH(description) AGAINST ('nice')
) ORDER BY revelence DESC

通过将名称相关性乘以 10,我将相关性得分偏向于名称的相关性得分

The full text search can give you the relative relevance of the search:

SELECT * FROM
(
    SELECT id,
    (MATCH(name) AGAINST ('nice')*10 ) + MATCH(description) AGAINST('nice') AS relevance
    FROM products
    WHERE MATCH(name) AGAINST ('nice') OR
    MATCH(description) AGAINST ('nice')
) ORDER BY revelence DESC

By multiplying the name relevance by 10 I have skewed the relevance score towards that of names

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