我想在我的MySQL表上使用全文搜索,但除此之外,我还想对列进行某种“权重顺序”,其中执行搜索。
例如:我有三个名为ChapterCol,TextCol,NoteCol的列。 当我搜索“财务”这个词时,它应该返回: 首先在ChapterCol中包含这个单词的所有行(和/或TextCol,NoteCol也是如此), 然后在TextCol(和/或NoteCol)中有它的所有行 最后只有在NoteCol中的所有行。
我希望有答案......
thanx很多
答案 0 :(得分:1)
首先,您需要使用MyISAM存储引擎,它是MySQL中唯一支持FULLTEXT索引的引擎。
创建3个单独的全文索引,每个索引一个。
在SELECT查询中,为WHERE子句和ORDER BY子句中的3列中的每一列执行MATCH ... AGAINST ...,这样您就可以获得至少有一个搜索项的所有行3列,并根据哪些列包含搜索词进行排序。
以下是一个例子:
CREATE TABLE IF NOT EXISTS your_table (
ChapterCol TEXT,
TextCol TEXT,
NoteCol TEXT,
FULLTEXT INDEX (ChapterCol),
FULLTEXT INDEX (TextCol),
FULLTEXT INDEX (NoteCol)
) Engine = MyISAM;
-- insert test values
insert into your_table (ChapterCol,TextCol,NoteCol) values ('foo','foo','foo'),
('financial blah blah','foo','foo'),('foo','financial blah blah','foo'),
('foo','foo','financial blah blah'),('financial blah blah','financial blah blah',
'financial blah blah');
-- insert filler
insert into your_table (ChapterCol,TextCol,NoteCol)
values (md5(rand()),md5(rand()),md5(rand())),(md5(rand()),md5(rand()),md5(rand())),
(md5(rand()),md5(rand()),md5(rand())),(md5(rand()),md5(rand()),md5(rand())),
(md5(rand()),md5(rand()),md5(rand())),(md5(rand()),md5(rand()),md5(rand())),
(md5(rand()),md5(rand()),md5(rand())),(md5(rand()),md5(rand()),md5(rand())),
(md5(rand()),md5(rand()),md5(rand())),(md5(rand()),md5(rand()),md5(rand())),
(md5(rand()),md5(rand()),md5(rand())),(md5(rand()),md5(rand()),md5(rand()));
SELECT ChapterCol,TextCol,NoteCol
FROM your_table
WHERE MATCH (ChapterCol) AGAINST ('financial' IN BOOLEAN MODE)
OR MATCH (TextCol) AGAINST ('financial' IN BOOLEAN MODE)
OR MATCH (NoteCol) AGAINST ('financial' IN BOOLEAN MODE)
ORDER BY MATCH (ChapterCol) AGAINST ('financial' IN BOOLEAN MODE) DESC,
MATCH (TextCol) AGAINST ('financial' IN BOOLEAN MODE) DESC,
MATCH (NoteCol) AGAINST ('financial' IN BOOLEAN MODE) DESC;