Unified Search - Filter file used on child pages

Vote:
 

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?

#79729
Jan 02, 2014 15:11
Vote:
 

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))

    

#79748
Jan 03, 2014 10:07
Vote:
 

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?

#79763
Jan 03, 2014 17:15
Vote:
 

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.

#79773
Jan 06, 2014 8:13
Vote:
 

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.

#79775
Jan 06, 2014 12:07
Vote:
 

Great :-)

#79778
Jan 06, 2014 14:18
Vote:
 
#114025
Dec 03, 2014 8:52
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.