{
"mappings": {
"exam": {
"properties": {
"id": {
"type": "long"
},
"score": {
"type": "integer"
},
"custom_score": {
"type": "integer"
}
}
}
}
}
我有 map 。使用此脚本计算custom_score
if(score >= 0)
custom_score = score
else
custom_score = score-100
Elasticsearch可以自动索引此字段吗?我想使用此值对某些查询进行一些排序。谢谢
最佳答案
您可以使用transform,但请注意,此功能在2.x中已弃用,在ES 5中将被删除。ES 5剩下的唯一选项是在您自己的客户端代码中进行转换,并索引已经相应更改的值。
但是,到目前为止,使用转换:
{
"mappings": {
"exam": {
"transform": {
"script": "if (ctx._source['score'].toInteger()>=0) ctx._source['custom_score'] = ctx._source['score'].toInteger(); else ctx._source['custom_score'] = ctx._source['score'].toInteger()-100"
},
"properties": {
"id": {
"type": "long"
},
"score": {
"type": "integer"
},
"custom_score": {
"type": "integer"
}
}
}
}
}
关于elasticsearch - Elasticsearch使用脚本映射自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39327669/