Node.js 如何在elasticsearch中设置计数器字段
我需要在elasticsearch中将字段“post_count”增加+1 例如:在我的例子中,当我点击一个按钮时,post_计数需要增加Node.js 如何在elasticsearch中设置计数器字段,node.js,
elasticsearch,Node.js,
elasticsearch,我需要在elasticsearch中将字段“post_count”增加+1 例如:在我的例子中,当我点击一个按钮时,post_计数需要增加 "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "test_work", "_type": "user", "_id": "d989dd8629f8b6cc
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_work",
"_type": "user",
"_id": "d989dd8629f8b6cc59faf8a1aa2328c8",
"_score": 1,
"_source": {
"first_name": "test",
"last_name": "amt",
"post_count":0
}
}
]
是否有任何一个查询可以在每次更新中增加post_计数
POST /test_work/user/d989dd8629f8b6cc59faf8a1aa2328c8/_update
{
"script" : "ctx._source.post_count+=1"
}
试着这样做:
POST /test_work/user/d989dd8629f8b6cc59faf8a1aa2328c8/_update
{
"script" : "ctx._source.post_count+=1"
}
还可以使用脚本中的参数控制计数器的增量。 如果您使用的是ES 1.3+,则可能会出现一个错误,提示“dynamic scripting disabled”,为了避免此错误,您需要指定脚本语言,在本例中为groovy
POST /test_work/user/d989dd8629f8b6cc59faf8a1aa2328c8/_update
{
"script" : "ctx._source.post_count+=increment",
"params" : {
"increment" : 4
},"lang":"groovy"
}
还可以使用脚本中的参数控制计数器的增量。 如果您使用的是ES 1.3+,则可能会出现一个错误,提示“dynamic scripting disabled”,为了避免此错误,您需要指定脚本语言,在本例中为groovy
POST /test_work/user/d989dd8629f8b6cc59faf8a1aa2328c8/_update
{
"script" : "ctx._source.post_count+=increment",
"params" : {
"increment" : 4
},"lang":"groovy"
}
感谢您的快速重播,我尝试了查询并得到错误{“error”:“ElasticsearchIllegalArgumentException[未能执行脚本];嵌套:ScriptException[动态脚本禁用];,“status”:400}在哪里可以启用动态脚本?您需要将此添加到
elasticsearch.yml
文件:script.disable\u dynamic:false
请注意,编写脚本时要小心。确保没有人可以对RESTAPI执行查询。根据版本,您有一些选项。最好是迁移到1.4.x并开始使用groovy脚本来实现这些目的。检查此处的文档:感谢您的快速重播,我尝试了查询并得到错误{“error”:“ElasticSearchillegargumentException[未能执行脚本];嵌套:ScriptException[动态脚本禁用];,“status”:400}在哪里可以启用动态脚本?您需要将此添加到elasticsearch.yml
文件:script.disable\u dynamic:false
请注意,编写脚本时要小心。确保没有人可以对RESTAPI执行查询。根据版本,您有一些选项。最好是迁移到1.4.x并开始使用groovy脚本来实现这些目的。检查此处的文档: