Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search plugin totalDocs returns incorrect value #9436

Open
TimLanzi opened this issue Nov 22, 2024 · 0 comments
Open

Search plugin totalDocs returns incorrect value #9436

TimLanzi opened this issue Nov 22, 2024 · 0 comments
Labels
plugin: search @payloadcms/plugin-search status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@TimLanzi
Copy link

TimLanzi commented Nov 22, 2024

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 },
              + },
            ],
          },
        }
      : {}),
  })
  1. 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>
  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
@TimLanzi TimLanzi added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Nov 22, 2024
@jacobsfletch jacobsfletch added the plugin: search @payloadcms/plugin-search label Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: search @payloadcms/plugin-search status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

2 participants