You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Prisma根据动态查询参数过滤论文数据?

如何使用Prisma根据动态查询参数过滤论文数据?

看起来你正在尝试实现一个支持多条件动态过滤论文的API,不过当前的代码还有几个关键问题需要调整,我来一步步帮你解决:

1. 先修正参数名不匹配的问题

你URL里的年份参数是yearStart和yearEnd,但代码里却用searchParams.get('year_start')来获取,这会导致拿不到参数值,先把参数名统一:

const searchTitle = searchParams.get('title')
const yearStart = searchParams.get('yearStart') // 改为和URL一致的参数名
const yearEnd = searchParams.get('yearEnd')
const department = searchParams.get('department')

2. 修正错误的OR逻辑使用

你当前的OR写法不符合Prisma的语法要求,而且从你的例子来看,你需要的是同时满足多个条件(比如标题含test + 日期在1999-2020 + 部门是CAHS),而不是满足任意一个。我们需要动态构建查询条件:只有当参数存在时,才把对应的过滤规则加入查询。

3. 处理日期类型转换

从URL获取的年份是字符串,而Prisma的DateTime字段需要Date对象,我们要把年份转换成对应的日期范围(比如1999年就转换成当年第一天到当年最后一天)。

4. 注意模型字段缺失问题

你的Publish_Papers模型里并没有department字段,但你在查询里用到了这个条件,这会直接导致Prisma报错。如果是字段名写错了(比如实际是categories字段),要把代码里的department改成categories;如果确实需要department字段,得先在Prisma Schema里添加这个字段,再执行数据库迁移。


修改后的完整代码

export async function GET(req: Request, res: NextResponse) {
    try {
        const { searchParams } = new URL(req.url)
        const searchTitle = searchParams.get('title')
        const yearStart = searchParams.get('yearStart')
        const yearEnd = searchParams.get('yearEnd')
        // 这里假设你是把categories当成部门字段,或者替换成实际存在的字段
        const department = searchParams.get('department')

        // 动态构建查询条件
        const whereConditions: any[] = []

        // 处理标题过滤:只有当title参数存在时才添加
        if (searchTitle) {
            whereConditions.push({
                title: {
                    contains: searchTitle,
                    mode: 'insensitive' // 可选:开启不区分大小写匹配
                }
            })
        }

        // 处理日期范围过滤:至少有一个年份参数存在时添加
        if (yearStart || yearEnd) {
            const dateFilter: any = {}
            if (yearStart) {
                // 转换为当年的第一天
                dateFilter.gte = new Date(`${yearStart}-01-01T00:00:00Z`)
            }
            if (yearEnd) {
                // 转换为当年的最后一天
                dateFilter.lte = new Date(`${yearEnd}-12-31T23:59:59Z`)
            }
            whereConditions.push({ publication_date: dateFilter })
        }

        // 处理部门/分类过滤:参数存在时添加
        if (department) {
            // 注意:如果模型里没有department字段,替换成categories或者其他实际字段
            whereConditions.push({ categories: department })
        }

        // 构建最终的where对象:有条件则用AND组合,无条件则返回所有数据
        const where = whereConditions.length > 0 ? { AND: whereConditions } : {}

        const papers = await db.publish_Papers.findMany({ where })

        console.log(papers)
        return NextResponse.json(papers, { status: 200 })
    } catch (error) {
        console.error('过滤论文出错:', error) // 加上错误日志方便排查
        return NextResponse.json({ error: 'failed to load data' }, { status: 500 })
    }
}

测试不同场景

  • 只传标题:http://localhost:3000/api/filter-papers?title=test → 返回标题含"test"的所有论文
  • 只传日期范围:http://localhost:3000/api/filter-papers?yearStart=1999&yearEnd=2020 → 返回发布日期在1999-2020年的所有论文
  • 组合多个条件:http://localhost:3000/api/filter-papers?title=test&yearStart=1999&yearEnd=2020&department=CAHS → 返回同时满足所有条件的论文

备注:内容来源于stack exchange,提问作者Stykgwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:14:33