☝️Small business or a startup? See if you qualify for our special offer.
+
All documentation
  • Introduction
  • Connecting to data source
    1. Supported data sources
    2. Connecting to other data sources
  • Browser compatibility
  • Documentation for older versions
  • Connecting to Elasticsearch

    This article illustrates how to build a report based on an Elasticsearch data source.

    Requirements

    • Elasticsearch version 6.x, 7.x, or 8.x
    • Flexmonster Pivot version 2.7.0 or higher (2.9.26 for Elasticsearch 8.x)

    Step 1. Embed the component into your webpage

    If Flexmonster is not yet embedded, set up an empty component in your webpage:

    In pure JavaScript

    Complete the Integrating Flexmonster guide. Your code should look similar to the following example:

    const pivot = new Flexmonster({
      container: "pivotContainer",
     toolbar: true
    });

    In React

    Complete the Integration with React guide. Your code should look similar to the following example:

    <FlexmonsterReact.Pivot
     toolbar={true}
    />

    In Angular

    Complete the Integration with Angular guide. Your code should look similar to the following example:

    <fm-pivot
     [toolbar]="true">
    </fm-pivot>

    In Vue

    Complete the Integration with Vue guide. Your code should look similar to the following example:

    <Pivot
     toolbar
    />

    Step 2. Enable cross-origin resource sharing (CORS)

    By default, the browser prevents JavaScript from making requests across domain boundaries. CORS allows web applications to make cross-domain requests.

    To enable CORS, open elasticsearch.yml and add the following lines:

    http.cors.enabled: true
    http.cors.allow-origin: "*"
    http.cors.allow-credentials: true
    http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"

    To allow a connection to the Elasticsearch server from machines other than localhost, open an appropriate port in the firewall. The default port is 9200, but it may be different depending on your configuration in the elasticsearch.yml file.

    Step 3. Configure the report with your own data

    On the client side, the report should be configured as follows (replace the node and index parameters with your values):

    const pivot = new Flexmonster({
    container: "pivotContainer",
    toolbar: true,
    report: {
    "dataSource": {
    "type": "elasticsearch",
    /* the host for the connection */
    "node": "https://olap.flexmonster.com:9200",
    /* the name of Elasticsearch index to connect */
    "index": "fm-product-sales"
    }
    }
    });

    To configure the dataSource object, check out the list of supported configuration parameters.

    Step 4. See the result

    Open your webpage in the browser to see Flexmonster with your Elasticsearch data Live example.

    If you run into any issues, visit our troubleshooting page.

    List of supported configuration parameters

    When connecting to data from Elasticsearch, you can use the following properties to configure the DataSourceObject:

    Property/TypeDescription
    type
    String
    The data source type. When connecting to data from Elasticsearch, set the type to "elasticsearch".
    node
    String
    The URL to the Elasticsearch endpoint.
    index
    Boolean
    The name of the Elasticsearch index.
    mapping
    MappingObject | String
    optional Defines how fields from the data source are treated and presented within the component. For example, you can specify the field’s captions or hide the field from the dataset. Read more about configuring the mapping in Elasticsearch.
    It can be either an inline MappingObject or a URL to a JSON file with the mapping Live example.
    subquery
    BoolQueryObject
    optional Sets a server-side filter to decrease the size of the response from the server Live example.
    requestHeaders
    Object
    optional Adds custom request headers. Consists of "key": "value" pairs, where "key" is a header name and "value" is its value Live example.
    Note The requestHeaders property is not saved when obtaining the report via save() and getReport() API calls.
    withCredentials
    Boolean
    optional Indicates whether cross-site Access-Control requests should be made using credentials such as authorization headers (true) or not (false). For more details, refer to MDN web docs.
    Setting the withCredentials flag to true is recommended when using Windows authentication and other types of server authentication. When set to false, the browser does not ask for credentials and does not include them in outgoing requests.
    Default value: false.

    Optimize data loading

    To decrease the data loading time, you can use the dataSource.subquery property. It sets a server-side filter, so Flexmonster will get only the needed part of the data. This approach also allows you to define which data is shown on the grid.

    The example below demonstrates how to load specific members of Category and Color fields:

    report: {
      dataSource: {
        type: "elasticsearch",
        node: "https://olap.flexmonster.com:9200",
        index: "fm-product-sales",
        subquery: {
          "bool": {
            "filter": [{
              "terms": {
                "Category.keyword": ["Bikes", "Accessories"],
              }
            },
            {
             "terms": {
               "Color.keyword": ["blue", "red"]
              }
            }]
          }
        }
      }
    }

    Live example

    What’s next?

    You may be interested in the following articles: