This section describes how to retrieve data from Elasticsearch database using Nest library and bind it to the Pivot Table via a Web API controller.
1. Download the ASP.NET Core Web Application from this GitHub repository.
2. The application named as PivotController (server-side) that is downloaded from the above GitHub repository includes the PivotController.cs file under Controllers folder, which is helps to do data communication with Pivot Table.
3. In the Web API controller (aka, PivotController), ElasticClient helps to connect the Elasticsearch database. Next, using Search method you can query your Elasticsearch index and retrieve results from the database.
4. In the Get() method of the PivotController.cs file, the FetchElasticsearchData method is used to retrieve the Elasticsearch data, which is then serialized into JSON using JsonConvert.SerializeObject().
using Microsoft.AspNetCore.Mvc;
using Nest;
using Newtonsoft.Json;
namespace PivotController.Controllers
{
[ApiController]
[Route("[controller]")]
public class PivotController : ControllerBase
{
[HttpGet(Name = "GetElasticSearchData")]
public object Get()
{
return JsonConvert.SerializeObject(FetchElasticsearchData());
}
private static object FetchElasticsearchData()
{
// Replace with your own connection string.
var connectionString = "<Enter your valid connection string here>";
var uri = new Uri(connectionString);
var connectionSettings = new ConnectionSettings(uri);
var client = new ElasticClient(connectionSettings);
var searchResponse = client.Search<object>(s => s
.Index("product")
.Size(1000)
);
return searchResponse.Documents;
}
}
}
5. Run the web application (aka, PivotController) and it will be hosted within the URL https://localhost:7105
.
6. Finally, the retrieved data from Elasticsearch database which is in the form of JSON can be found in the Web API controller available in the URL link https://localhost:7105/Pivot
, as shown in the browser page below.
1. Download the TypeScript Pivot Table sample from this GitHub repository.
2. Next, map the hosted Web API’s URL link https://localhost:7105/pivot
to the Pivot Table component in app.ts by using the url property under dataSourceSettings.
import { PivotView, FieldList } from '@syncfusion/ej2-pivotview';
PivotView.Inject(FieldList);
let pivotObj: PivotView = new PivotView({
dataSourceSettings: {
url: 'https://localhost:7105/Pivot',
//Other codes here...
}
});
pivotObj.appendTo('#PivotView1');
3. Frame and set the report based on the data retrieved from the Elasticsearch database.
import { PivotView, FieldList } from '@syncfusion/ej2-pivotview';
PivotView.Inject(FieldList);
let pivotObj: PivotView = new PivotView({
dataSourceSettings: {
url: 'https://localhost:7105/Pivot',
expandAll: false,
enableSorting: true,
columns: [{ name: 'Product' }],
values: [{ name: 'Quantity' }, { name: 'Amount', caption: 'Sold Amount' }],
rows: [{ name: 'Country' }, { name: 'State' }],
formatSettings: [{ name: 'Amount', format: 'C0' }],
filters: []
},
showFieldList: true,
width: '100%',
height: 290,
gridSettings: { columnWidth: 140 }
});
pivotObj.appendTo('#PivotView1');
4. Run the sample to get the following result.