elasticsearch7.x implements functions like redis self-increasing decr key

I. Introduction

es can add, delete, modify and check documents.

When changing the function, you can add a script (Painless language). This is a LUA script similar to redis, which is a singleton. That is, every request comes in, it will be executed once, and there will be no threading problems.

Painless is a scripting language developed and maintained by Elastic and optimized for Elasticsearch.

Refer to the following pictures for details

Insert picture description here

Reference article: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update.html

2. How to create es script

es scripts are divided into two types (Painless voice), one is inline script (inline script), that is, rendering is performed every time a request is made. The second is stored script (stored script), that is, after it is created, it is stored in a specific location in es

2.1 inline script

The following code is: java code

UpdateRequest request = new UpdateRequest("posts", "1"); 

Map<String, Object> parameters = singletonMap("count", 4); 

Script inline = new Script(ScriptType.INLINE, "painless",
        "ctx._source.field += params.count", parameters);  
request.script(inline); 

The above is the official case, which mainly means that every time an update operation is performed, 4 will be added to ctx._source.field.

2.2 stored script

Step 1: Store the script

The following uses es native language to create a script

POST _scripts/increment-field
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.field += params.count"
  }
}

Step 2: Use the created steps

Script stored = new Script(
        ScriptType.STORED, null, "increment-field", parameters);  
request.script(stored);

Three, specific cases

Just talk without practicing the fake style, let’s show you how to use the Painless voice of es to realize the incremental function of redis

3.1 The case of inline script

Initialize index data

PUT /canscript/_doc/1
{
  "name" :"小明",
  "age" : 18
}

Insert picture description here

It can be seen from the above that age=18

Use java for testing

        //更新请求
        UpdateRequest request = new UpdateRequest("canscript", "1");

        //添加脚本
        Map<String, Object> parameters = singletonMap("count", 1);

        Script inline = new Script(ScriptType.INLINE, "painless",
                "ctx._source.age += params.count", parameters);
        request.script(inline);

        //添加重试次数---并发情况下,丢弃抢占锁不到的请求
        request.retryOnConflict(3);

                //执行更新请求
        UpdateResponse update1 = restHighLevelClient.update(request, RequestOptions.DEFAULT);

Insert picture description here

It can be seen from the above that age is added by 1.

Note that documents and footsteps cannot be used at the same time. If you use footsteps to update data, you cannot update data with documents. Only one of the two

3.2 stored script (stored script) specific operations

Store the script and name it: increment-field

POST _scripts/increment-field
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.age += params.count"
  }
}

Use java to query

        //更新请求
        UpdateRequest request = new UpdateRequest("canscript", "1");

        //添加脚本
        Map<String, Object> parameters = singletonMap("count", 1);

        //使用已经存储在es的模板
        Script stored = new Script(
                ScriptType.STORED, null, "increment-field", parameters);
        request.script(stored);
        
//        Script inline = new Script(ScriptType.INLINE, "painless",
//                "ctx._source.age += params.count", parameters);
//        request.script(inline);

        //添加重试次数---并发情况下,丢弃抢占锁不到的请求
        request.retryOnConflict(3);

        //执行更新请求
        UpdateResponse update1 = restHighLevelClient.update(request, RequestOptions.DEFAULT);

Insert picture description here

It can be seen from the above that age is added by 1.

Four, attention

Elasticsearch is discarded by concurrency. If there is high concurrency, data will be discarded. If you want to ensure that every request will not lose data, you need to set the number of retries. For specific tests, when the number of retries is equal to the maximum number of concurrency , The data will not be discarded.

Five, the end

Most of the programmers are programming for Baidu or Google, and the information on the Internet is messy, sometimes it is uncomfortable to find, so I collect data for free, most of the information is collected and tested by myself. However, you don’t have to doubt the accuracy. However, your ability is limited and avoid omissions. I hope that readers can make corrections in comments or private messages, and everyone can contribute to Internet technology together.


Collecting information is boring. If this article is helpful to you, you can like it. This is also my greatest encouragement and praise.

Can I change my name or my surname? The Chaoshan’s Cancan Exhibition

Determined to make your own contribution in the Internet industry


Guess you like

Origin blog.csdn.net/qq_34168515/article/details/109284592