ShaneKm ShaneKm - 1 year ago 157
C# Question

Add Timestamp to each document added in ElasticSearch - Nest 2.0

How can I make sure each document indexed in elasticsearch gets timestamped?

node = new Uri("http://localhost:9201);
settings =
new ConnectionSettings(node).DefaultIndex("mytestindex");
elasticClient = new ElasticClient(settings);

// Then I do
elasticClient.Index(connections, idx => idx.Id("1")

  1. Using NEST and C# how can I make sure each document I index gets timestamp?

  2. How can I query for all documents of type
    with older than an hour ago?

I found this: Adding Timestamp to each document added in ElasticSearch
however it doesn't show me how to do this with NEST

I've tried this, however looking at the result timestamp is null and it returns all documents in index:

var test =
s => s.Query(q => q.DateRange(x => x.LessThan(DateTime.Now.AddHours(-1)))));

Answer Source

You can enable _timestamp but _timestamp is deprecated. Don't use it anymore, just define a date properties in your data object and set it explicitly

await client.MapAsync<Blog>(m => m.TimestampField(t => t.Enabled(true)));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download