Ignore content in hidden "blocks" in frontend search results

How do I setup $site->search() in a way that it doesn’t return pages that have matches to the search query, but that match is inside a hidden block?

I have editors that use the blocks’ hide option to prep things, but those show up when ppl search for them in the frontend. Obviously this is unexpected and unwanted.

Thx!

@texnixe already replied swiftly via discord:

Kirby searches in the raw content of the fields, so you would have to exclude those results in a second step. I.e. do the search, then filter the search result.

But, I’m at a loss on how to approach that “second step”?

Haha, yes, sorry, @bvdputte I suggested that in a rush, but I don’t think the filtering is really straightforward, because you would have to check for every block type if your search term is contained and if that block is hidden ( $blocks->search() should do the job here, though), or maybe even better create a virtual field with all visible blocks and check that. Maybe search would also work with virtual fields (that idea just came to mind).

I’m sorry, but I’m still lost :sweat_smile:. How would I do that?

Would you consider this issue something to be solved by the developer?
I’ld assume $site->search() returning hidden content to be a bug tbh?

Well, the current search implementation isn’t really a good fit for fields where parts are hidden. It was created at a time when the layout/blocks field didn’t exist yet.

So from the perspective of the search doing what it is supposed to do, it works as expected. It does a search in $page->content()->toArray().

However, this has certain limitations, not only the issue you are encountering now, but also - since it is page based - if you are using pages as containers, where the content would live in parent pages and some more.

The search component itself can be replaced with your own implementation, of course, where you could modify what is included in the search and what is not.

An alternative would be to use a 3rd party tool like Algolia, which we are using on the getkirby.com website.

So I wouldn’t consider this a bug, but rather missing functionality.

I quickly tested the idea I had with the virtual field, but that didn’t work, it seems.