You could do something like this:
First, create an extension method to fetch the desired information:
public static IEnumerable<int> ReferencedOnChildOfPages<T>(this T content)
{
var file = content as IContent;
if (file != null)
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var references = contentRepository.GetReferencesToContent(file.ContentLink, false);
foreach (var referenceInformation in references)
{
PageData page = null;
contentRepository.TryGet(referenceInformation.OwnerID, out page);
if (page != null)
yield return page.ParentLink.ID;
}
}
}
Then, include the data in the index in an initializable module:
SearchClient.Instance.Conventions.ForInstancesOf<GenericMedia>().IncludeField(x => x.ReferencedOnChildOfPages());
Reindex, and you should then be able to filter your query like this:
.Filter(x => x.ReferencedOnChildOfPages().Match(somePage.ContentLink.ID))
Thanks man. That worked like a charm.
But if ReferenceChildOfPages is stored on the file in the index, do I need to reindex everytime I reference a file on a page for it to be searchable? I know the index of the page it self is updated when I hit Publish, but that won't update the index of the file will it?
Yeah, you will need to reindex. You could listen to the PublishedContent event, and then check the page for file references. If it does, reindex those references files.
Perfect!
In addition I had to get references to files from previous versions of the page and reindex those to handle when a file is removed from the linkitemcollection.
I'm trying to add a filter for files of type GenericMedia to my unified search that matches the following criterias:
- The file is added to a page in a LinkItemCollection (or a contentarea if that is easier)
- The page the file is added to is child of another specified page (I'm passing the pagedata object of the parent as an argument to the search method)
I'm drawing a blank as how to implement this. Anybody else have an idea?