Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

How do you use UnifiedSearch's ProjectImageUriFrom

Vote:
 

Hi,

 

I tried using this extension method to add a image thumbnail to my search results  as most of my pages have some sort of image, I used what 'http://find.episerver.com/Documentation/unified-search' suggested, and tried first this (in a init module depending on "IndexingModule"):

SearchClient.Instance.Conventions.UnifiedSearchRegistry.ForInstanceOf<ArticlePage>().ProjectImageUriFrom(a => a.ImageUri);

I also tried this:  

SearchClient.Instance.Conventions.UnifiedSearchRegistry.ForInstanceOf<ArticlePage>()
			            .CustomizeProjection(p => p.ProjectImageUriFrom<ArticlePage>(a => a.ImageUri));

I then reindexed and made a search like so:

EPiServer.Find.Framework.SearchClient.Instance.UnifiedSearchFor(query.Term, Language.Swedish).GetResult();

They both throw the error: "EPiServer.Find.ProjectionException: To deserialize objects the $type property must be set."

Could someone point me in the right direction as of how exactly I am supposed to use these projection extensions?

#75805
Oct 07, 2013 14:30
Vote:
 

After some experimentation I notice that all other predefined projections (ex. title) works fine, it is only ImageUri that I cant get to work.

#75819
Oct 07, 2013 16:54
Vote:
 

Hi

Try to use the OriginalString property:

SearchClient.Instance.Conventions.UnifiedSearchRegistry.ForInstanceOf<ArticlePage>()
  .CustomizeProjection(p => p.ProjectImageUriFrom<ArticlePage>(a => a.ImageUri.OriginalString));

    

#75825
Edited, Oct 08, 2013 9:24
Vote:
 

That wouldnt even compile, the type must be a Uri, which lead me to believe there is some sort of json serialization error going on, but I have no idea what I can do to resolve it.

#75835
Edited, Oct 08, 2013 14:01
Vote:
 

Ok, sorry, I'm using the EPiServer.Url as property type.

The implementation below works for me.

 SearchClient.Instance.Conventions.UnifiedSearchRegistry
                .ForInstanceOf<SitePageData>()
                .CustomizeProjection(
                    x => x.ProjectImageUriFrom<SitePageData>(
                        page => GetPageImage(page.PageImage.OriginalString)));


 private static Uri GetPageImage(string url)
        {
            if(string.IsNullOrWhiteSpace(url))
            {
                return null;
            }

            return new Uri(url, UriKind.Relative);
        }

    

#75854
Edited, Oct 09, 2013 9:18
Vote:
 

Taht stopped me from getting exceptions atleast. But if I check the index, the 'SearchImageUri' property (or any like that) is not present anywhere for pages that do have a valid image set.

#75898
Oct 10, 2013 11:34
Vote:
 

Hi guys,

I had the exact same Exception as Erik but I solved it in another way. My problem was that I couldn't project a thumbnail from an ImageVault image. I figured out that since the projection is executed on the searchresult the returned PageData is just a deserialized JSON-object, not a true PageData-object. And on that premiss I wrote this little crowbar implementation.

SearchClient.Instance.Conventions.UnifiedSearchRegistry
   .ForInstanceOf<PageData>()
   .CustomizeProjection(
      x => x.ProjectImageUriFrom<PageData>(
         page => GetThumbnail(page.ContentGuid)));

    

private static Uri GetThumbnail(Guid id)
{
   if (id != null)
   {
      PageData p = Utilities.GetPageFromGuid(id);
      if (p != null && (p[Web.Global.ImageProperties.List] != null ||
         p[Web.Global.ImageProperties.Media] != null))
      {
         Thumbnail media = SiteDataFactory.Instance.GetThumbnail(p, 200, 140);
         if (media != null)
            return new Uri(media.Url, UriKind.RelativeOrAbsolute);
      }
   }
   return null;
}

#76455
Oct 24, 2013 17:49
Vote:
 

Provided code examples don't work for me and ImageUri is always null if I use custom methods in ProjectImageUriFrom expression. The fix is to directly call "new Uri()" in the expression. This code worked for me:

client.ForInstancesOf<PageData>()
	.IncludeField(x => x.SearchImageUri());

client.UnifiedSearchRegistry.ForInstanceOf<PageData>()
	.ProjectImageUriFrom(x => x.SearchImageUri() != null ? new Uri(x.SearchImageUri()) : null);

SearchImageUri is my extension method to get string url for page image and store it in Find index. It is fine to call it several times in ProjectImageUriFrom expression, because it's not really executed there, but used as a getter expression for "SearchImageUri" property of a search result.

#148675
Edited, May 19, 2016 19:31
* 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.