# How to return document field for NEST client with search?

**URL:** https://discuss.elastic.co/t/how-to-return-document-field-for-nest-client-with-search/278175
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [July 8, 2021, 10:32am UTC](https://discuss.elastic.co/t/how-to-return-document-field-for-nest-client-with-search/278175 "2021-07-08T10:32:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dreic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dreic/32/77811_2.png) [@dreic](https://discuss.elastic.co/u/dreic)
#### Post date: [July 8, 2021, 10:32am UTC](https://discuss.elastic.co/t/how-to-return-document-field-for-nest-client-with-search/278175/1 "2021-07-08T10:32:05Z")

</div>

I have following mappings:

```auto
{
  "mappings": {
      "properties" : {
        "CompletionTime" : {
          "type" : "date",
          "format": "date_hour_minute_second",
          "fields" : {
            "keyword" : {
              "type" : "date"
            }
          }
        },
        "Description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "InitiatorName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "StartTime" : {
          "type" : "date",
          "format": "date_hour_minute_second",
          "fields" : {
            "keyword" : {
              "type" : "date"
            }
          }
        },
        "TargetObjectName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "TargetObjectType" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
  }
}

```

I created following code for C# Nest client:

```auto
public DateTime GetNewestDocumentDate()
        {
            var searchResponse = this.ec.Client.Search<Record>(s => s
                    .Query(q => q.MatchAll())
                    .Sort(so => so
                        .Field(fs => fs
                            .Field("StartTime")
                            .Order(SortOrder.Descending)
                        ))
                    .Size(1)
            );
            var a = searchResponse.Documents;
            Console.WriteLine(JsonSerializer.Serialize(a));

            return a.StartTime;
        }

```

I am new with C# and Elastic so I don't know how to return document field. In my case it should be something like this, but my syntax is not correct. `var a` is an array and a[0].StartTime didn't pass check in Visual Studio.

---

<div class="post-metadata">

### Author: ![dreic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dreic/32/77811_2.png) [@dreic](https://discuss.elastic.co/u/dreic)
#### Post date: [July 8, 2021, 7:06pm UTC](https://discuss.elastic.co/t/how-to-return-document-field-for-nest-client-with-search/278175/2 "2021-07-08T19:06:28Z")

</div>

Solution:

```auto
var a = searchResponse.Documents.ElementAt(0);
            
return a.StartTime;

```

---

<div class="post-metadata">

### Author: ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)
#### Post date: [July 27, 2021, 10:56am UTC](https://discuss.elastic.co/t/how-to-return-document-field-for-nest-client-with-search/278175/3 "2021-07-27T10:56:13Z")

</div>

Hi dreic! I'm glad you found the solution. Indeed, Documents is a collection of the hits, deserialised to your `Record` type so you can enumerate those and access any of the properties as required.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 24, 2021, 10:56am UTC](https://discuss.elastic.co/t/how-to-return-document-field-for-nest-client-with-search/278175/4 "2021-08-24T10:56:36Z")

</div>

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