Mysql全文索引搜索返回奇怪的结果

标签 mysql sql full-text-search

我有配料表。我想要所有含有特定成分的食谱。下面是我的表结构。

Table(ingredient) - Applied fulltext index on ingredient column.
------------------------------------------------------
 ingredientID   rcteID  ingredient  
    310           1     Mint Leaves     
    311           1     Corriender Leaves   
    312           1     GreenChili

我正在尝试获取全文搜索查询下方的上方记录,但未获取该记录。

SELECT `Ingredient`.`ingredientID` , `Ingredient`.`rcteID`
FROM `ingredient` AS `Ingredient`
WHERE MATCH (`Ingredient`.`ingredient`) 
      AGAINST ('+Mint Leaves +Corriender Leaves +Greenchili' IN BOOLEAN MODE)
AND `Ingredient`.`rcteID`
IN ( 1 )
GROUP BY `Ingredient`.`rcteID`

为什么上面的查询对上面的记录不起作用?

当我尝试下面的查询时,它起作用了。只是更改了搜索文本。

SELECT `Ingredient`.`ingredientID` , `Ingredient`.`rcteID`
FROM `ingredient` AS `Ingredient`
WHERE MATCH (`Ingredient`.`ingredient`) 
      AGAINST ('+Greenchili +Mint Leaves +Corriender Leaves' IN BOOLEAN MODE)
AND `Ingredient`.`rcteID`
IN ( 1 )
GROUP BY `Ingredient`.`rcteID`

OUTPUT
--------------------
ingredientID    rcteID
311               1

不明白这是怎么回事。为什么先查询不返回结果,后查询返回结果?

最佳答案

这不是真正的解释,但您可以运行此查询以查看分数。

SELECT MATCH (`Ingredient`.`ingredient`) 
      AGAINST ('+Mint Leaves +Corriender Leaves +Greenchili' IN BOOLEAN MODE)
FROM `ingredient` AS `Ingredient`
WHERE MATCH (`Ingredient`.`ingredient`) 
      AGAINST ('+Mint Leaves +Corriender Leaves +Greenchili' IN BOOLEAN MODE)

我认为您的查询的意思是:找到其中每一种都包含所有薄荷叶、Corriender 叶、Greenchili 并且在您的数据中找不到的成分放。 MySQL 无法找到包含以上所有这些关键字的任何行。

但是,如果您将查询放在方括号中,那就是另外一回事了:

SELECT `Ingredient`.`ingredientID` , `Ingredient`.`rcteID`
FROM `ingredient` AS `Ingredient`
WHERE MATCH (`Ingredient`.`ingredient`) 
      AGAINST ('(+Greenchili) (+Mint Leaves) (+Corriender Leaves)' IN BOOLEAN MODE)
AND `Ingredient`.`rcteID`
IN ( 1 )
GROUP BY `Ingredient`.`rcteID`

此查询可以翻译成:获取包含至少其中之一的成分:薄荷叶、Corriender 叶、Greenchili,并按 rcteID 对它们进行分组。 p>

更新:

SELECT t1.rcteID FROM `ingredient` t1
    JOIN `ingredient` t2 ON t2.rcteID = t1.rcteID
    JOIN `ingredient` t3 ON t3.rcteID = t2.rcteID
WHERE 
    MATCH (t1.`ingredient`) AGAINST ('+Greenchili' IN BOOLEAN MODE)
AND
    MATCH (t2.`ingredient`) AGAINST ('+Mint Leaves' IN BOOLEAN MODE)
AND 
    MATCH (t3.`ingredient`) AGAINST ('+Corriender Leaves' IN BOOLEAN MODE)
AND t1.`rcteID` IN ( 1 )
GROUP BY t1.`rcteID`

我认为这个查询对您有用。基本上,它与您的想法相同,但它分别查找 3 个关键字,并且只得到包含 3 个成分的 rcteID。

关于Mysql全文索引搜索返回奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27294066/

相关文章:

sql - (递归)SQL 查询而不是循环

python - 如何在django的sqlite3数据库中使用全文搜索?

C# - 如何安全地存储 MySQL 连接字符串以便没有人可以看到它?

sql - 查找表中的最小和最大数据列

mysql - 如何写Nhibernate Query C# .net Mysql

Django - 搜索匹配所有对象 - 即使它们实际上不匹配

mysql - 如何创建包含匹配记录的良好搜索

mysql - mysql中按年、月分组时出错

php - 如果字段为空或 !empty 运行查询并且不显示空字段的错误

php - 无法在文本区域中显示带有换行符的文本,如何保留它们?