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 fields being omitted in beforeSync #6590

Closed
stoddabr opened this issue May 31, 2024 · 5 comments
Closed

search plugin fields being omitted in beforeSync #6590

stoddabr opened this issue May 31, 2024 · 5 comments

Comments

@stoddabr
Copy link
Contributor

Link to reproduction

No response

Describe the Bug

Hi all, I noticed a weird behavior with the search plugin. I was trying to make multiple fields searchable efficiently by combining them into a single field in the created search collection. However, custom fields are removed and fields inside of doc are not searchable.

I'd be happy to help write a fix (or maybe a feature to build a similar, more optimized use case into the plugin). I just want to make sure this is a proper bug and not an implementation issue first.

To Reproduce

Here's a simplified version the search parameters I was using which hopefully illustrates my goal of combining multiple fields from different collections into a single searchable string field in the search collection:

        search({
            collections: ["article", "glossary"],
            defaultPriorities: {
                article: 10,
                glossary: 30,
            },
            beforeSync: ({ originalDoc, searchDoc }) => {
                const collectionSlug = searchDoc.doc.relationTo;
                const columnsToIndexForCollections = {
                    collectionA: ["title", "subtitle", "content"],
                    glossary: ["title", "tooltip"],
                };
                if (!columnsToIndexForCollections[collectionSlug]) {
                    console.warn(
                        `Collection '${collectionSlug}' not recognized in columns to index.`
                    );
                    return searchDoc;
                }
                let search_string = originalDoc.title;
                const body = {};
                columnsToIndexForCollections[collectionSlug].forEach(
                    (column) => {
                        body[column] = originalDoc[column];
                        search_string += ` ${originalDoc[column]}`;
                    }
                );

                const newSearchDoc = {
                    ...searchDoc,
                    search_string,
                    doc: {
                        ...searchDoc.doc,
                        body,
                    },
                };
            },

With this, I noticed two issues:

  1. The additional field search_string disappears from the search documents and cannot be queried.
  2. Documents inside of doc cannot be queried. I think this is related to this: Querying second nested collection value #2150 (comment)

Workarounds include:

  • setting title:search_string
  • moving the search_string inside of body and querying on that

Payload Version

2.0.0

Adapters and Plugins

(related) plugin-search v1.1.1, (probably unrelated) bundler-webpack, db-mongodb, plugin-cloud, richtext-slate

@stoddabr stoddabr added the status: needs-triage Possible bug which hasn't been reproduced yet label May 31, 2024
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jun 10, 2024
@paulpopus
Copy link
Contributor

Before I dive deeper into this, @stoddabr can you double check the following for me please?

  1. That in your searchOverrides you add this new field you created search_string
  2. That you return newSearchDoc from your beforeSync function

@paulpopus
Copy link
Contributor

Closing this for now but happy to re-open if there's a reproduction repo offered!

@stoddabr
Copy link
Contributor Author

Hi @paulpopus , what's the best way to setup a quick reproduction repo? Is there a template floating around? I've skimmed all over the contribution and main docs but I don't see one. I'd love to help drill down into this issue

(I think I've found the problem in code but want to get some guidance before I waste time on a contribution that may not be well received)

@paulpopus
Copy link
Contributor

@stoddabr If you create a blank payload repo with npx create-payload-app, make sure the packages are all on the latest versions and add the configuration that breaks it, then we can check that more easily

Basically distil it down to the simplest setup that still causes the bug

Copy link

github-actions bot commented Sep 7, 2024

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants