Customize Projection of Title for a UnifiedFile

Vote:
 

I am unable to implement a customized projection for the SearchTitle of a UnifiedFile.

I want to first use a Title set on the file meta data with a fallback to the file name. As an extension method, the logic would look like this:

public static string SearchTitle(this UnifiedFile file)
        {
            if (file.Summary.Dictionary.Contains("Title"))
                return file.Summary.Dictionary["Title"].ToString();
            else
                return file.Name;
        }

My first thought was to call the extension method SearchTitle so Find would get this automatically but this conflicts with an extension method already in EPiServer at EPiServer.Find.Cms.UnifiedFileExtensions.

I therefore tried to customzise the project in an initalisation module and call a method in the expression, however it seems my method never gets called:

SearchClient.Instance.Conventions.UnifiedSearchRegistry
    .ForInstanceOf<UnifiedFile>()
    .CustomizeProjection(
        x => x.ProjectTitleFrom<UnifiedFile>(
            file => GetFileTitle(file)));


public static string GetFileTitle(UnifiedFile file)
{
return "This never gets returned :( so it seems its never called";
}

  If however I just return an string in the expression, the string is returned in the search results:

SearchClient.Instance.Conventions.UnifiedSearchRegistry
    .ForInstanceOf<UnifiedFile>()
    .CustomizeProjection(
        x => x.ProjectTitleFrom<UnifiedFile>(
            file => "This string works for each result"));

Can anyone help?      

#72440
Jun 17, 2013 15:24
Vote:
 

Hi Adam,

I think you are close. When I added my own file metadata field I did like this:

                SearchClient.Instance.Conventions

                    .ForInstancesOf<UnifiedFile>()

                    .IncludeField(x => x.Summary.AuthorEmail);

Where AuthorEmail was an extension of Summary.

#72446
Jun 17, 2013 16:36
Vote:
 

Have you re-index all your files after you added the convention? You could override the extension with your own by just adding a dummy parameter to yours.

public static string SearchTitle(this UnifiedFile file, bool extended)
{
    if (file.Summary.Dictionary.Contains("Title"))
    {
        return file.Summary.Dictionary["Title"].ToString();
    }
    else
    {
        return file.Name;
    }
}

And then call your extension by passing in "true" or something.  

#72448
Jun 17, 2013 16:43
Vote:
 

Thanks guys - I've gone for the IncludeField and extension approach.

Extension:

        public static string TitleOrFileName(this UnifiedFile file)
        {
            if (file.Summary.Dictionary.Contains("Title"))
            {
                var title = file.Summary.Dictionary["Title"].ToString();

                if(!string.IsNullOrWhiteSpace(title))
                    return title;
            }
            
            return file.Name;
        }

Conventions:

            SearchClient.Instance.Conventions
                .ForInstancesOf<UnifiedFile>()
                .IncludeField(x => x.TitleOrFileName());
            
            SearchClient.Instance.Conventions.UnifiedSearchRegistry
                .ForInstanceOf<UnifiedFile>()                
                .CustomizeProjection(
                    x => x.ProjectTitleFrom<UnifiedFile>(
                        file => file.TitleOrFileName()));

    

#72449
Jun 17, 2013 16:56
Vote:
 

Just remember that you have to use those fields then when you're rendering search hits.

#72450
Jun 17, 2013 16:59
Vote:
 

Thanks Johan - in this instance, I am using a UnifiedSearchFor so I get the value through UnifiedSearchHit.SearchTitle property correctly.

However, as you say though, If I was using the other one (do we call that a typed search? i.e Client.Search<UnifiedFile>) I would need to call TitleOrFileName() as SearchTitle() would be the EpiServers extension method.

 

#72452
Jun 17, 2013 17:06
Vote:
 

The last code you posted will not give you the right title (from SearchSummary) if you call SearchTitle for files, if that was what you wanted. Your first code was the way to go, to add a projection for the the title. But you have to re-index.

#72453
Jun 17, 2013 17:16
Vote:
 

Sorry - your're right, I meant to say I am calling the UnifiedSearchHit.Title property in my code which is pulling through the correct value.

#72454
Jun 17, 2013 17:19
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.