Script_fields always returns array. how to return an object or simple data type?

I noticed that when using script_fields it always return array of the values that should be returned. I wonder why is this happening and is it possible to just return a non-array data type like an object or bool?

Example to illustrate (taken from web)

	GET sat/_search

	{
	  "script_fields": {
		"some_scores": {
		  "script": {
			  "lang": "painless",
			"inline": "def scores = 0; scores = doc['AvgScrRead'].value + doc['AvgScrWrit'].value; return scores;" <--- we are returning a number here!
		  }
		}
	  }
	}

Result:

	{
		"_index": "sat",
		"_type": "scores",
		"_id": "AV3CYR8JFgEfgdUCQSON",
		"_score": 1,
		"_source": {
			"cds": 1611760130062,
			"rtype": "S",
			"sname": "American High",
			"dname": "Fremont Unified",
			"cname": "Alameda",
			"enroll12": 444,
			"NumTstTakr": 298,
			"AvgScrRead": 576,
			"AvgScrMath": 610,
			"AvgScrWrit": 576,
			"NumGE1500": 229,
			"PctGE1500": 76.85,
			"year": 1516
		},
		"fields": {
			"some_scores": [ <----- here it's an array
				1152
			]
		}
	}

An array is always returned because there could be 1 to many values. Sometimes returning a singular value directly, rather than in an array, would mean the structure of the output would change, depending on the data, which is undesirable from a parsing perspective.

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