Cane's Blog

Cane

【Problem】Elasticsearch5.x迁移7.x版本问题集合

43
2020-07-08

Elasticsearch 连接不上 Elasticsearch-head

问题描述

Elasticsearch 连接不上 Elasticsearch-head

截图

2020-07-08-14-13-07.png

解决方案

原因:Elasticsearch 默认不支持跨域连接

在 Elasticsearch 配置文件添加跨域支持配置文件路径:/etc/elasticsearch/elasticsearch.yml 跨域支持

http.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"

Python Elsticsearch 法创建索引

问题描述

Error: elasticsearch.exceptions.RequestError: TransportError

截图

...

解决方案

原因:Python Elasticsearch支持库与 Elasticsearch 版本不一致

  • 级至对应版本

    查看 Python Elasticsearch

    pip show elasticsearch
    
    
    Name: elasticsearch
    Version: 5.5.3
    Summary: Python client for Elasticsearch
    Home-page: https://github.com/elastic/elasticsearch-py
    Author: Honza Král, Nick Lang
    Author-email: honza.kral@gmail.com, nick@nicklang.com
    License: Apache License, Version 2.0
    Location: e:\anaconda\lib\site-packages
    Requires: urllib3
    Required-by: elasticsearch-dsl
  • 使用 Postman 来创建索引

Elasticsearch 7.x 以上版本不支持自定义 Mapping

问题描述

Root mapping definition has unsupported parameters

{
    "error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "Root mapping definition has unsupported parameters:  xxxx"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:  xxxx,
        "caused_by": {
            "type": "mapper_parsing_exception",
            "reason": "Root mapping definition has unsupported parameters:  xxxx"
        }
    },
    "status": 400
}

截图

...

解决方案

原因:Elasticsearch7.4 及以上版本默认情况下不再支持「指定索引类型」,默认索引类型是「_doc」,详情见:「Removal of mapping types

如果要像之前旧版版本一样兼容自定义 type,需要设置 include_type_name=true ,详情见:「示例

PUT localhost:9200/people?include_type_name=true

注:这一字段可能将在「8.x」版本舍弃

Elasticsearch 自定义 Mapping 出错

问题描述

analyzer [ik] not found for field [xxx]

{
    "error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "analyzer [ik] not found for field [xxx]"
            }
        ]
    }
}

截图

...

解决方案

原因: 未安装对应的 IK 插件

  1. 下载对应版本的 IK 插件:「各版本 IK 插件列表

  2. 解压到 Elasticsearch 插件文件夹,路径:/opt/elasticsearch/plugins/ik

  3. 重启容器

集群健康值: yellow (5 of 10)

问题描述

集群健康值显示 yellow (5 of 10)

为什么集群状态为yellow

由于是部署的单节点 elasticsearch,默认的分片副本数目配置为 1,而相同的分片不能在一个节点上,所以就存在副本分片指定不明确的问题,所以显示为yellow。

截图

...

解决方案

  1. 可以通过在 elasticsearch 集群上添加一个节点来解决问题

  2. 可以删除指定不明确的副本分片(当然这不是一个好办法)但是作为测试和解决办法还是可以尝试的

    curl -XPUT "http://localhost:9200/_settings" -d' {  "number_of_replicas" : 0 } '
    {"acknowledged":true}

关于这个问题的进一步探讨:「聊一聊Elasticsearch的健康状态

补充

索引模板

{
    "mappings": {
        "company": {
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "oper": {
                    "type": "keyword"
                },
                "capi": {
                    "type": "float"
                },
                "start_time": {
                    "type": "date",
                    "format": "yyyy-MM-dd"
                },
                "email": {
                    "type": "keyword"
                },
                "contact": {
                    "type": "keyword"
                },
                "address": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "status": {
                    "type": "keyword"
                },
                "pic_url": {
                    "type": "keyword"
                },
                "tag": {
                    "type": "keyword"
                },
                "hash": {
                    "type": "keyword"
                },
                "coy_type": {
                    "type": "integer"
                },
                "credit": {
                    "type": "keyword"
                },
                "insured": {
                    "type": "integer"
                },
                "latitude": {
                    "type": "keyword"
                },
                "longitude": {
                    "type": "keyword"
                }
            }
        }
    }
}