0

我的索引之一的部分映射如下:

"schools": {
  "type": "object",
  "dynamic": true,
  "properties": {}
}

现有文档的示例schools对象是:

"schools": {
  "8291": {
    "max": 11,
    "min": 11
  },
  "3546": {
    "max": 12,
    "min": 10
  },
  "3896": {
    "max": 18,
    "min": 12
  }
}

我在 ES6.2 中有无痛脚本,其中动态key(例如 3896)在运行时计算并作为参数传递给脚本

"script": {
  "params": {
    "key": key
  },
  "source": {
    if (doc.containsKey('schools') && doc.schools.containsKey(key)){
      String dynamic_key = "schools."+key+".min";
      return doc[dynamic_key].value;
    } else {
      return 0;
  }
}

所以如果传递key的值是 3896,那么这应该返回 12。但我收到一条错误消息

No field found for [schools.3896.min] in mapping with types [myindexname]

我究竟做错了什么?访问在运行时计算键名以获取其值的动态对象的正确方法是什么?

4

1 回答 1

0

这是获取嵌套对象值的方法:

PUT intstitutes
{
  "mappings": {
    "log": {
      "properties": {
        "schools": {
          "type": "object",
          "dynamic": true,
          "properties": {}
        }
      }
    }
  }
}

PUT /intstitutes/log/1?refresh
{
  "schools": {
    "3546": {
      "max": 12,
      "min": 10
    },
    "3896": {
      "max": 18,
      "min": 12
    },
    "8291": {
      "max": 11,
      "min": 11
    }
  }
}

POST intstitutes/_search
{
  "query": {
    "match": {
      "_id": "1"
    }
  },
  "script_fields": {
    "script_score": {
      "script": {
        "params": {
          "key": "3896"
        },
        "source": "if (params._source.containsKey('schools') && params._source.schools.containsKey(params.key)) { return params._source.schools.get(params.key).min } else { return 0 }"
      }
    }
  }
}
DELETE intstitutes
于 2018-07-10T13:26:23.223 回答