Search code examples
c#elasticsearchnestelasticsearch-bulk-api

How to use ttl in elasticsearch bulk api (NEST)


I'm trying to use bulk API using NEST client. How can I specify a TTL value based on an attribute of a collection. Consider the following code snippet, how can I specify a "ttl" to say person.Age > 50 --> 1 month, otherwise 6 months?:

var coll = new List<Person>();
// fill the collection from db etc...

var desc = new BulkDescriptor();
foreach(var p in coll)
{
    //  desc.Index<Person>( .... );  
    //  How can I say, "if person.Age > 50, ttl = 1 month, otherwise 6 months?
}

var result = client.Bulk(desc);

Solution

  • desc.Index<Person>(i => i
        .Document(p)
        .Ttl(p.Age > 50 ? "1M" : "6M")
    );