ES自动创建索引导致类型不匹配的坑。

前言

目前在线上的环境,突然发现个严重问题,我们创建的索引有的字段的类型是不对的。

原因

按照我们在索引的时候会先判断是否需要创建索引,如果不存在则创建索引,创建索引的时候带Map信息,这样数据不应该是和我们字段是不一致的。

可能原因猜测

建索引目前是多进程多线程并发,创建索引的可能Elasticsearch还没有来得及建好,数据已经发过来了,按照es的默认配置是会自动创建索引的,所以导致了部分索引数据字段类型的不一致问题。

解决办法

自然是自动创建索引惹的祸,那就禁止自动创建索引好了,禁止命令如下,在elasticsearch.yml中配置:

action.auto_create_index: +first*,-full*,+.watches*,+.triggered_watches,+.watcher-history-*,+.kibana*

+号后面的索引表示可以自动创建。
-号后面的索引表示不能自动创建。

后记

后来发现是不能自动创建索引了,还存在着类型不一致的问题,后面通过配置:
index.mapper.dynamic=false
限制自动创建索引。

也通过了索引模板的方式建索引,希望可以规避这个问题。
提供一个例子:

PUT _template/firstcorrelation_template 
{
  "template": "firstcorrelation*",
  "settings": {
    "index": {
      "codec": "best_compression",
      "routing": {
        "allocation": {
          "total_shards_per_node": "2"
        }
      },
      "refresh_interval": "120s",
      "number_of_shards": "36",
      "translog": {
        "flush_threshold_size": "800mb",
        "sync_interval": "120s",
        "durability": "async"
      },
      "merge": {
        "scheduler": {
          "max_thread_count": "1"
        }
      },
      "max_result_window": "100000",
      "number_of_replicas": "0"
    }
    
  },
    "mappings": {
      "aus_type": {
        "_source": {
          "enabled": true
        },
        "_all": {
          "enabled": false
        },
        "properties": {
          "collectTime": {
            "format": "yyyy-MM-dd HH:mm:ss.SSS||epoch_millis||epoch_second",
            "type": "date"
          },
          "recordTime": {
            "format": "yyyy-MM-dd HH:mm:ss.SSS||epoch_millis||epoch_second",
            "type": "date"
          },
          "rawMsg": {
            "index": "analyzed",
            "type": "text"
          },
          "logtype": {
            "index": "not_analyzed",
            "store": "yes",
            "type": "byte"
          },
          "username": {
            "index": "analyzed",
            "store": "yes",
            "type": "keyword"
          }
        }
      }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_33757609/article/details/87424596