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
23.77k stars 1.53k forks source link

plugin-search: Search results won't filter by Search_doc_Relation_RelationTo #5338

Closed slavanossar closed 4 months ago

slavanossar commented 6 months ago

Link to reproduction

No response

Describe the Bug

I am writing a query to retrieve search results that belong to a specific collection

query GetSearchResults(
  $searchTerm: String!
  $collectionSlug: Search_doc_Relation_RelationTo!
  $limit: Int
  $page: Int
) {
  Searches(
    where: {
      AND: [
        { title: { contains: $searchTerm } }
        { doc: { relationTo: $collectionSlug } }
      ]
    }
    limit: $limit
    page: $page
  ) {
    docs {
      id
      title
    }
  }
}

When executing the query for a specified $collectionSlug, the results include documents that are from all collections

image

When a value that doesn't exist on Search_doc_Relation_RelationTo is used, the response correctly states that the value isn't part of the enum.

image

To Reproduce

  1. Create two collections
  2. Enable search plugin for both collections
  3. Create some docs
  4. Perform a GraphQL query as described above

Payload Version

2.11.2

Adapters and Plugins

@payloadcms/plugin-search@1.1.0

wkentdag commented 6 months ago

+1 experiencing the same issue.

  "dependencies": {
    "@azure/storage-blob": "^12.17.0",
    "@nouance/payload-better-fields-plugin": "^1.3.5",
    "@payloadcms/bundler-webpack": "^1.0.6",
    "@payloadcms/db-postgres": "^0.x",
    "@payloadcms/plugin-cloud-storage": "^1.1.2",
    "@payloadcms/plugin-search": "^1.1.0",
    "@payloadcms/richtext-lexical": "^0.7.0",
    "cross-env": "^7.0.3",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "payload": "^2.11.2"
  },
Screen Shot 2024-04-05 at 5 58 23 PM
slavanossar commented 4 months ago

I did some investigating and it seems like the issue actually originates within the db-mongodb adaptor, although haven't been able to pinpoint where.

slavanossar commented 4 months ago

Looking at the code in db-mongodb it's clear that relationTo isn't a supported operation and so is completely ignored by parseParams, buildSearchParam, etc., due to not being a included in validOperators or in the operatorMap.

It seems like the issue actually stems from the generation of the collection's schema, since relationTo should actually contain an operator when used within a where query.

The Search_where inputs are defined as

input Search_where {
  title: Search_title_operator
  priority: Search_priority_operator
  doc: Search_doc_Relation
  updatedAt: Search_updatedAt_operator
  createdAt: Search_createdAt_operator
  id: Search_id_operator
  AND: [Search_where_and]
  OR: [Search_where_or]
}

input Search_doc_Relation {
  relationTo: Search_doc_Relation_RelationTo
  value: JSON
}

enum Search_doc_Relation_RelationTo {
  articles
  artists
  episodes
  hosts
  programs
  trackPlays
}

where in reality it should be something like

input Search_where {
  title: Search_title_operator
  priority: Search_priority_operator
  doc: Search_doc_Relation
  updatedAt: Search_updatedAt_operator
  createdAt: Search_createdAt_operator
  id: Search_id_operator
  AND: [Search_where_and]
  OR: [Search_where_or]
}

input Search_doc_Relation {
  relationTo: Search_doc_Relation_RelationTo_operator
  value: JSON
}

input Search_doc_Relation_RelationTo_operator {
  equals: Search_doc_Relation_RelationTo
  not_equals: Search_doc_Relation_RelationTo
  like: Search_doc_Relation_RelationTo
  contains: Search_doc_Relation_RelationTo
  in: [Search_doc_Relation_RelationTo]
  not_in: [Search_doc_Relation_RelationTo]
  all: [Search_doc_Relation_RelationTo]
  exists: Boolean
}

enum Search_doc_Relation_RelationTo {
  articles
  artists
  episodes
  hosts
  programs
  trackPlays
}
slavanossar commented 4 months ago

Closed in favour of #6399

github-actions[bot] commented 1 month ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.