script_fields始终返回数组。如何返回对象或简单数据类型?

时间:2018-08-31 15:39:43

标签: elasticsearch elasticsearch-painless

我注意到,使用script_fields时,它总是返回应返回值的数组。我不知道为什么会这样,是否有可能只返回对象或布尔等非数组数据类型?

示例说明(来自网络)

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!
      }
    }
  }
}

结果:

{
    "_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
        ]
    }
}

0 个答案:

没有答案