Process and analyze data with Transforms

Updated at:

The Transforms feature in Elasticsearch lets you preprocess, aggregate, and transform data. It creates a new destination index without affecting the raw data, making it ideal for analysis and visualization. In this tutorial, you will use Transforms to process NBA shot data and view the results.

Prerequisites

  1. Download the sample data. This tutorial uses the Kaggle dataset NBA shot logs. It includes fields such as shot time, shooter, shot location, the closest defender, and the defender's distance from the shooter. To download the dataset, see shot_logs.csv.

  2. Create an Alibaba Cloud Elasticsearch instance. For more information, see Create an Alibaba Cloud Elasticsearch instance. This tutorial uses an Elasticsearch V7.10.0 instance.

    Note

    Elasticsearch V8.5 instances are not supported.

  3. Log on to the Kibana console. For more information, see Log on to the Kibana console.

  4. Import the shot_logs data and create an index.

    1. In the upper-left corner of the Kibana page, choose menu.png > Kibana > Machine Learning.

    2. Click the Data Visualizer tab.

    3. In the Import data area, click Upload file.

    4. Click the image.png icon.

    5. Select the shot_logs file.

    6. In the lower-right corner of the page, click Import.

    7. On the Simple tab, set Index name to nba_shot_logs and select Create index pattern.

    8. Click Import.

      After a successful import, the following summary is displayed:

      • Index: nba_short_logs

      • Index pattern: nba_short_logs

      • Ingest pipeline: nba_short_logs-pipeline

      • Documents ingested: 128069

Procedure

This tutorial describes three methods to transform and view data.

Method 1: Create a transform in Kibana

  1. In the upper-left corner of the Kibana page, choose menu.png > Management > Stack Management.

  2. In the left-side navigation pane, click Data > Transforms.

  3. Click Create your first transform.

  4. Select the nba_shot_logs index.

  5. In the Configuration section, set Group by to histogram(GAME_ID), and set Aggregations to DRIBBLES.sum, DRIBBLES.avg, and DRIBBLES.max.

    Note
    • Groups data by game ID.

    • DRIBBLES.sum: the total number of dribbles by all players in each game.

    • DRIBBLES.avg: the average number of dribbles per player in each game.

    • DRIBBLES.max: The maximum number of dribbles before a shot in each game.

  6. In the lower-right corner of the Configuration section, click Next.

  7. In the Transform details section, specify the Transform ID and destination index, and click Next.

  8. In the Create section, click Create and start.

    Note

    The transform is complete when the progress bar reaches 100%.

  9. Click Discover to view the data in the destination index.

    The Discover page displays the destination index test_nba_shot_logs with 91 hits. Each document contains the aggregation fields DRIBBLES.avg, DRIBBLES.max, DRIBBLES.sum, and the group-by field GAME_ID.

Method 2: Create and preview with API

  1. In the upper-left corner of the Kibana page, choose menu.png > Management > Dev Tools.

    1. On the Console tab, run the following command to create a transform.

      PUT _transform/test2_nba_shot_logs
      {
          "source": {
          "index": "nba_shot_logs"
        },
        "dest" : { 
          "index" : "test2_nba_shot_logs"
        },
        "pivot": {
          "group_by": { 
            "game_id": { "terms": { "field": "GAME_ID" }}
          },
          "aggregations": {
            "dribbles_sum": { "sum": { "field": "DRIBBLES" }},
            "dribbles_avg": { "avg": { "field": "DRIBBLES" }},
            "dribbles_max": { "max": { "field": "DRIBBLES" }}
          }
        }
      }
    2. Run the following command to preview the transformed data.

      POST _transform/_preview
      {
        "source": {
          "index": "nba_shot_logs"
        },
        "dest" : { 
          "index" : "test2_nba_shot_logs"
        },
        "pivot": {
          "group_by": { 
            "game_id": { "terms": { "field": "GAME_ID" }}
          },
          "aggregations": {
            "dribbles_sum": { "sum": { "field": "DRIBBLES" }},
            "dribbles_avg": { "avg": { "field": "DRIBBLES" }},
            "dribbles_max": { "max": { "field": "DRIBBLES" }}
          }
        }
      }

Method 3: Create by API, view in Discover

  1. Create and start the transform.

    1. In the upper-left corner of the Kibana page, choose menu.png > Management > Dev Tools.

    2. On the Console tab, run the following command to create the transform.

      PUT _transform/test2_nba_shot_logs
      {
          "source": {
          "index": "nba_shot_logs"
        },
        "dest" : { 
          "index" : "test2_nba_shot_logs"
        },
        "pivot": {
          "group_by": { 
            "game_id": { "terms": { "field": "GAME_ID" }}
          },
          "aggregations": {
            "dribbles_sum": { "sum": { "field": "DRIBBLES" }},
            "dribbles_avg": { "avg": { "field": "DRIBBLES" }},
            "dribbles_max": { "max": { "field": "DRIBBLES" }}
          }
        }
      }
    3. Start the transform.

      POST _transform/test2_nba_shot_logs/_start
      Note

      Transforms are stopped by default.

  2. Create an index pattern.

    Note

    You must create an index pattern to view the data in Discover.

    1. In the upper-left corner of the Kibana page, choose menu.png > Management > Stack Management.

    2. In the left-side navigation pane, click Kibana > Index Patterns.

    3. In the upper-right corner of the page, click Create index pattern.

    4. For Index pattern name, enter the destination index, test2_nba_shot_logs, and click Next step.

    5. Click Create index pattern.

  3. View data in Discover.

    1. In the upper-left corner of the Kibana page, choose menu.png > Kibana > Discover.

    2. Select the destination index to view the transformed data.