NEST Search and return most recent document?


(Alberto Gonzalez) #1

I am indexing documents using NEST, they dont have a timestamp, is there any way to retrieve/search for the most recent inserted document? or do i have to add a timestamp to my docs when indexing and then sort by that and size(1)?


(Ashley Mannix) #2

Yeh, you'll have to add a timestamp to your docs, and can use a simple match_all query with size = 1 and sort on your timestamp field.


(Alberto Gonzalez) #3

I am trying to set a Timestamp to documents on Index but the parameter is Nest.Time how can i convert DateTime.Now to Nest.Time TimeSpan?

index_result = elk.Index(c, i => i
    .Index("index-" + date)
    .Id(asset_tag + "_appliance")
    .Timestamp(DateTime.Now)
    );

(Ashley Mannix) #4

That's probably not the timestamp you want to be using.

What you want to do is add a DateTime (or DateTimeOffset) field to your document type, and that's the field you then sort on.


(Alberto Gonzalez) #5

Thanks that worked!


(system) #6