Use Percentiles to estimate p95, the time within which about 95% of matching requests finish.
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 = SearchIndex.InMemory<Request>(); Seed(index); var results = index.Search("checkout") .Aggregate(a => a.Percentiles("latency", x => x.Milliseconds, 95)).Take(0).Execute(); var p95 = results.Aggregations .Get<PercentilesResult>("latency"); var estimated = p95.Values[0].Value; Console.WriteLine(FormattableString.Invariant( $"p95: {estimated:0.##} ms")); Console.WriteLine($"Matches: {results.TotalHits}"); static void Seed(SearchIndex<Request> index) { foreach (int id in Enumerable.Range(1, 20)) index.Upsert(new Request( id.ToString(), "checkout", id * 10)); index.Upsert(new Request("21", "login", 900)); index.Commit(); } public class Request( string id, string endpoint, int milliseconds) { public Request() : this("", "", 0) { } [SearchId] public string Id { get; set; } = id; [FullText] public string Endpoint { get; set; } = endpoint; [Filterable] public int Milliseconds { get; set; } = milliseconds; }
Unzip the example, then dotnet run.
p95: 195 ms Matches: 20
Percentiles are estimated with a t-digest, so 195 ms need not be a recorded latency.
This example uses OneIndex 0.4.0-preview.2. APIs and file formats may change between previews.
An invalid field or aggregation setting throws ArgumentException; if no matching document has a numeric value, the estimate is NaN.
Variations
To read the median and two tail percentiles in one request, replace the query and output with:
var results = index.Search("checkout") .Aggregate(a => a.Percentiles("latency", x => x.Milliseconds, 50, 95, 99)) .Take(0).Execute(); var latency = results.Aggregations .Get<PercentilesResult>("latency"); foreach (var (percent, value) in latency.Values) Console.WriteLine(FormattableString.Invariant( $"p{percent}: {value:0.##} ms"));
p50: 105 ms p95: 195 ms p99: 200 ms
Related recipes
Aggregate search results by month · Faceted search in C#
The short answer
Call Aggregate(a => a.Percentiles("latency", x => x.Milliseconds, 95)) on the query whose matches you want to summarize. Mark the numeric field with [Filterable] so the index keeps a column to aggregate. Then read the estimate from results.Aggregations.Get<PercentilesResult>("latency"). The example estimates p95 from 20 checkout requests with latencies from 10 to 200 ms. It excludes the login request, even though that request has the highest latency in the index.
What is p95 latency?
The 95th percentile is the latency at or below which roughly 95% of observations fall. The example’s 195 ms is a latency, not a percentage and not the average of the slowest requests. If the median stays steady but p95 increases, slower requests can be getting worse even if a typical request hasn’t changed much. The variation reads p50 (the median), p95, and p99 from the same query so you can compare them for the same population.
Match the query to the question you want to answer. “How slow are checkout requests?” and “How slow are all requests?” describe different populations. OneIndex aggregates over every matching document, excluding deleted documents. Take(0) skips returning the hit documents; it doesn’t limit which matches contribute values. TotalHits counts matching documents, so don’t treat it as the count of numeric observations if some documents don’t have a value.
How do you calculate p95 with an approximate percentile aggregation?
OneIndex uses a t-digest, which summarizes the values in small groups and estimates a percentile by interpolation. It does not need to retain a sorted copy of every value. On a small data set such as this one, the digest can keep the individual values, but it can still interpolate between them; 195 ms isn’t the latency recorded by any of the requests.
The estimate can vary with the index’s segment layout. Don’t use it to answer the different question “Exactly how many requests took more than 195 ms?” For that, apply a numeric filter and request an exact count. If your report requires the exact percentile under a specific convention, use the raw observations and calculate it under that convention. See the percentile algorithm for the interpolation rules.