I cobbled together a user script that allows filtering the gallery by text search, style, age restriction and more. The usefulness is limited (see below), but hopefully it can serve as a reference for a future native implementation. It also adds a filter button overlay to individual image previews, which show you other images from the same other.
Limitations:
- The filter panel is stuck to the top of the gallery, because the gallery iframe doesn't know the main page's scroll state.
- Only filters the currently loaded images. after using the "load more" button, the filters have to be cleared and reapplied to the new set.
You can access the code under https://www.protectedtext.com/perchance-gallery-filter with password "perchance".
Regarding "Implement search/filter in galleries", I added a post about my userscript for a gallery filter here: https://lemmy.world/post/32573141