Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Vega visualization on nested fields #9510

Open
alycesst opened this issue Mar 10, 2025 · 0 comments
Open

[BUG] Vega visualization on nested fields #9510

alycesst opened this issue Mar 10, 2025 · 0 comments
Labels
bug Something isn't working untriaged

Comments

@alycesst
Copy link

I have the following index user_roles_analytics

{
  "user_roles_analytics" : {
    "mappings" : {
      "properties" : {
        "roles" : {
          "type" : "nested",
          "properties" : {
            "effective_from" : {
              "type" : "date"
            },
            "effective_until" : {
              "type" : "date"
            },
            "role_id" : {
              "type" : "keyword"
            },
            "role_name" : {
              "type" : "keyword"
            }
          }
        },
        "timestamp" : {
          "type" : "long"
        },
        "user_id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "username" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

which represents the assignment of roles to a user with the field roles being a nested field.
I am trying to create a visualization, I've noticed that the classical visualizations aren't available for the nested fields, the nested fields do not appear in the list of available fields

Online it is suggested to use vega, but I could not find a vega table visualization to display on one column the user name, and on the other the flattened role name

I am trying this, but I am getting error undefined

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "data": [
    {
      "name": "user_roles_analytics",
      "url": {
        "index": "user_roles_analytics",
        "body": {
          "_source": ["username", "roles.role_name"],
          "query": {
            "match_all": {}
          }
        }
      },
      "format": {
        "property": "hits.hits"
      }
    },
    {
      "name": "flattened",
      "source": "user_roles_analytics",
      "transform": [
        {
          "type": "flatten",
          "fields": ["_source.roles"]  // Flatten the roles array
        },
        {
          "type": "formula",
          "as": "row",
          "expr": "datum._id"  // Using _id as a unique row identifier
        }
      ]
    }
  ],
  "marks": [
    {
      "type": "group",
      "from": {
        "data": "flattened"
      },
      "marks": [
        {
          "type": "text",
          "encode": {
            "enter": {
              "x": { "value": 0 },  // Positioning for username column
              "y": { "field": "row" },
              "text": { "field": "_source.username" },  // Accessing username
              "fontSize": { "value": 12 },
              "align": { "value": "left" },
              "baseline": { "value": "middle" },
              "fill": { "value": "#000000" }
            }
          }
        },
        {
          "type": "text",
          "encode": {
            "enter": {
              "x": { "value": 200 },  // Positioning for role_name column
              "y": { "field": "row" },
              "text": { "field": "_source.roles.role_name" },  // Accessing role_name
              "fontSize": { "value": 12 },
              "align": { "value": "left" },
              "baseline": { "value": "middle" },
              "fill": { "value": "#000000" }
            }
          }
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": ["username", "role_name"],
      "range": "width"
    },
    {
      "name": "y",
      "type": "linear",
      "domain": [0, {"signal": "length(data('flattened'))"}],  // Adjust for number of rows
      "range": "height"
    }
  ],
  "size": {
    "signal": "{width: 600, height: 400}"
  }
}

How to overpass the error?

Thanks a lot!

@alycesst alycesst added bug Something isn't working untriaged labels Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working untriaged
Projects
None yet
Development

No branches or pull requests

1 participant