Elasticsearch 7:刷新周期 refresh_interval


#Elasticsearch 笔记


简介

当数据添加到索引后并不能马上被查询到,等到索引刷新后才会被查询到。 refresh_interval 配置的刷新间隔。

refresh_interval 的默认值是 1s。

单位:

  • ms: 毫秒
  • s: 秒
  • m: 分钟

如果是指定的纯数字,单位是毫秒。

-1 的使用

当 refresh_interval 为 -1 时,意味着不刷新索引。

当需要大量导入数据到ES中,可以将 refresh_interval 设置为 -1 以加快导入速度。导入结束后,再将 refresh_interval 设置为一个正数,例如1s

或者手动 refresh 索引。

速查

将某索引的 refresh_interval 设置为 1分钟

PUT student/_settings
{
    "index" : {
        "refresh_interval" : "1m"
    }
}

添加数据时忽略 refresh_interval 配置,直接触发刷新索引

POST student/_doc?refresh
{ "name" : "李四" }

重置 refresh_interval

PUT student/_settings
{
    "index" : {
        "refresh_interval" : null
    }
}

示例:

创建索引:

PUT student
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "keyword"
      },
      "age" : {
        "type" : "integer"
      }
    }
  }
}

设置 refresh_interval:

PUT student/_settings
{
    "index" : {
        "refresh_interval" : "1m"
    }
}

添加数据:

POST student/_doc
{ "name" : "张三" }

马上查询数据,是查不到的:

# 执行
GET student/_search

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

1分钟后查询数据:

# 执行
GET student/_search

# 结果
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "5b7bmnEBLucs5EFBg4vB",
        "_score" : 1.0,
        "_source" : {
          "name" : "张三"
        }
      }
    ]
  }
}

写入数据时可以指定 ?refresh 让数据马上写入。

POST student/_doc?refresh
{ "name" : "李四" }

马上去查,能查到数据:

# 执行
GET student/_search

# 结果
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "5b7bmnEBLucs5EFBg4vB",
        "_score" : 1.0,
        "_source" : {
          "name" : "张三"
        }
      },
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "5r7dmnEBLucs5EFBO4ts",
        "_score" : 1.0,
        "_source" : {
          "name" : "李四"
        }
      }
    ]
  }
}

相关文档



( 本文完 )