首页 > 解决方案 > script_fields 总是返回数组。如何返回对象或简单数据类型?

问题描述

我注意到在使用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
        ]
    }
}

标签: elasticsearchelasticsearch-painless

解决方案


推荐阅读