Use Cardinality to estimate how many sellers match a search, even with one hit per page.
dotnet add package RecursiveAI.OneIndex --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneIndex; using OneIndex.Aggregations; using var index = MakeIndex(); var results = index.Search("mug") .Aggregate(a => a.Cardinality( "sellers", x => x.Seller)) .Take(1).Execute(); var count = results.Aggregations .Get<CardinalityResult>("sellers"); Console.WriteLine(count.Value); SearchIndex<Listing> MakeIndex() { var index = SearchIndex.InMemory<Listing>(); index.Upsert([ new() { Id = "a", Name = "Mug", Seller = "North" }, new() { Id = "b", Name = "Mug", Seller = "North" }, new() { Id = "c", Name = "Mug", Seller = "South" } ]); index.Commit(); return index; } public class Listing { [SearchId] public string Id { get; set; } = ""; [FullText] public string Name { get; set; } = ""; [Keyword, Facet] public string Seller { get; set; } = ""; }
Prints the distinct seller count for three matching listings.
2Unzip the example, then dotnet run.
The full program
The download generates 10,000 mug listings from 5,000 sellers, plus one plate listing from a different seller. Each mug seller appears twice. The query returns one hit but estimates the number of sellers across all matching mug listings. The download pins OneIndex 0.4.0-preview.2.
using OneIndex; using OneIndex.Aggregations; using var index = SearchIndex.InMemory<Listing>(); for (int i = 0; i < 10_000; i++) { index.Upsert(new Listing { Id = $"mug-{i}", Name = "mug", Seller = $"seller-{i % 5_000}" }); } index.Upsert(new Listing { Id = "plate", Name = "plate", Seller = "plate-only" }); index.Commit(); var results = index.Search("mug").Take(1) .Aggregate(a => a.Cardinality( "sellers", x => x.Seller)).Execute(); var sellers = results.Aggregations .Get<CardinalityResult>("sellers"); Console.WriteLine($"Hits returned: {results.Count}"); Console.WriteLine($"Matching listings: {results.TotalHits}"); Console.WriteLine($"Estimated sellers: {sellers.Value}"); Console.WriteLine("Distinct sellers in the mug input: 5000"); public class Listing { [SearchId] public string Id { get; set; } = ""; [FullText] public string Name { get; set; } = ""; [Keyword, Facet] public string Seller { get; set; } = ""; }
dotnet run prints:
Hits returned: 1 Matching listings: 10000 Estimated sellers: 4951 Distinct sellers in the mug input: 5000
If Seller is marked only [Keyword], the aggregation throws ArgumentException because the field has no column; [Facet] supplies one.
Variations
To count sellers for plates instead of mugs, replace the query and output below index.Commit() in the download.
var results = index.Search("plate").Take(1) .Aggregate(a => a.Cardinality( "sellers", x => x.Seller)).Execute(); var sellers = results.Aggregations .Get<CardinalityResult>("sellers"); Console.WriteLine(sellers.Value);
1Related recipes
Faceted search · Aggregate search results by month
The short answer
Attach Cardinality to the search and read CardinalityResult.Value by the name you gave the aggregation. It estimates how many distinct values the selected field has across all matching documents, not just those returned by Take. The search can return one listing while the aggregation counts thousands of sellers.
How do you count distinct values across all search matches?
Choose a column that represents what you want to count. The example uses Seller as a keyword rather than full text, so the aggregation counts whole seller values, not words in a seller name. In an application, use a stable seller ID if two sellers can share a display name. [Facet] keeps the column the aggregation reads, and Cardinality("sellers", ...) names the result so it can be retrieved alongside other aggregations.
The query determines which documents contribute. The plate seller doesn’t count in the mug search. Repeated seller values do not increase the distinct count. A document with no value for that field contributes no value. In the snippet at the top, North appears twice and South appears once, so the count is two even though only one hit comes back. The operation doesn’t remove duplicate sellers from the returned hits or list which seller values were counted.
How accurate is an approximate distinct count?
OneIndex’s cardinality aggregation uses a HyperLogLog++ sketch, a compact summary of the hashed values. At the default precision, the sketch keeps the hashes themselves through 2,048 distinct hashes, so small counts are exact except for hash collisions. Above that threshold it switches to registers that summarize the hashes and returns an estimate. The documented standard error at default precision is 0.81%, not a guarantee that every result lands within that percentage.
That’s why the small snippet prints 2 but the larger download prints 4951 for a known 5000. If an exact count determines a payment, a threshold or an export total, compute it from the complete filtered data instead of treating a sketch estimate as exact.