简体   繁体   English

如何使用Elasticsearch NPM在Node.js中实现ElasticSearch脚本

[英]How do i implement elasticsearch scripts in nodejs using elasticsearch npm

I wanted to maintain a field in my ES doc that maintains the count of every time I make a request to that doc such as an update request. 我想在我的ES文档中维护一个字段,该字段用于维护每次向该文档发出请求(例如更新请求)时的计数。 I am unable to make a syntax of a node query. 我无法进行节点查询的语法。 Please help me with this. 请帮我解决一下这个。

My query on console is: 我在控制台上的查询是:

POST /test_index_1/_doc/{id}/_update
{
 "script" : "ctx._source.visits+=1",
 "upsert": {
  "visits": 1
 }
}

I want to make this Query in nodejs. 我想在nodejs中进行此查询。

Elastic manages document version by default. Elastic默认管理文档版本。 So instead of having own field and using script to update is for any update would be redundant and more error prone as if you miss to update the field the actual count would be lost. 因此,与其拥有自己的字段并使用脚本进行更新,不如进行任何更新都是多余的,并且更容易出错,就像您错过更新字段一样,实际计数将丢失。 All this is managed by elastic search and I would suggest to leverage that. 所有这些都是通过弹性搜索来管理的,我建议利用它。

So whenever there is an update in document elastic search increases the value of document version by one. 因此,只要文档中有更新,弹性搜索就会将文档版本的值增加一。 To get the current version for each hit all you need to do is to add "version": true along with the search query as below: 要获得每次匹配的当前版本,您需要做的就是添加"version": true以及如下所示的搜索查询:

{
  "version": true,
  "query": {
    "match_all": {}
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM