在Sequelize 4中有没有办法进行MySQL FullText搜索?

时间:2017-12-10 18:41:20

标签: mysql node.js sequelize.js

我正在尝试使用Sequelize 4:

生成如下所示的查询
SELECT * FROM authors WHERE MATCH (name, altName) AGAINST ('shakespeare');

我曾经能够这样做:

Author.find({
           where: ['MATCH (name, altName) AGAINST (?)',
                  ['shakespeare']]
           })
          .then((result) => {
              console.log(result);
          });

但是现在,升级到Sequelize 4之后,我收到了这个错误:

Error: Support for literal replacements in the `where` object has been removed.

1 个答案:

答案 0 :(得分:2)

在Sequelize 4.0+中你可以这样做:

Author.find({
    where: Sequelize.literal(`MATCH (name, altName) AGAINST('shakespeare' IN NATURAL LANGUAGE MODE)`),
  }).then(…);

如果您需要按相关性排序记录列表,请使用以下示例:

Author.findAll({
    attributes: { include:[[Sequelize.literal(`MATCH (name, altName) AGAINST('shakespeare' IN NATURAL LANGUAGE MODE)`), 'score']] },
    where: Sequelize.literal(`MATCH (name, altName) AGAINST('shakespeare' IN NATURAL LANGUAGE MODE)`),
    order:[[Sequelize.literal('score'), 'DESC']],
  });

我希望这有帮助。