ShaneKm
ShaneKm

Reputation: 21328

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 <MyDoc> 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 =
            elasticClient.Search<MyDoc>(
                s => s.Query(q => q.DateRange(x => x.LessThan(DateTime.Now.AddHours(-1)))));

Upvotes: 0

Views: 1434

Answers (1)

T N
T N

Reputation: 406

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)));

Upvotes: 2

Related Questions