Unified Search - Return custom field

Vote:
 

Hi,

I am trying to show a custom field in a unified search results.  I can successfully add custom fields to the index based on extension methods:

SearchClient.Instance.Conventions.ForInstancesOf().IncludeField(x => x.MyGenericExtensionMethod())

I can filter on the custom field:

mySearchClient = mySearchClient.Filter(x => x.MyGenericExtensionMethod().Match("text to match"));

... but what I can't work out how to do, is return the result of the extension method into my results.  NB: I've run out of ISearchContent fields to override / project into (primarily because we have different types of searches on the site, which require different implementations of the same field to be displayed in the results, e.g. different excerpt/summary text).  I don't really understand the CustomizeProjection method which I suspect may be the answer?  

I guess worst case I can use specific elements in the SearchAuthors field for different purposes, but it seems a horrible hack.

NB: I found Per's comments in this article useful to get so far

http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=79729

#119311
Mar 26, 2015 15:47
Vote:
 

You could retrieve the the original object when listing results.

If you do GetContentResult it's already in the cache.

If it's a page you could can do something like this:

public static PageReference PageLink(this UnifiedSearchHit hit)
{
    if (typeof(PageData).IsAssignableFrom(hit.OriginalObjectType))
    {
        PageReference pageReference = PageReference.ParseUrl(hit.Url);

        if (!PageReference.IsNullOrEmpty(pageReference))
        {
            return pageReference;
        }
                
        object internalObject;

        if (Global.UrlRewriteProvider.ConvertToInternal(new UrlBuilder(hit.Url), out internalObject))
        {
            pageReference = internalObject as PageReference;

            if (!PageReference.IsNullOrEmpty(pageReference))
            {
                return pageReference;
            }
        }
    }

    return PageReference.EmptyReference;
}
#119438
Mar 27, 2015 15:49
Vote:
 

Thanks Johan, that's useful especially the cache advice.

#119450
Mar 27, 2015 18:09
Vote:
 

Or just call the OriginalObjectGetter:

public static T GetOriginalObject<T>(this UnifiedSearchHit hit)
{
    if (hit.OriginalObjectGetter != null)
    {
        var original = hit.OriginalObjectGetter.Invoke();

        if (original is T)
        {
            return (T)original;
        }
    }

    return default(T);
}

Then you can:

var content = hit.GetOriginalObject<YourPageDataPage>();
#119452
Mar 27, 2015 21:14
Vote:
 

Thank you very much! 

#175003
Feb 09, 2017 11:59
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.