MySQL加入和全文搜索

时间:2011-09-08 16:52:10

标签: mysql database indexing full-text-search

那里有任何全文的MySQL专家吗?

昨天我写了一个问题,询问有关如何使用大量联接的全文搜索的任何建议,有点懒,我实际上没有尝试过。

从那以后,我自己很好地掌握了它,在我的全文字段中构建了一些成功的测试查询。如果我不使用任何连接,并单独查询每个字段,全文函数完美地工作,相关性/布尔值也很好,性能也很好......但是......只要我添加连接以运行完整查询,它永远运行。

任何人都可以在我的查询中发现任何可能导致这种情况发生的事情,因为作为业余爱好者,我真的看不到它!

SELECT

photos.photoID,
photos.headline,
photos.dateCreated,
MATCH (people.people) AGAINST ('+sarah +harding' IN BOOLEAN MODE) AS Rel1

FROM photos

LEFT JOIN ( photoPeople INNER JOIN people ON photoPeople.peopleID = people.PeopleID ) 
ON photos.photoID = photoPeople.photoID AND MATCH (people.people) AGAINST ('+sarah +harding' IN BOOLEAN MODE)

WHERE

photos.photoStatus = 'Live'

GROUP BY

photos.photoID

ORDER BY Rel1

这是我成功的个人查询之一:

SELECT

photoID,
headline,
dateCreated,
MATCH (caption) AGAINST ('+sarah +harding' IN BOOLEAN MODE) AS Relevance

FROM photos

WHERE

photoStatus = 'Live'

AND

MATCH (caption) AGAINST ('+sarah +harding' IN BOOLEAN MODE)

ORDER BY Relevance

这是数据库架构:

photos (tbl)
photoID             INT(11)         Primary     Auto-Increment
headline            Long-Text
caption             Long-Text / FULLTEXT
dateCreated         DateTime

people (tbl)
peopleID            INT(11)         Primary     Auto-Increment
people              VarChar(255) / FULLTEXT

photoPeople (tbl)
photoID             INT(11)
peopleID            INT(11)

keywords (tbl)
keywordID           INT(11)         Primary     Auto-Increment
keyword             VarChar(255) / FULLTEXT

photoKeyword (tbl)
photoID             INT(11)
keywordID           INT(11)

photoContributor (tbl)
photoID             INT(11)
contributorRef      VarChar(100) / FULLTEXT

这是我的EXPLAIN打印出来的:

id  select_type         table               type        possible_keys       key     key_len     ref     rows
1   SIMPLE              photos              ALL         NULL                NULL        NULL        NULL        89830
1   SIMPLE              photoContributor    ALL         NULL                NULL        NULL        NULL        149635  
1   SIMPLE              photoPeople         ALL         NULL                NULL        NULL        NULL        110606
1   SIMPLE              people              eq_ref      PRIMARY             PRIMARY     4       1   
1   SIMPLE              photoKeyword        ALL         NULL                NULL        NULL        NULL        699102  
1   SIMPLE              keywords            eq_ref      PRIMARY             PRIMARY     4       1

我的网站访问者应该能够搜索: “Brad Pitt Angelina Jolie Dorchester Hotel太阳镜@MG” - 这应该从'people.people'表中找到“Brad Pitt”和“Angelina Jolie”,也可能找到'photos.caption'表。它还应该从'photos.caption'表中找到“Dorchester Hotel”,从'keywords.keyword'表中找到“Sunglasses”,最后,它应该在'photoContributor.contributorRef'表中找到“@MG”。

任何对此的帮助都会感激不尽......

2 个答案:

答案 0 :(得分:3)

我建议使用单独的较小查询并使用UNION创建组合结果集。

这是另一个类似任务的问题。 Mysql Full text search across more than one table

<强>更新

SELECT "Photos" AS TableName, photoID AS ID, MATCH (caption) AGAINST ('+sarah +harding' IN BOOLEAN MODE) AS Relevance
FROM photos
UNION ALL
SELECT "People" AS TableName, peopleID AS ID, MATCH (people) AGAINST ('+sarah +harding' IN BOOLEAN MODE)
FROM people
UNION ALL 
SELECT "Keyword" AS TableName, keywordID AS ID, MATCH (keyword) AGAINST ('+sarah +harding' IN BOOLEAN MODE)
FROM keyword

我真的不知道你有什么数据,但它会产生类似的东西:

表名 | ID |的关联

照片| 1 | 1

照片| 2 | 0

人物| 1 | 1

关键字| 10 | 1

您可以添加其他where子句以满足您的需求,但这是基本想法。

答案 1 :(得分:0)

我建议不使用MySQL并使用真正的FTS解决方案,例如Lucene,它通常与那些想要在MySQL中使用FTS的人结合使用。 https://stackoverflow.com/questions/553055/best-full-text-search-for-mysql