In the project I am working with there are several sites with each site having a search page that is only listing hits belonging to the site.
I am doing a UnifiedSearch. The part with listing pages belonging to the site is no problem. I am just checking the ancestors. But how do I do it with MediaData? I only want to list medias that are being used in a page belonging to the site. Here the ancestor field is only listing the content folders, not the pages (or blocks).
I have actually solved it by implementing a property listing all pages the media is referenced in. But the problem is how to reindex the medias when a page is adding or removing the media.
Any nice ideas how to perform the search?
I have been in a similar situation before but with surfacing blocks in search results, having multiple sites configured. In that scenario we configured separate folders for each website, and placed the blocks in the relevant site folder - then when it came to filtering the results by current site, we got reference to the relevant subfolder via the sites settings page, and filtered accordingly. I'm guessing you maybe want to share media between all sites however, so not exactly the same scenario.
Instead of adding a property listing the pages the media is used on, couldn't you create a new property on the media item, using a custom SelectionFactory, that provides a list of checkboxes per media item - representing your current sites you have configured. By checking X number of sites per item, then filtering by the current site in your search, that may work. That would obviously mean knowing up front where media items are used, and individually tagging each one with the site they relate to.
If you're looking for more of an automated approach, maybe you could hook into the publish event of the page, and reindex media items that are used on them. Then for each media item you can index the sites that the media item is used on by getting the media items usage:
var references = ServiceLocator.Current.GetInstance<IContentRepository>().GetReferencesToContent(contentlink, false);
Then working your way back to the sites the pages are in.
Instead of having this hooked into a publish event there is also the possibility to reindex this in a scheduled job also.
Hope this gives you some ideas!