Files
rikako-note/elastic search/02_elastic_search_index.md
2025-01-02 12:54:29 +08:00

13 KiB
Raw Blame History

index modules

index module introduce

index module用于对索引进行创建并且控制和索引相关的各个方面。

索引设置

索引级别的设置可以针对每个索引来进行设置,设置分类如下:

  • staticstatic设置只能在如下时机应用于索引
    • 索引创建时
    • 索引处于closed状态
    • 使用update-index-settings api并且带有reopen=true的查询参数,带有该参数时,会关闭受影响索引,更新后将受影响索引重新开启
  • dynamicdynamic索引可以在索引处于活跃状态时通过udpate-index-settings api进行修改

closed index

当一个索引处于closed状态时所有针对该索引的read/write操作都会被阻塞所有可以用于opened状态索引的操作,其执行对于closed状态的索引来说都不允许。

对于closed状态的索引,既无法向其中新增文档,也无法在索引中检索文档。

处于closed状态的索引能够通过open index api来重新开启。

static index settings

index.number_of_shards

索引拥有的primary shards数量默认为1该配置项只能在索引创建时被设置。即使索引处于closed状态该配置项也无法修改。

index.number_of_shards最大为1024

Analysis

index analysis module充当了可配置的analyzer注册中心analyzer可用于将string类型字段转换为独立的terms这将用于

  • 将文档string field的字段转化为terms并且将terms添加到倒排索引中令文档可以被搜索
  • analyzer被用于高级查询例如match查询将用户输入的查询字符串分割为search terms用于查询

index shard allocation

index shard allocation提供了可针对单个索引的设置用于控制node中shard的分配

  • shard allocating filtering控制shard被分配给哪个node
  • delayed allocation节点离开导致未分配shard的延迟分配
  • total shards per node相同索引在同一node中shards数量的上限
  • data tier allocation控制对data tier分配的索引

index blocks

index blocks限制了针对特定索引的操作类型。操作阻塞的类型可以分为

  • 读操作阻塞
  • 写操作阻塞
  • 元数据操作阻塞

针对索引操作的阻塞可以通过dynamic index setting来进行新增和移除。并且阻塞也可以通过特定的api来进行添加和移除。

针对wrtie blocks设置的修改一旦修改成功那么所有的在途写操作都已经完成。

index block settings

index.blocks.read_only

如果该配置项设置为true该索引和索引的元数据都是只读的当设置为false时,允许写操作和元数据变更。

index.blocks.read_only_allow_delete

类似于index.blocks.write但是可以对index进行删除。不要针对index.blocks.read_only_allow_delete进行手动设置或移除。disk-based shard allocator会根据磁盘剩余空间自动添加或移除该配置项。

从索引中删除文档释放资源而不是删除索引本身会暂时增加索引的大小故而在node的磁盘空间不足时可能无法实现。当index.blocks.read_only_allow_delete被设置为true时并不允许删除索引中的文档。但是,删除索引本身的操作只需要极少量的额外磁盘空间,并且几乎可以立即删除索引所占用的空间,故而删除索引本身的操作仍然被允许。

elastic search在磁盘占用高于flood stage watermark时,会自动为索引增加 read-only-allow-delete阻塞;当磁盘占用率跌倒high watermark之下时,则是会自动释放该阻塞

index.blocks.read

如果设置为true会阻塞针对index的读操作

index.blocks.write

如果设置为true会阻塞针对索引的写操作index.blocks.read_only不同本设置项并不影响metadata。

例如为索引设置write block后仍然可以对metadata进行变更但是设置了index.blocks.read_only后,无法对元数据进行变更

index.blocks.metadata

如果设置为true会禁止对元数据的读取和变更

增加index block示例

# PUT /<index>/_block/<block>
PUT /my-index-000001/_block/write

path param

  • <index>: 由,分隔的列表或通配符表达式,代表该请求的索引名称
    • 默认情况下,<index>部分需要指定索引的精确名称。如果想要使用_all, *等通配表达式,需要将action.destructive_requires_name属性设置为false
  • <block>: 向索引应用的阻塞类型
    • <block>部分可选的值为metadata, read, read_only, write

query param

  • allow_no_indices
    • 如果该参数设置为false那么当索引项中任一wildcard expression, idnex alias或_all值没有匹配的索引或只能匹配到closed状态的索引那么该请求会返回异常。
    • 例如foo*,bar*foo*表达式匹配到索引,但是bar*没有相匹配的索引,那么会抛出异常。
    • 该参数认值为true
  • expand_wildcards:
    • wildcard pattern能够匹配到的索引类型。如果请求能够匹配到data stream那么该参数能够决定wildcard pattern能够匹配到hidden data stream
    • 该参数的值支持,分隔,有效的值如下:
      • all匹配任何data stream或index包括hidden的
      • open:匹配open, non-hidden状态的索引和non-hidden状态的data stream
      • closed:匹配closed, non-hidden状态的索引和non-hidden状态的data stream
      • hidden:匹配hidden状态的索引和hidden状态的data stream。hidden必须和open, closed中任一组合使用,也能和两者一起使用open, closed, hidden
      • none不接受wildcard pattern
      • 该参数默认值为open
    • ignore_unavailable: 如果参数设置为false若未匹配到索引或匹配到closed状态的索引返回异常
      • 该参数默认值为false
    • master_timeout等待master node的最大时间默认为30s如果超过该限制master node仍然不可访问那么该请求会返回异常
    • timeout在更新完metadata后等待cluster中所有节点返回的时间限制默认为30s。如果超时后仍未能接受到返回那么针对cluster metadata的修改仍然会被应用但是在返回中会指定并非接受到了所有的ack

示例

添加write block的示例如下所示

PUT /my-index-000001/_block/write

返回结果如下:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "indices" : [ {
    "name" : "my-index-000001",
    "blocked" : true
  } ]
}

Similarity module

similarity moudlescoring/ranking model定义了如何对匹配到的document进行打分。similaity是针对单个字段的这意味着可以通过mapping为每个字段都定义不同的mapping。

similarity仅适用于text类型和keyword类型的字段。

配置similarity

大多similarity都可以通过如下方式进行配置

PUT /index
{
  "settings": {
    "index": {
      "similarity": {
        "my_similarity": {
          "type": "DFR",
          "basic_model": "g",
          "after_effect": "l",
          "normalization": "h2",
          "normalization.h2.c": "3.0"
        }
      }
    }
  }
}

上述示例中配置了DFR similarity故而在mapping中即可通过my_similarity来进行引用,示例如下所示:

PUT /index/_mapping
{
  "properties" : {
    "title" : { "type" : "text", "similarity" : "my_similarity" }
  }
}

slow query

Search Slow log

shard level slow search log允许将slow query记录到特定的日志文件中。

对于threshold可以对query阶段和fetch阶段分别进行配置示例如下所示

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

上述所有的配置都是dynamic并且可以针对每个index单独进行设置示例如下所示

PUT /my-index-000001/_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"
}

默认情况下threshold为-1代表threshold被停用。

该日志针对的是shard的范围。

search slow log file在log4j2.properties文件中进行配置。

identify search log origin

通过将index.search.slowlog.include.user配置项设置为true可以在slow log中输出触发该slow query的用户信息,示例如下:

PUT /my-index-000001/_settings
{
  "index.search.slowlog.include.user": true
}

上述设置将导致用户信息将会被包含在slow log中

{
  "@timestamp": "2024-02-21T12:42:37.255Z",
  "log.level": "WARN",
  "auth.type": "REALM",
  "elasticsearch.slowlog.id": "tomcat-123",
  "elasticsearch.slowlog.message": "[index6][0]",
  "elasticsearch.slowlog.search_type": "QUERY_THEN_FETCH",
  "elasticsearch.slowlog.source": "{\"query\":{\"match_all\":{\"boost\":1.0}}}",
  "elasticsearch.slowlog.stats": "[]",
  "elasticsearch.slowlog.took": "747.3micros",
  "elasticsearch.slowlog.took_millis": 0,
  "elasticsearch.slowlog.total_hits": "1 hits",
  "elasticsearch.slowlog.total_shards": 1,
  "user.name": "elastic",
  "user.realm": "reserved",
  "ecs.version": "1.2.0",
  "service.name": "ES_ECS",
  "event.dataset": "elasticsearch.index_search_slowlog",
  "process.thread.name": "elasticsearch[runTask-0][search][T#5]",
  "log.logger": "index.search.slowlog.query",
  "elasticsearch.cluster.uuid": "Ui23kfF1SHKJwu_hI1iPPQ",
  "elasticsearch.node.id": "JK-jn-XpQ3OsDUsq5ZtfGg",
  "elasticsearch.node.name": "node-0",
  "elasticsearch.cluster.name": "distribution_run"
}

index slow log

index slow log和search slow log类似其log file名称以_index_indexing_slowlog.json结尾。index slow log的配置如下所示

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

index slow log的配置也是dynamic的可以通过如下示例来进行配置

PUT /my-index-000001/_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"
}

如果想要在日志中包含触发该slow index请求的用户可以通过如下方式进行请求

PUT /my-index-000001/_settings
{
  "index.indexing.slowlog.include.user": true
}

默认情况下elasticsearch会打印slow log中头1000个字符。可以通过index.indexing.slowlog.source来修改该配置。

  • 如果将indexing.slowlog.source设置为false或0将会跳过对source的输出
  • 如果将indexing.slowlog.source设置为true将会输出所有source的内容