Advanced JSON Inputs Math functions

I want to calculate an angle from a x and a y value with this script:

{
  "script": {
    "inline": "if (doc['x'].value == 0 and doc['y'].value > 0{return 270;}if (doc['x'].value == 0 and doc['y'].value < 0 {return 90;}return Math.atan(-doc['y'].value/doc['x'].value);",
    "lang": "painless"
  }
}

...but it doesnt work. What am i doing wrong?

@6tUBp3gwd9Zp there are many syntax issues in the script, mainly forgetting to put the closing parenthesis on your if predicates and using and instead of &&

{
  "script": {
    "inline": """
    if (doc['x'].value == 0 && doc['y'].value > 0) {
        return 270;
    }
    if (doc['x'].value == 0 && doc['y'].value < 0) {
        return 90;
    }
    return Math.atan(-doc['y'].value/doc['x'].value);
    """,
    "lang": "painless"
  }
}
1 Like

Thank you very much this helped a lot.

1 Like

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