Elasticsearch7.x 增删改可选参数的Refresh的分析

起因:增删改之后,马上查找,数据不会发生变化

发现elasticsearch执行增删改之后,是不会立即进行刷新的,需要配置refresh这个参数

参数由来

以下摘录自官网:https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html

Update API
Updates a document using the specified script.

Request
POST /<index>/_update/<_id>

Query parameters
 if_seq_no
(Optional, integer) Only perform the operation if the document has this sequence number. See Optimistic concurrency control.
 if_primary_term
(Optional, integer) Only perform the operation if the document has this primary term. See Optimistic concurrency control.
 lang
(Optional, string) The script language. Default: painless.

 refresh
(Optional, enum) If true, Elasticsearch refreshes the affected shards to make this operation visible to search, 
if wait_for then wait for a refresh to make this operation visible to search,
if false do nothing with refreshes. Valid values: true, false, wait_for. Default: false.

Refresh参数(true、false、wait_for)分析

  1. 如果为 true,Elasticsearch刷新受影响的碎片,使搜索操作可见。既立即刷新,但是损坏性能
  2. 如果为wait_for,则等待刷新以使该操作对搜索可见。等待时间为1s
  3. 如果是假的,就不要刷新。

有效值:true、false、wait_for。默认值:false。

官方对Refresh的解释

以下摘录自 https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-refresh.html

Empty string or true

Refresh the relevant primary and replica shards (not the whole index) immediately after the operation occurs, 
so that the updated document appears in search results immediately. This should ONLY be done after careful thought and verification that it does not lead to poor performance, 
both from an indexing and a search standpoint.

wait_for

Wait for the changes made by the request to be made visible by a refresh before replying. This doesn’t force an immediate refresh, rather, 
it waits for a refresh to happen. Elasticsearch automatically refreshes shards that have changed every index.refresh_interval which defaults to one second. That setting is dynamic. 
Calling the Refresh API or setting refresh to true on any of the APIs that support it will also cause a refresh, 
in turn causing already running requests with refresh=wait_for to return.

 false (the default)
 
Take no refresh related actions. 
The changes made by this request will be made visible at some point after the request returns.

在这里插入图片描述

Java客户端配置

在这里插入图片描述

    public String updateDataById(Object object, String index, String id,Boolean isRealTime) throws IOException {
        //更新请求
        UpdateRequest request = new UpdateRequest(index, id);

        if(isRealTime!=null){
            if(isRealTime){
                //立即同步,但是消耗性能
                request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            }else{
                //1s 进行同步,但消耗时间长
                request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
            }
        }

        request.timeout("1s");
        request.doc(JSON.toJSONString(object), XContentType.JSON);

        //添加重试次数
        request.retryOnConflict(3);

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

        String idString = update1.getId();

        LogUtils.info("索引为: {}, id为: {}, 更新数据成功",index, idString);

        return idString;
    }

猜你喜欢

转载自blog.csdn.net/qq_34168515/article/details/108869052
今日推荐