How to bind searchResult from ElasticSearch into Gridview (C# ASP.NET) by using NEST

I need to bind the search result from NEST (ElasticSearch) to a Gridview in ASP.NET (Webform).

Code I get the result from ElasticSearch from using NEST:

public class Address
{
    public int SN { get; set; }
    public string JLN { get; set; }
}

protected void BtnSearch_Clicked(object sender, EventArgs e)
{
    string SearchValue = txtSearchValue.Text;
    string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
    string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
    string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

    var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
        .DefaultIndex("masterlist*");

    var client = new ElasticClient(settings);

    var searchResponse = client.Search<Address>(s => s
            .Index("masterlist*")
            .From(0)
            .Size(10)
            .Query(q => q
                 .QueryString(qs => qs
                    .Query("JLN:\""+ SearchValue +"\"")
                )
            )
        );

    var address = searchResponse.Documents.ToList();

    ESGridview.DataSource = address;
    ESGridview.DataBind();
}

With this code, the gridview can auto-generate two fields of correct header which is "SN" and "JLN", and it can auto generate 10 rows (I limit the size to 10 rows max in search syntax) but it's empty data in the column.

I did found another POST with this link
https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/returned-fields.html#returned-fields
After check with this link,
I changed my code to

string SearchValue = txtSearchValue.Text;
string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
    .DefaultIndex("masterlist*");

var client = new ElasticClient(settings);

var searchResponse = client.Search<Address>(s => s
    .StoredFields(sf => sf
        .Fields(
            f => f.SN,
            f => f.JLN
            )
         )
        .From(0)
        .Size(10)
        .Query(q => q
             .QueryString(qs => qs
                .Query("JLN:\""+ SearchValue +"\"")
            )
        )
    );

foreach (var fieldValues in searchResponse.Fields)
{
    var document = new
    {
        SN = fieldValues.ValueOf<Address, int>(p => p.SN),
        JLN = fieldValues.Values<Address, string>(p => p.JLN)
    };
}

var address = searchResponse.Documents;

ESGridview.DataSource = address;
ESGridview.DataBind();

But I get an error while run the code from start on whole foreach (var...) area :

System.NullReferenceException:'Object reference not set to an instance of an object.'

Did anyone can teach me how can solve this problem or anything I do fault ?
Many many thanks ~

ElasticSearch 7.0.1
NEST 7.0.0
C#
ASP.NET (Webform)

I solve my problem already.
The code below is how to get the searchResult from ElasticSearch and bind the data into Gridview in ASP.NET by using NEST.

public class Address
{
    [Text(Name = "SN")]
    public string SN { get; set; }

    [Text(Name = "JLN")]
    public string JLN { get; set; }
}

protected void BtnSearch_Clicked(object sender, EventArgs e)
{
    string SearchValue = txtSearchValue.Text;
    string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
    string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
    string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

    var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
        .DefaultIndex("masterlist*");

    var client = new ElasticClient(settings);

    var searchResponse = client.Search<Address>(s => s
            .From(0)
            .Size(100)
            .Query(q => q
                 .QueryString(qs => qs
                    .Query("JLN:\"" + SearchValue + "\"")
                )
            )
        );

    var address = searchResponse.Documents.ToList();

    ESGridview.DataSource = address;
    ESGridview.DataBind();
}

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