Elastic Search | Version 6.7 | NEST | How to Read Script Field Values on Search Result

Hi Guys,
earlier, I have used elastic 5.7 version in my application for the elastic search feature and used Nest lib in C# to get search results. Recently, upgraded Elastic version to 6.7, similarly, I have updated the relevant C# Code. But, facing issue while reading Script field values and it does not allow me access Value from it.

var remainders = topHits.Hits().Select(x => x.Fields).First().ToList();
var d = ((LazyDocument)remainders[0].Value);

it has field value type as LazyDocument and it does not have the option to read a value from it.

Kindly suggest me any solution to this problem.

ScriptFields can be accessed as follows

var client = new ElasticClient();

var searchResponse = client.Search<object>(s => s
    .ScriptFields(sf => sf
        .ScriptField("commentMultiplier", sd => sd
            .Source("doc['commentCount'].value * params.factor")
            .Params(d => d
                .Add("factor", 3)

// gather a collection of document id and commentMultiplier tuples
var idsAndcommentMultipliers = searchResponse.Hits.Select(h =>
    var id = h.Id;
    // Script fields always return an array, so get first value for each document
    var commentMultiplier = h.Fields["commentMultiplier"].As<int[]>().First();  
    return (id, commentMultiplier);
1 Like

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