algolia / algoliasearch-client-csharp

⚡️ A fully-featured and blazing-fast C# API client to interact with Algolia.
https://www.algolia.com/doc/api-client/getting-started/install/csharp/
MIT License
120 stars 61 forks source link

Hard to find documentation about highlighting. #851

Closed SebastianStehle closed 2 months ago

SebastianStehle commented 9 months ago

I spent hours to find documentation or a sample how to use highlighting. Eventually I figured it out myself and created the following base class:

public abstract record IndexedSearchItem
{
    [JsonProperty(PropertyName ="_snippetResult")]
    public Dictionary<string, SnippetResult>? SnippetResult { get; set; }

    [JsonProperty(PropertyName = "_highlightResult")]
    public Dictionary<string, HighlightResult>? HighlightResult { get; set; }

    public string RenderProperty(string property)
    {
        if (SnippetResult?.TryGetValue(property, out var snippetResult) == true)
        {
            return snippetResult.Value;
        }

        if (HighlightResult?.TryGetValue(property, out var highlightResult) == true)
        {
            return highlightResult.Value;
        }

        var clrProperty = GetType().GetProperties().FirstOrDefault(x => string.Equals(x.Name, property, StringComparison.OrdinalIgnoreCase));

        if (clrProperty?.CanRead == true)
        {
            return clrProperty.GetValue(this)?.ToString() ?? string.Empty;
        }

        return string.Empty;
    }

    public bool ShouldSerializeSnippetResult()
    {
        return false;
    }

    public bool ShouldSerializeHighlightResult()
    {
        return false;
    }
}

Would be great to have documentation about this...

morganleroi commented 8 months ago

Hello,

We will release in few days a new major version of all Algolia C# Clients. In this version, Highlight and Snippet are directly available.

I'll reply in this thread as soon as the client is released.

morganleroi commented 8 months ago

The Alpha version is now released on Nuget.

If you have time, we need customer feedback about this new version. It's still not GA so you may not want to use it in production.

There is no more deps on Newtonsoft, all Algolia APIs are now available (Insights, Recommend, Query Perso, ...) and all models and routes are synced.

To help you migrate, there is a Migration guide.

Happy to help in your migration process !

millotp commented 2 months ago

Hello, since version 7 you can use the Hit class to extends your object to get access to strongly typed HighlightResult and SnippetResult.