# How to read ScriptField from search response using NEST

**URL:** <https://discuss.elastic.co/t/how-to-read-scriptfield-from-search-response-using-nest/138392>\
**Category:** Elasticsearch\
**Created:** [July 3, 2018, 12:49pm UTC](https://discuss.elastic.co/t/how-to-read-scriptfield-from-search-response-using-nest/138392 "2018-07-03T12:49:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hooman\_Bahreini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hooman_bahreini/32/29500_2.png) [@Hooman\_Bahreini](https://discuss.elastic.co/u/Hooman_Bahreini)\
**Post date:** [July 3, 2018, 12:49pm UTC](https://discuss.elastic.co/t/how-to-read-scriptfield-from-search-response-using-nest/138392/1 "2018-07-03T12:49:04Z")

</div>

I am using Elasticsearch V6, Nest V6.

I am searching ES as below and I am using ScriptFields to calculate the distance and include it the result.

```
var searchResponse = _elasticClient.Search<MyDocument>(new SearchRequest<MyDocument>
{
    Query = new BoolQuery
    {
        Must = new QueryContainer[] { matchQuery },
        Filter = new QueryContainer[] { filterQuery },
    },
    Source = new SourceFilter
    {
        Includes = resultFields
    },
    ScriptFields = new ScriptField
    {
        Script = new InlineScript("doc['geoLocation'].planeDistance(params.lat, params.lng) * 0.001") // divide by 1000 to convert to km
        {
            Lang = "painless",
            Params = new FluentDictionary<string, object>
            {
                { "lat", _center.Latitude },
                { "lng", _center.Longitude }
            }
        }
    }
});

```

Now, I am trying to read the search result and I am not sure how to read the distance from the response, this is what I have tried:

```
// this is how I read the Document, all OK here
var docs = searchResponse.Documents.ToList<MyDocument>();

// this is my attempt to read the distance from the result
var hits = searchResponse.Hits;
foreach (var h in hits)
{
    var d = h.Fields["distance"];
    // d is of type Nest.LazyDocument 
    // I am not sure how to get the distance value from the type
}                                  

```

While debugging I can see the distance value, I am just not sure how to read the value?

![Distance](https://us1.discourse-cdn.com/elastic/original/3X/8/5/85e6f06c868541efefc868c67991af5dfa27688a.png)

---

<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:** [July 31, 2018, 12:49pm UTC](https://discuss.elastic.co/t/how-to-read-scriptfield-from-search-response-using-nest/138392/2 "2018-07-31T12:49:07Z")

</div>

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