+1 800 256 3608 (toll-free in North America) or +49 7531 90 60 10| service@combit.com

Connecting List & Label to Elasticsearch Using NEST in C#

Introduction

NEST is a convenient way to access Elasticsearch data. List & Label can easily be bound to NEST result objects, which enables to bind to data from Elasticsearch clusters.

Implementation

As the response object is rather complex, you should set a couple of properties on an explicitly generated ObjectDataProvider object in order to present the user with just what is required. A minimal example (using the data from this example) might be:

using (ListLabel LL = new ListLabel())
{
    // retrieve data from Elasticsearch
    var response = ConnectionToES.EsClient().Search<DocumentAttributes>(s => s
    .Index("disney")
    .From(0)
    .Size(1000)
    .Query(q => q.MatchAll()));

    // create ObjectDataProvider with minimal clutter
    ObjectDataProvider provider = new ObjectDataProvider(response.Hits)
    {
        // flatten structure to the minimal amount required
        MaximumRecursionDepth = 1,

        // assign a nice, descriptive name to the main data table
        RootTableName = "Data"
    };

    // register event of the ObjectDataProvider to engage into its parsing
    provider.HandleEnumerableProperty += Provider_HandleEnumerableProperty;

    // bind to data and call the Designer
    LL.DataSource = provider;
    LL.Design();
}

Using the event HandleEnumerableProperty of the List & Label ObjectDataProvider provides access to the assigned object structure:

private void Provider_HandleEnumerableProperty(object sender, HandleEnumerablePropertyEventArgs e)
{
    // just don't follow any paths
    e.CancelRecursion = true;
}

Result

The data is then available from the Data > Source folder in the Designer:

image

and can easily be used from there as simple table:

image

If you want to further strip this down, you may create a simplified, typed object from the results of course as well.

© combit GmbH