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.
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.
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()));
Just remember that you have to use those fields then when you're rendering search hits.
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.
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.
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.
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:
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:
If however I just return an string in the expression, the string is returned in the search results:
Can anyone help?