gpt4 book ai didi

mysql - MySQL 中的多词搜索结果

转载 作者:行者123 更新时间:2023-11-29 21:12:20 26 4
gpt4 key购买 nike

我想知道是否可以在 MySQL 中对结果建立索引。例如,我有一个要使用的搜索词,可以是一个单词或多个单词。例如。 “房子”或“公海房子”。现在我想知道 MySQL 是否可以在查询中索引“high sea house”适合 3 次,而“house”只适合一次。

我正在考虑这样的事情:

select `fields` from `table` where
`searchfield` like 'high' or
`searchfield` like 'sea' or
`searchfield` like 'house' or

等等 - 取决于我在搜索过程中使用的数量。

现在我需要按如下方式对搜索过程进行排序:

  1. 我需要包含从搜索词到首先索引的所有单词的记录
  2. 我需要评估搜索词中的每个单词适合搜索的单词数量。

有什么想法吗?

非常感谢!

最佳答案

您可以使用以下查询:

SELECT `fields` FROM `table`, (`searchfield` LIKE '%high%') +
(`searchfield` LIKE '%sea%') + (`searchfield` LIKE '%house%') AS hits
WHERE `searchfield` LIKE '%high%' OR
`searchfield` LIKE '%sea%' OR
`searchfield` LIKE '%house%'
ORDER BY hits DESC

或者简单地说:

SELECT `fields` FROM `table`, (`searchfield` LIKE '%high%') +
(`searchfield` LIKE '%sea%') + (`searchfield` LIKE '%house%') AS hits
HAVING hits > 0
ORDER BY hits DESC

它作为 bool 评估工作,返回 1 或 0。 highseasearchfield 的一部分,hits 将为 1 + 1 + 0 = 2。然后行是按降序排序,这样您将首先获得点击次数最多的行。

现在我不知道您在 searchfield 中存储什么,但如果它包含由空格分隔的单词,您也可以用空格包围搜索词(例如 '% sea % '),因此您将无法在seahorse上找到匹配项。如果 searchfield 中的第一个和最后一个单词前后都没有空格,这会给您带来问题。

关于mysql - MySQL 中的多词搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36256791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com