payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
28.36k stars 1.75k forks source link

Search plugin totalDocs returns incorrect value #9436

Open TimLanzi opened 3 days ago

TimLanzi commented 3 days ago

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

  1. Create a new Payload project with the website template
  2. Create your account and seed your database
  3. 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"
  4. Edit the file /src/app/(frontend)/search/page.tsx to add a filter on category.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 },
              + },
            ],
          },
        }
      : {}),
    })
5. Add following JSX to the same file to view totalDocs on the page:
```jsx
      <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>
  1. 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