在ES的日常使用中,需要使用到Nested结构存储数个同级的子节点数据,例如一条主订单下的N条子订单的数据。
新增更新操作
现在,假设我们在ES中有这样一条数据
PUT /celebrities/_doc/114
{
"user" : "Kun",
"post_date" : "2009-11-15T14:12:12",
"message" : "Idol who has been practicing for two and a half years",
"skills":[
{
"name":"sing",
"skill_level":"A"
},
{
"name":"jump",
"skill_level":"S"
},
{
"name":"rap",
"skill_level":"SS"
}
]
}
我们需要往skills的Nested数组中添加一个新的节点,节点的name为“consecutive five whips”,则可以这么写
POST /celebrities/_doc/114
{
"script": {
"source": "if (ctx._source.skills == null) {List ls = new ArrayList();ls.add(params.skill);ctx._source.skills = ls;} else {ctx._source.skills.add(params.skill);}",
"lang": "painless",
"params": {
"skill": {
"name": "consecutive five whips",
"skill_level": "SSS"
}
}
}
}
得到返回结果,表明执行成功
{
"_index": "celebrities",
"_id": "114",
"_version": 6,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
通过代码可以简单的看出逻辑,如果_source.skills为null,则创建一个新的ArrayList,并将参数中的skill节点的内容放进去,最后赋值给_source.skills,反之如果不为null的话,则直接往里加入当前的skill字段的内容。
if (ctx._source.skills == null) {
List ls = new ArrayList();
ls.add(params.skill);
ctx._source.skills = ls;
} else {
ctx._source.skills.add(params.skill);
}
要先判断是否为null,否则无法调用add方法,并会抛出一个异常
"caused_by": {
"type": "null_pointer_exception",
"reason": "cannot access method/field [add] from a null def reference"
}
Continue reading