Unable to project search with CategoryList

Nat
Nat
Vote:
 

I am trying to do the following: 

this will work fine if I omit the Categories property, but if its included I always get the error: 

EPiServer.Find.ProjectionException: To deserialize objects the $type property must be set.

dont really understand why this is happening, I cant be the only person wanting the categories to come out in a search result?

public SearchResults<DocumentResult> Documents(int[] ids)
  {
    var query = _searchClient.Search<Document>();
    query = query.Filter(x => x.ContentLink.ID.In(ids));

    var results = query.Select(x => new DocumentResult
    {
      Title = x.Title,
      Extension = x.SearchFileExtension(),
      FileSize = x.FileSize,
      FileDate = x.FileDate,
      Reference = x.ReferenceId,
      Url = _urlResolver.GetUrl(
        x.ContentLink,
        string.Empty,
        new UrlResolverArguments { ContextMode = ContextMode.Default, ForceAbsolute = true }),
      Categories = x.Category, //<- here
      ContentId = x.ContentLink.ID,
      ViewCount = x.ViewCount
    });

    return results.GetResult();
}
#324411
Edited, Jun 27, 2024 10:26
Vote:
 

What's behind the `Document` class, what does it inherit or derive from and what properties does it have? 

I'm specifically interested in knowing how you set up the Categories property.

#324419
Jun 27, 2024 14:24
Nat
Vote:
 

Hi Eric

sorry should have specified that, its just extendeds MediaData 

public class Document : MediaData, IHasFileSize
{
  [Required]
  [Display(GroupName = PropertyGroupNames.Content, Order = 10)]
  public virtual string Title { get; set; }

  [Display(GroupName = PropertyGroupNames.Content, Order = 20)]
  public virtual string Description { get; set; }

  [CultureSpecific]
  [UIHint(UIHint.Image)]
  [AllowedTypes(typeof(ImageFile))]
  [Display(GroupName = PropertyGroupNames.Content, Order = 30)]
  public virtual ContentReference Image { get; set; }

  [Display(GroupName = PropertyGroupNames.Content, Order = 40)]
  public virtual bool ExcludeFromSearchResults { get; set; }

  [Editable(false)]
  [Display(GroupName = PropertyGroupNames.Content, Order = 50)]
  public virtual string Extension { get; set; }

  [Editable(false)]
  [Display(GroupName = PropertyGroupNames.Content, Order = 60)]
  public virtual string FileSize { get; set; }

  [UIHint("DateOnly")]
  [Display(GroupName = PropertyGroupNames.Content, Order = 70)]
  public virtual DateTime? FileDate { get; set; }

  [Display(GroupName = PropertyGroupNames.Content, Order = 80)]
  public virtual string ReferenceId { get; set; }

  public virtual int ViewCount => this.ViewCount();
}

and the DocumentResult class is just a simple POCO with some attributes

[Serializable]
public class DocumentResult
{
  [StringLength(150)]
  public string Title { get; set; }

  [StringLength(5)]
  public string Extension { get; set; }

  [StringLength(15)]
  public string FileSize { get; set; }

  public DateTime? FileDate { get; set; }

  [StringLength(150)]
  public string Reference { get; set; }

  [StringLength(250)]
  public string Url { get; set; }

  public CategoryList Categories { get; set; }

  public int ContentId { get; set; }

  public int ViewCount { get; set; }
}
#324420
Edited, Jun 27, 2024 14:29
Vote:
 

I would assume you cannot use CategoryList that way. It inherits both IList and IReadOnly where the latter should make the instance immutable.

Instead try to set up your Categories property like this

[Serializable]
public class DocumentResult
{
  // ...
  public List<int> Categories { get; set; } // this
  // ...
}
#324646
Jun 28, 2024 9:18
* 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.