Skip to content

Commit

Permalink
Fix/facet stats (#95)
Browse files Browse the repository at this point in the history
* Fix facet stats property type

* Add test for numerical facet field with facet stats
  • Loading branch information
brianweet authored Jul 25, 2022
1 parent 6fc3a6b commit a1ab4ec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
16 changes: 11 additions & 5 deletions src/Typesense/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,27 @@ public FacetCountHit(string value, int count, string highlighted)
public record FacetStats
{
[JsonPropertyName("avg")]
public int Average { get; init; }
public float Average { get; init; }

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

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

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

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

public FacetStats(int average, int max, int min, int sum, int totalValues)
[JsonConstructor]
public FacetStats(
float average,
float max,
float min,
float sum,
int totalValues)
{
Average = average;
Max = max;
Expand Down
37 changes: 31 additions & 6 deletions test/Typesense.Tests/TypesenseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task Create_schema()
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("num_employees", FieldType.Int32, true, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees");
Expand All @@ -50,7 +50,7 @@ public async Task Create_schema()
new List<Field>
{
new Field("company_name", FieldType.String, false),
new Field("num_employees", FieldType.Int32, false),
new Field("num_employees", FieldType.Int32, true),
new Field("country", FieldType.String, true),
},
"num_employees");
Expand Down Expand Up @@ -98,7 +98,7 @@ public async Task Retrieve_collection()
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("num_employees", FieldType.Int32, true, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees");
Expand All @@ -119,7 +119,7 @@ public async Task Retrieve_collections()
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("num_employees", FieldType.Int32, true, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees")
Expand All @@ -138,7 +138,7 @@ public async Task Delete_collection()
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("num_employees", FieldType.Int32, true, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees");
Expand Down Expand Up @@ -557,6 +557,31 @@ public async Task Search_facet_by_country()
}
}

[Fact, TestPriority(11)]
public async Task Search_facet_by_num_employees()
{
var expected = new FacetCount("num_employees", new List<FacetCountHit>
{
new FacetCountHit( "10", 1, "10"),
new FacetCountHit( "531", 1, "531"),
new FacetCountHit( "1232", 1, "1232"),
new FacetCountHit( "6000", 1, "6000"),
}, new FacetStats(1943.25F, 6000F, 10F, 7773F, 4));

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

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()
{
Expand Down Expand Up @@ -962,7 +987,7 @@ private async Task CreateCompanyCollection()
new List<Field>
{
new Field("company_name", FieldType.String, false),
new Field("num_employees", FieldType.Int32, false),
new Field("num_employees", FieldType.Int32, true),
new Field("country", FieldType.String, true),
},
"num_employees");
Expand Down

0 comments on commit a1ab4ec

Please sign in to comment.