Scripted fields in vega visualization - Getting Bad request as error


{
  $schema: "https://vega.github.io/schema/vega-lite/v2.json"
 data: {
  url: {
    %context%: true
 
    index: index_ssd_2
    body: {
      size: 10000,
      _source: ["mediatype","pred_comp_sem","Gender","PhotoOfPerson"],
      script_fields : {
        photo : {
          script : {
            lang: 'painless',
            source: (doc['PhotoOfPerson'].value) / 2
          }
        }
      }
    }
  }
  format: { property: "hits.hits" }
},
 "transform": [
 {
    "joinaggregate": [{
      "op": "sum",
      "field": "fields.photo",
      "as": "a_pop"
    }]
  }
  ],
  
  "mark": {
      "type": "text",
      "fontSize": 32,
      "align":"center",
      "dx": -240,
      "dy": 12
    },
    "encoding": {
      "text": {"field": "a_pop", "type": "quantitative"}
      }
    
}

In vega visualzation I tried to create a scripted field....it's showing bad request as error, may I know where exactly I went wrong

Is PhotoOfPerson a number?

Open up developer tools in your browser (usually F12) and go to the Network tab. Update your Vega visualization and click the Update button to trigger a new request. Look for es in red if it failed and click the response tab like I've shown below. That is the full error that could help determine what is going on.

But syntax wise it looks good besides wondering if PhotoOfPerson is a mapped and stored as a number in order to do some math on it.

Also you can go to Dev Tools and run the below and see if it works or gives an error also.

GET index_ssd_2/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "photo": {
      "script": {
        "lang": "painless",
        "source": "doc['PhotoOfPerson'].value * 2"
      }
    }
  }
}

I tried the query in dev tools, the error was regarding not filtering out null values.

This is the new query

{
  $schema: "https://vega.github.io/schema/vega-lite/v2.json"
 data: {
  url: {
    %context%: true
 
    index: index_ssd_2
    body: {
      size: 10000,
      _source: ["mediatype","pred_comp_sem","Gender","PhotoOfPerson"],
      script_fields : {
        photo : {
          script : {
            lang: 'painless',
            source: if(doc['PhotoOfPerson'].size() != 0){return doc['PhotoOfPerson'].value/1} else{return 0}
        }
      }
    }
  }
  format: { property: "hits.hits" }
},
 "transform": [
 {
    "joinaggregate": [{
      "op": "sum",
      "field": "fields.photo",
      "as": "a_pop"
    }]
  },
   {
    "joinaggregate": [{
      "op": "sum",
      "field": "_source.PhotoOfPerson",
      "as": "a_p"
    }]
  }
  ],
  "mark": {
      "type": "text",
      "fontSize": 32,
      "align":"center",
      "dx": -240,
      "dy": 12
    },
    "encoding": {
      "text": {"field": "a_pop", "type": "quantitative"}
    }
    
}
}

Here, I filtered out null values, its not showing any error in dev tools. But its showing Invalid specification Vega visualization

Any thoughts on this @aaron-nimocks

I don't use vega-lite so not as familiar with it. But the error does not recognize the mark element so something is off with your formatting.

Okay, thanks @aaron-nimocks

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.