如何在不丢失 FULLTEXT-Index 的情况下连接表?
How to join tables without losing the FULLTEXT-Index?
我目前正在尝试为我的网站编写一个小型多语言搜索引擎。
我编写了一个函数来获取所请求语言的文本,如果该语言的文本不存在,则返回到英语。
但是由于在 MATCH 子句中调用函数时出错,我不想调用它 3 次,所以我只是将函数代码写为 INNER JOIN,但现在我失去了我的全文索引。
问题:
- 如何维护我的全文索引?
- 有没有比加入第二个更好的方法table?
- 还有其他错误吗?
错误:
Can't find fulltext index matching the column list
TABLE "translations":
- 类型------名称----索引
- INT--------"ID"-------- 唯一(主要)
- INT----------"tr_num"-
- TEXT"-----"text"----- FULLTEXT
- CHAR(2)-"lang"-----
搜索引擎:(缩短)
SELECT
c.ID,
c.name,
tr.description,
MATCH(c.name) AGAINST(?) AS relevance_title,
MATCH(tr.description) AGAINST(?) AS relevance_description,
FROM
companies c
#Joining transations (function code)
INNER JOIN (
SELECT
tr1.tr_num,
#Select translation if available
COALESCE(tr2.text, tr1.text) AS description
FROM
translations tr1
LEFT JOIN
translations tr2
ON
tr1.tr_num = tr2.tr_num
AND
tr2.lang = '$lang' #PHP - preferred langauge
WHERE
tr1.lang = 'en' #default language (english)
) tr
ON
tr.tr_num = c.description_tr_num
WHERE
MATCH(c.name) AGAINST(?)
OR MATCH(tr.description) AGAINST(?)
ORDER BY
((relevance_title * "/*PHP*/.$config['relevance_title'].") + (relevance_description * "/*PHP*/.$config['relevance_description'].")) DESC
函数:(不重要 - 以防万一我可以在搜索中使用它)
BEGIN
DECLARE output TEXT;
SELECT
COALESCE(tr2.text, tr1.text)
INTO
output
FROM
translations tr1
LEFT JOIN
translations tr2
ON
tr1.tr_num = tr2.tr_num
AND
tr2.lang = input_lang
WHERE
tr1.tr_num = input_tr_num
AND
tr1.lang = 'en';
RETURN output;
END
提前致谢!
-注意
这是我解决问题的方法,以防万一有人遇到类似问题:
采取另一种方法,而不是先加入,然后搜索我选择先搜索,然后加入:
#INSTEAD OF
COALESCE(tr2.text, tr1.text) AS description
#AND
MATCH(description) AGAINST(?) AS relevance_description
#USE
GREATEST(
MATCH(tr2.text) AGAINST(?),
MATCH(tr1.text) AGAINST(?)
) AS relevance_description
我目前正在尝试为我的网站编写一个小型多语言搜索引擎。
我编写了一个函数来获取所请求语言的文本,如果该语言的文本不存在,则返回到英语。
但是由于在 MATCH 子句中调用函数时出错,我不想调用它 3 次,所以我只是将函数代码写为 INNER JOIN,但现在我失去了我的全文索引。
问题:
- 如何维护我的全文索引?
- 有没有比加入第二个更好的方法table?
- 还有其他错误吗?
错误:
Can't find fulltext index matching the column list
TABLE "translations":
- 类型------名称----索引
- INT--------"ID"-------- 唯一(主要)
- INT----------"tr_num"-
- TEXT"-----"text"----- FULLTEXT
- CHAR(2)-"lang"-----
搜索引擎:(缩短)
SELECT
c.ID,
c.name,
tr.description,
MATCH(c.name) AGAINST(?) AS relevance_title,
MATCH(tr.description) AGAINST(?) AS relevance_description,
FROM
companies c
#Joining transations (function code)
INNER JOIN (
SELECT
tr1.tr_num,
#Select translation if available
COALESCE(tr2.text, tr1.text) AS description
FROM
translations tr1
LEFT JOIN
translations tr2
ON
tr1.tr_num = tr2.tr_num
AND
tr2.lang = '$lang' #PHP - preferred langauge
WHERE
tr1.lang = 'en' #default language (english)
) tr
ON
tr.tr_num = c.description_tr_num
WHERE
MATCH(c.name) AGAINST(?)
OR MATCH(tr.description) AGAINST(?)
ORDER BY
((relevance_title * "/*PHP*/.$config['relevance_title'].") + (relevance_description * "/*PHP*/.$config['relevance_description'].")) DESC
函数:(不重要 - 以防万一我可以在搜索中使用它)
BEGIN
DECLARE output TEXT;
SELECT
COALESCE(tr2.text, tr1.text)
INTO
output
FROM
translations tr1
LEFT JOIN
translations tr2
ON
tr1.tr_num = tr2.tr_num
AND
tr2.lang = input_lang
WHERE
tr1.tr_num = input_tr_num
AND
tr1.lang = 'en';
RETURN output;
END
提前致谢! -注意
这是我解决问题的方法,以防万一有人遇到类似问题:
采取另一种方法,而不是先加入,然后搜索我选择先搜索,然后加入:
#INSTEAD OF
COALESCE(tr2.text, tr1.text) AS description
#AND
MATCH(description) AGAINST(?) AS relevance_description
#USE
GREATEST(
MATCH(tr2.text) AGAINST(?),
MATCH(tr1.text) AGAINST(?)
) AS relevance_description