如何通过关联表进行模糊搜索?

现在只支持 article 表中的 title 和 introduction 字段进行模糊搜索

相关代码

Article.hasMany(models.tag, {
      foreignKey: 'article_id',
      constraints: false
    })
Tag.belongsTo(models.article, {
  foreignKey: 'article_id',
  constraints: false
})
const { rows, count } = await
ArticleModel.findAndCountAll({
      distinct: true,
      limit: pageSize,
      offset: (pageNum - 1) * pageSize,
      order: [['id', 'DESC']],
      attributes: [
        'id',
        'title',
        'author',
        'cover',
        'introduction',
        'created_at'
      ],
    where: {
        [Op.or]: [
          {
            title: {
              [Op.like]: `%${wd}%`
            }
          },
          {
            introduction: {
              [Op.like]: `%${wd}%`
            }
          }]
    },
      include: [
        {
          model: TagModel,
          required: false,
          attributes: ['name']
        },
      ]
    })

我希望支持 article 表中的 title 和 introduction 字段并且也支持 TagModel 中的name进行模糊搜索
即输入关键字,只要文章标题和描述以及文章标签中其中一个符合条件就返回该条数据
请问下改怎么解决

待解决 悬赏分:40 - 离问题结束还有 209天1分17秒
反对 0举报 0 收藏 0

我来回答

回答3