"invalid sequence of tokens near ['for']" error in painless script query

I an getting error while executing this script query using Nest :

 new ScriptQuery
                                            Lang = "painless",
                                            Source = "(!doc[params.headquartersCoordinatesField].empty && doc[params.headquartersCoordinatesField].arcDistance(params.latitude, params.longitude) * 0.000621371 <= params.maxDistance) || (!doc[params.offices].empty && (for (def office : doc[params.offices].values){if(office.coordinates).arcDistance(params.latitude, params.longitude) * 0.000621371 < =params.maxDistance{return true;}}))",
                                            Params = new Dictionary<string, object>
                                                {"headquartersCoordinatesField", Field<Provider>(f => f.Headquarters.Coordinates)},
                                                {"offices", Field<Provider>(f => f.Offices)},
                                                {"latitude", _latitude},
                                                {"longitude", _longitude},
                                                {"maxDistance", 50} 

The error is as follows :
ServerError: Type: search_phase_execution_exception Reason: "all shards failed" CausedBy: "Type: script_exception Reason: "compile error" CausedBy: "Type: illegal_argument_exception Reason: "invalid sequence of tokens near ['for']." CausedBy: "Type: no_viable_alt_exception Reason: "no_viable_alt_exception: null""""

Can someone look into this and let me know what changes are needed to run it successfully ? Thanks in advance.

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