四时宝库

程序员的知识宝库

Elasticsearch常用索引设置settings配置项详解

一.静态设置

所谓静态设置,是指在创建索引时指定的设置,后面是不能更改的

1.number_of_shards

  • 功能: 指定主分片的数量
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.number_of_shards":3
    }
}
  • 建议:主分片的数量一般与索引的数据量相关,一般是elasticsearch实例的数据节点数的1-3倍,如有3个数据节点,那么主分片的数量一般为3-9个主分片

2.codec

  • 功能: 设置数据压缩方式
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.codec":"best_compression"
    }
}

3.shard.check_on_startup

  • 功能: 在elasticsearch启动时是否检查分片内容
  • 可选项:
    • false: 在打开一个分片时不检查
    • checksum: 检查每个匹配的分片的内容的校验码
    • true: 不仅检查每个分片的检验码,还要检查逻辑的不一致性

4.sort

  • 功能: 在索引数据(写入数据)时指定数据的排序字段,用于检索时排序(是提高搜索效率的一种重要手段),根据业务需要,设置相应的排序字段和排序方式
  • 参数:
    • field: 排序字段(可以是一个字段,也可以是多个字段)
    • order: 排序方式(asc|desc)
    • mode: 应用于多字段值(multi-fields)的字段的排序模式
      • min: 使用最小的那个值
      • max: 使用最大的那个值
    • missing: 当文档中该字段值不存在时,排序方式:
      • _first: 排在最前面
      • _last: 排在最后面
PUT steam_item_730
{
    "settings":{
        "index.sort.field": "last_sale_time",
        "index.sort.order":"desc"
    }
}

多个排序字段时:

PUT steam_item_730
{
    "settings":{
        "index.sort.field": ["min_price", "last_sale_time","sale_count"]
        "index.sort.order":["asc","desc","desc"]
    }
}
  • 排序优先级依次是: min_price.asc , last_sale_time.desc , 最后是 sale_count.desc

5.analysis

  • 功能: 定义分词器(自定义分词器,分词器更多内容后续将详细介绍)
  • 参数:
    • analyzer: 定义文本分词器
    • search_analyzer: 定义搜索查询时使用和文本分词器(如果未指定,则使用analyzer定义的分词器)

二.动态设置

所谓动态设置,是指可以在创建索引时指定设置,在后续中也可以更改的配置项

1.number_of_replicas

  • 功能:设置副本分片的个数(注:这里是指一个主分片对应的副本分片数量,不是整个集群副本分片数量)
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.number_of_replicas":1
    }
}

2.refresh_interval

  • 功能: 数据refresh到对搜索可见的刷新频率(该参数影响数据写入速度,如果刷新频率频繁,则写入效率更低,反之,刷新频率更高)
  • 默认值: 默认是每1s刷新1次
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.refresh_inteval":"30s"
    }
}
  • 需要根据实现的业务情况,来调整这个值
    • 如果业务数据实时性要求较高,建议不要调整该值的配置
    • 如果实时性无特殊要求,但要求高速写入,可适当调高该值

3.max_result_window

  • 功能: 搜索时可展示的最大结果数量(是from+size的总和)
  • 默认值:10000
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.max_result_window":50000
    }
}
  • 笔者建议:
    • 技术方面的考量:非强烈的业务要求,建议不要调高这个值,因为调高这个值,意味着需要更多的内存和CPU,可能会影响整个系统的稳定性
    • 业务方面的思考:一般对于用户来说,不会去关注10页以后的数据,可以通过其他筛选条件或更加智能化、更加个性化的搜索,来满足用户的搜索需求

4.max_inner_result_window

  • 功能: 与 max_result_window参数类似,只不过它关注的内联结果inner_hits的结果数量
  • 默认值: 100
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.max_inner_result_window":200
    }
}

5.max_script_fields

  • 功能:script_fields 最大数量(通过脚本的自定义字段的数量)
  • 默认值:32
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.max_script_fields":64
    }
}

7.hidden

  • 功能: 索引是否隐藏不可见(只是在使用通配符表达式的情况下隐藏)
  • 默认值: false
  • 实例:
PUT steam_item_730
{
    "settings":{
        "index.hidden":true
    }
}

8.blocks.read_only

  • 功能: 索引数据及索引元数据只可读
  • 实例:
PUT test_history
{
  "mappings": {
    "properties": {
      "id":{
        "type":"keyword"
      },
      "name":{
        "type":"text",
        "analyzer": "ik_max_word"
      }
    }
  },
  "settings": {
    "index":{
      "number_of_shards":1,
      "number_of_replicas":1,
      "blocks.read_only":true
    }
  }
}

此次写入数据:

PUT test_history/_doc/1
{
  "id":1,
  "name":"test1"
}

会抛出一个只读的异常

{
  "error": {
    "root_cause": [
      {
        "type": "cluster_block_exception",
        "reason": "index [test_history] blocked by: [FORBIDDEN/5/index read-only (api)];"
      }
    ],
    "type": "cluster_block_exception",
    "reason": "index [test_history] blocked by: [FORBIDDEN/5/index read-only (api)];"
  },
  "status": 403
}

9.blocks.read_only_allow_delete

  • 功能: 索引数据及索引元数据只能读,写操作仅支持删除

10.blocks.read

  • 功能:索引数据不支持读操作

11.blocks.write

  • 功能:索引数据不支持写操作

12.blocks.metadata

  • 功能: 索引元数据不支持读写操作

13. index.merge.scheduler.max_thread_count

  • 功能: 段合并最大的线程数

14.translog.sync_interval

  • 功能: 同步写事务日志的频率
  • 默认值: 5s
  • 范围:最小值为100ms
  • 实例:
PUT steam_item_730
{
  "settings": {
    "index":{
      "translog.sync_interval":"10s"
    }
  }
}

15.translog.durability

  • 功能: 写入事务日志的时机
  • 可选值:
    • request: 每次写入数据时写入
    • async: 异步写入(根据写事务日志的频率来写入)
PUT steam_item_730
{
  "settings": {
    "index":{
      "translog.durability":"async"
    }
  }
}

16.translog.flush_threshold_size

  • 功能: 当事务日志达到指定的阈值时执行flush操作
  • 默认值: 512m

17.search.slowlog

  • 功能:查询慢日志阈值设置
  • 实例:
PUT steam_item_730/_settings
{
  "index.search.slowlog.threshold.query.warn": "10s",
  "index.search.slowlog.threshold.query.info": "5s",
  "index.search.slowlog.threshold.query.debug": "2s",
  "index.search.slowlog.threshold.query.trace": "500ms",
  "index.search.slowlog.threshold.fetch.warn": "1s",
  "index.search.slowlog.threshold.fetch.info": "800ms",
  "index.search.slowlog.threshold.fetch.debug": "500ms",
  "index.search.slowlog.threshold.fetch.trace": "200ms"
}

18.indexing.slowlog

  • 功能: 索引慢日志阈值设置
  • 实例:
PUT steam_item_730/_settings
{
  "index.indexing.slowlog.threshold.index.warn": "10s",
  "index.indexing.slowlog.threshold.index.info": "5s",
  "index.indexing.slowlog.threshold.index.debug": "2s",
  "index.indexing.slowlog.threshold.index.trace": "500ms",
  "index.indexing.slowlog.source": "1000"
}

19.indexing_pressure.memory.limit

  • 功能: 索引数据时内存限制参数(即当内存达到一个指定值时,不再索引数据,当达到1.5倍的这个限制值时,复制操作也将不再执行)

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接