从全文搜索MATCH中添加分数

时间:2018-02-02 12:07:11

标签: mysql select nlp

我有两张桌子:

pages

|id |title      |
+---+-----------+
|1  |Foxes      |
|2  |Pet dogs   |

keywords

|id |page_id |phrase                |
+---+--------+----------------------+
|1  |1       |quick brown fox jumps |
|2  |1       |lazy dog              |
|3  |2       |caring for a dog      |
|4  |2       |pet dog               |

我目前正在使用此查询:

SELECT pages.title, MATCH (phrase) AGAINST ('pet dog') as relevance
FROM pages
INNER JOIN keywords
ON pages.id = keywords.page_id
WHERE MATCH (phrase) AGAINST ('pet dog')
ORDER BY relevance DESC;

我得到了这个结果:

|title |relevance            |
+------+---------------------+
|Dogs  |0.3780859112739563   |
|Foxes |0.015609688125550747 |
|Dogs  |0.015609688125550747 |

但我希望每个title只有一个relevance,因此每个适用于relevance的短语的title应该像这样加在一起:

|title |relevance            |
+------+---------------------+
|dogs  |0.3936955994         |
|foxes |0.015609688125550747 |

2 个答案:

答案 0 :(得分:3)

  1. 在结果集中添加GROUP BY title语句,因此相同title的记录将汇总到一个组中。
  2. 调用SUM函数获取每个组relevance的总和。
  3. 因此,此查询可能会帮助您:

    SELECT title, SUM(relevance)
    FROM
    (
      SELECT pages.title, MATCH (phrase) AGAINST ('pet dog') as relevance
      FROM pages
      INNER JOIN keywords
      ON pages.id = keywords.page_id
      WHERE MATCH (phrase) AGAINST ('pet dog')
      ORDER BY relevance DESC
    )
    GROUP BY title;
    

答案 1 :(得分:0)

您需要按标题分组并总结相关性分数。以下查询解决了您的问题:

SELECT title, SUM(MATCH (phrase) AGAINST ('pet dog' IN BOOLEAN MODE)) as relevance
FROM pages AS p
INNER JOIN keywords AS k ON p.id = k.page_id
GROUP BY title
HAVING relevance > 0
ORDER BY relevance DESC;

参见 DEMO