payload
payload copied to clipboard
Search plugin totalDocs returns incorrect value
Describe the Bug
When using the search plugin filtering by more than one field, results can return a totalDocs value higher than reality. Sometimes even higher than docs that exist.
Link to the code that reproduces this issue
https://github.com/TimLanzi/payload-search-bug
Reproduction Steps
- Create a new Payload project with the website template
- Create your account and seed your database
- Create the category 'Dive' (I tried to pick a word that would show up in at least 2 of the posts) and add it to the post "Dollar and Sense"
- Edit the file
/src/app/(frontend)/search/page.tsx
to add a filter oncategory.title
:
const posts = await payload.find({
collection: 'search',
depth: 1,
limit: 12,
...(query
? {
where: {
or: [
{
title: {
like: query,
},
},
{
'meta.description': {
like: query,
},
},
{
'meta.title': {
like: query,
},
},
{
slug: {
like: query,
},
},
+ {
+ 'categories.title': { like: query },
+ },
],
},
}
: {}),
})
- Add following JSX to the same file to view totalDocs on the page:
<div className="container mb-16">
<div className="prose dark:prose-invert max-w-none">
<h1 className="sr-only">Search</h1>
<Search />
</div>
+ <div>{posts.totalDocs}</div>
</div>
- Go to search page on the local website and search 'dive'. You should see 2 results in the archive block but the totalDocs should read 3.
Which area(s) are affected? (Select all that apply)
plugin: search
Environment Info
Binaries:
Node: 22.7.0
npm: 10.8.2
Yarn: 1.22.22
pnpm: 9.8.0
Relevant Packages:
payload: 3.0.2
next: 15.0.3
@payloadcms/db-postgres: 3.0.2
@payloadcms/email-nodemailer: 3.0.2
@payloadcms/graphql: 3.0.2
@payloadcms/live-preview: 3.0.2
@payloadcms/live-preview-react: 3.0.2
@payloadcms/next/utilities: 3.0.2
@payloadcms/payload-cloud: 3.0.2
@payloadcms/plugin-form-builder: 3.0.2
@payloadcms/plugin-nested-docs: 3.0.2
@payloadcms/plugin-redirects: 3.0.2
@payloadcms/plugin-search: 3.0.2
@payloadcms/plugin-seo: 3.0.2
@payloadcms/richtext-lexical: 3.0.2
@payloadcms/translations: 3.0.2
@payloadcms/ui/shared: 3.0.2
react: 19.0.0-rc-65a56d0e-20241020
react-dom: 19.0.0-rc-65a56d0e-20241020
Operating System:
Platform: linux
Arch: x64
Version: #202405300957~1732141768~22.04~f2697e1 SMP PREEMPT_DYNAMIC Wed N
Available memory (MB): 31971
Available CPU cores: 8