Include structure field value in site search

I’m trying to set up a site search that includes both pages and contents of structure fields.

For instance, I have a Videos section that contains a structure field for video’s name, embed, and publish date. I would like to be able to query and display these values instead of just listing the Videos page.

Kirby only returns page results, not parts of pages. So you would have to implement something yourself, maybe a custom page method to search within these pages.

I’m reading through the documentation on custom page methods, but if it’s not too much trouble, could you provide a simple example of how that would work?

The problem with this is that the Kirby content does not contain any information about its field types. You can of course loop through all individual fields of a single page ($page->content()->fields() gives you can array of all fields) to check if the query is contained in any of the fields, but with the structure field it doesn’t help that much.

Do you have many pages with these structure fields that you would have to query?

Just two pages. I have a Videos and Products page which I use to merge in an Episodes page. I haven’t up until this point needed them to be searchable by users.

Then I would check if the search result is one of those pages and if so, loop through the structure field of this page to find the matches. Currently, I have no other idea, but maybe someone else comes up with a better way of dealing with this.