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

时间:2014-12-04 12:26:21

标签: 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

不明白发生了什么。为什么第一个查询不返回任何结果,而下面的查询返回结果?

1 个答案:

答案 0 :(得分:0)

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

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)

我相信您的查询意味着:找到每个都包含所有 Mint Leaves,Corriender Leaves,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`

此查询可以翻译成:获取包含至少其中一个的成分: Mint Leaves,Corriender Leaves,Greenchili 并通过rcteID对它们进行分组。

更新:

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。