Rare Terms Aggregation Usage

edit

A multi-bucket value source based aggregation which finds "rare" terms — terms that are at the long-tail of the distribution and are not frequent. Conceptually, this is like a terms aggregation that is sorted by _count ascending. As noted in the terms aggregation docs, actually ordering a terms agg by count ascending has unbounded error. Instead, you should use the rare_terms aggregation.

Valid only in Elasticsearch 7.3.0+

See the Elasticsearch documentation on rare terms aggregation for more detail.

Fluent DSL example

edit
a => a
.RareTerms("names", st => st
    .Field(p => p.Name)
    .Missing("n/a")
    .MaximumDocumentCount(5)
    .Precision(0.001)
    .Meta(m => m
        .Add("foo", "bar")
    )
)

Object Initializer syntax example

edit
new RareTermsAggregation("names")
{
    Field = Infer.Field<Project>(p => p.Name),
    MaximumDocumentCount = 5,
    Precision = 0.001,
    Missing = "n/a",
    Meta = new Dictionary<string, object> { { "foo", "bar" } }
}

Example json output.

{
  "names": {
    "meta": {
      "foo": "bar"
    },
    "rare_terms": {
      "field": "name",
      "max_doc_count": 5,
      "missing": "n/a",
      "precision": 0.001
    }
  }
}

Handling Responses

edit
response.ShouldBeValid();
var rareTerms = response.Aggregations.RareTerms("names");
rareTerms.Should().NotBeNull();
rareTerms.Buckets.Should().NotBeNull();
rareTerms.Buckets.Count.Should().BeGreaterThan(0);
foreach (var item in rareTerms.Buckets)
{
    item.Key.Should().NotBeNullOrEmpty();
    item.DocCount.Should().BeGreaterOrEqualTo(1);
}
rareTerms.Meta.Should().NotBeNull().And.HaveCount(1);
rareTerms.Meta["foo"].Should().Be("bar");