Skip to content

Commit

Permalink
Implement facet counts (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianweet authored Jul 22, 2022
1 parent 813a4de commit 6fc3a6b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
69 changes: 67 additions & 2 deletions src/Typesense/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,75 @@ public Hit(IReadOnlyList<Highlight> highlights, T document, long textMatch)
}
}

public record FacetCount
{
[JsonPropertyName("counts")]
public IReadOnlyList<FacetCountHit> Counts { get; init; }

[JsonPropertyName("field_name")]
public string FieldName { get; init; }

[JsonPropertyName("stats")]
public FacetStats Stats { get; init; }

public FacetCount(string fieldName, IReadOnlyList<FacetCountHit> counts, FacetStats stats)
{
FieldName = fieldName;
Counts = counts;
Stats = stats;
}
}

public record FacetCountHit
{
[JsonPropertyName("count")]
public int Count { get; init; }

[JsonPropertyName("highlighted")]
public string Highlighted { get; init; }

[JsonPropertyName("value")]
public string Value { get; init; }

public FacetCountHit(string value, int count, string highlighted)
{
Value = value;
Count = count;
Highlighted = highlighted;
}
}

public record FacetStats
{
[JsonPropertyName("avg")]
public int Average { get; init; }

[JsonPropertyName("max")]
public int Max { get; init; }

[JsonPropertyName("min")]
public int Min { get; init; }

[JsonPropertyName("sum")]
public int Sum { get; init; }

[JsonPropertyName("total_values")]
public int TotalValues { get; init; }

public FacetStats(int average, int max, int min, int sum, int totalValues)
{
Average = average;
Max = max;
Min = min;
Sum = sum;
TotalValues = totalValues;
}
}

public record SearchResult<T>
{
[JsonPropertyName("facet_counts")]
public IReadOnlyCollection<int> FacetCounts { get; init; }
public IReadOnlyCollection<FacetCount> FacetCounts { get; init; }
[JsonPropertyName("found")]
public int Found { get; init; }
[JsonPropertyName("out_of")]
Expand All @@ -65,7 +130,7 @@ public record SearchResult<T>

[JsonConstructor]
public SearchResult(
IReadOnlyCollection<int> facetCounts,
IReadOnlyCollection<FacetCount> facetCounts,
int found,
int outOf,
int page,
Expand Down
47 changes: 47 additions & 0 deletions test/Typesense.Tests/TypesenseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,53 @@ public async Task Search_query_by_two_fields()
}
}

[Fact, TestPriority(11)]
public async Task Search_facet_by_country()
{
var expected = new FacetCount("country", new List<FacetCountHit>
{
new FacetCountHit( "AU", 1, "AU"),
new FacetCountHit( "UK", 1, "UK"),
new FacetCountHit( "SWE", 1, "SWE"),
new FacetCountHit( "USA", 1, "USA"),
}, new FacetStats(0, 0, 0, 0, 4));

var query = new SearchParameters("", "company_name")
{
FacetBy = "country"
};

var response = await _client.Search<Company>("companies", query);

using (var scope = new AssertionScope())
{
response.FacetCounts.Should().HaveCount(1);
response.FacetCounts.First().Should().BeEquivalentTo(expected);
}
}

[Fact, TestPriority(11)]
public async Task Search_facet_by_country_with_query()
{
var expected = new FacetCount("country", new List<FacetCountHit>
{
new FacetCountHit("USA", 1, "USA"),
}, new FacetStats(0, 0, 0, 0, 1));

var query = new SearchParameters("Stark", "company_name")
{
FacetBy = "country"
};

var response = await _client.Search<Company>("companies", query);

using (var scope = new AssertionScope())
{
response.FacetCounts.Should().HaveCount(1);
response.FacetCounts.First().Should().BeEquivalentTo(expected);
}
}

[Fact, TestPriority(11)]
public async Task Multi_search_query_single()
{
Expand Down

0 comments on commit 6fc3a6b

Please sign in to comment.