How to get data from elastic using the Collpase method in fluent in C#?

I'm hoping to find the answer to this. I have a C# application that bulk downloads elastic data according to user parameters. Some of the requests can be for all data that don't require parameters. In elastic I have an index that contains signals for DOT, each signal is a document. These are updated on certain days, so a signal can have multiple documents in the index based on time stamp.

What I would like to accomplish is to bring back all signals based on their latest time stamps, so essentially it would be one document per signal, the document with the latest time stamp. In kibana, I was able to accomplish this using the "collapse" method with "inner_hits" in order to bring back the latest document by time stamp for each signal. Here is that kibana code:

"size": 10000,
   "field": "atspmid.keyword",
   "inner_hits": {
      "name": "most_recent",
      "size": 1,
      "sort": [{"updated_timestamp": "desc"}]

In kibana, this does exactly what I want to do, bring back the latest signal documents by their time stamps.

When I entered the fluent code in C# and ran the application, I would get "0 documents found". Here is the code in C#:

var searchResponse = EsClient().Search<Tmdd_inventory>(s => s

           //.Query(q => q.MatchAll()));

                .Collapse(c => c
                  .Field(f => f.atspmid.Suffix("keyword"))
                  .InnerHits(ih => ih
                    .Sort(so => so
                        .Field(f2 => f2.updated_timestamp, Nest.SortOrder.Descending)

If I commented out the Collapse code and uncommented the Match All line above it, I would indeed get all documents, but that means all documents including all the ones per signal for all the differing times, which isn't what I want, I want only one document, the latest by time stamp, for each signal.

Why does the collapse code not bring back the documents I want in the order I specified? Is there something I'm missing, or is the C# code incorrect? If collapse won't work in C#, is there another way to retrieve the documents in the way I described? I was somewhat surprised to find so little information or documentation in the elastic site regarding the collapse method in fluent; believe me, I looked!

Any insight would be appreciated. Thank you.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.