使用参数对全文查询进行续集

sequelize fulltext query using parameters

您好,我目前正在尝试从数据库中查询记录,这些是条件

所以我试过了

    let order_by = req.query.orderby;
    let order = req.query.order;
    let page = req.query.pagenum;
    let perpage = req.query.parpage;
    let searchword = req.query.foodsearch;
    let offset = (parseInt(page) - 1) * parpage;

    let foods = await models.food.findAll({
        limit: parseInt(perpage),
        offset: offset,
        order: [
            [order_by, order]
        ],

    //  where: Sequelize.literal
    //  (
    //      `MATCH 
    //      (Name, Place, RestoNum, Ingredient, ChefName, Region...) 
    //      AGAINST
    //      ( ? IN NATURAL LANGUAGE MODE)`, 
    //      { replacements: [ searchword ] }
    //  )
    });

但是这段代码中的注释部分似乎有误。 我尝试了原始查询,但后来我无法参数化那些 order by、order、offset、limit 变量。 我不想像 ${orderby} 这样添加它们,因为它有风险。

如果您对此问题有任何解决方案,请告诉我。

提前致谢!

您混淆了 Sequelize.literal()sequelizeInstance.query() API。

  1. .literal() only take a string. If you want to use the object notation for your query, your commented code will work. Except that there is no second argument. You will need to concatenate-in or interpolate-in your search term into the AGAINST clause. Also, don't forget your quotesliteral() 的输出本质上是一个字符串。您的 MySQL FTS 参数将需要正确类型的引号,就像它们出现在您的原始 SQL 查询中一样。
  2. .query() DOES take an options parameter. Through this, you don't have to use string interpolation, you can use named replacements or bound-parameters。这不仅允许您放置 searchword 参数,还可以放置您想要的任何 ORDER BY 子句。

我会选择选项 1。这就是我们在 MS SQL.

中为我们的 FTS 所做的