【问题标题】:ElasticSearch :: Exception while ceating index and documentElasticSearch :: 在创建索引和文档时出现异常
【发布时间】:2019-01-30 18:03:45
【问题描述】:

我是 ElasticSearch 的新手,正在尝试执行他们主页中提到的示例,但我遇到了这个错误 -

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "unknown setting [index.mappings.employee.properties.age.type] please check that any required plugins are installed, or check the breaking changes documentation for removed settings"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "unknown setting [index.mappings.employee.properties.age.type] please check that any required plugins are installed, or check the breaking changes documentation for removed settings",
        "suppressed": [
            {
                "type": "illegal_argument_exception",
                "reason": "unknown setting [index.mappings.employee.properties.experience.type] please check that any required plugins are installed, or check the breaking changes documentation for removed settings"
            },
            {
                "type": "illegal_argument_exception",
                "reason": "unknown setting [index.mappings.employee.properties.name.analyzer] please check that any required plugins are installed, or check the breaking changes documentation for removed settings"
            },
            {
                "type": "illegal_argument_exception",
                "reason": "unknown setting [index.mappings.employee.properties.name.type] please check that any required plugins are installed, or check the breaking changes documentation for removed settings"
            }
        ]
    },
    "status": 400
}

post请求的url&body如下——

网址->http://localhost:9200/company 身体 - >

{
  "settings": {
    "index": {
       "number_of_shards": 1,
       "number_of_replicas": 1
    },
    "analysis": {
      "analyzer": {
        "analyzer-name": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": "lowercase"
        }
      }
    },
    "mappings": {
      "employee": {
        "properties": {
          "age": {
            "type": "long"
          },
          "experience": {
            "type": "long"      
          },
          "name": {
            "type": "string",
            "analyzer": "analyzer-name"
          }
        }
      }
    }
  }  
}

如何修复错误?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您的 JSON 正文对象的语法有两个错误:

    1. 节点settings 必须只有两个子节点:indexanalysis。节点 mappings 必须是根级别的。
    2. 字段name 的类型无效string,它必须是textkeyword。因为你需要分析这个字段,所以在你的情况下应该是text

    所以 ES 版本 6.x 的工作查询(在提出问题时是最新的)应该是这样的:

    {
      "settings": {
        "index": {
           "number_of_shards": 1,
           "number_of_replicas": 1
        },
        "analysis": {
          "analyzer": {
            "analyzer-name": {
              "type": "custom",
              "tokenizer": "keyword",
              "filter": "lowercase"
            }
          }
        }
      },
      "mappings": {
        "employee": {
          "properties": {
            "age": {
              "type": "long"
            },
            "experience": {
              "type": "long"      
            },
            "name": {
              "type": "text",
              "analyzer": "analyzer-name"
            }
          }
        }
      }
    }
    

    从 ES 版本 7.0 开始,从索引定义映射类型 was removed,因此上面的查询在 ES 7.x 中不起作用。

    适用于 ES 版本 7.x 的工作查询可能有两种类型:

    1. 如果索引应该只包含有关员工的数据,您可以简单地删除employee 映射类型,查询将是这样的:

      {
        "settings": {
          "index": {
            "number_of_shards": 1,
            "number_of_replicas": 1
          },
          "analysis": {
            "analyzer": {
              "analyzer-name": {
                "type": "custom",
                "tokenizer": "keyword",
                "filter": "lowercase"
              }
            }
          }
        },
        "mappings": {
          "properties": {
            "age": {
              "type": "long"
            },
            "experience": {
              "type": "long"      
            },
            "name": {
              "type": "text",
              "analyzer": "analyzer-name"
            }
          }
        }
      }
      
    2. 如果索引应该包含有关员工的数据和其他一些数据,您可以使用employee 作为object type 的字段,查询将是这样的:

      {
        "settings": {
          "index": {
            "number_of_shards": 1,
            "number_of_replicas": 1
          },
          "analysis": {
            "analyzer": {
              "analyzer-name": {
                "type": "custom",
                "tokenizer": "keyword",
                "filter": "lowercase"
              }
            }
          }
        },
        "mappings": {
          "properties": {
            "employee": {
              "properties": {
                "age": {
                  "type": "long"
                },
                "experience": {
                  "type": "long"      
                },
                "name": {
                  "type": "text",
                  "analyzer": "analyzer-name"
                }
              }
            }
          }
        }
      }
      

    【讨论】:

    • @user3898336,你使用什么版本的 Elasticsearch?我在6.* 上验证了我的答案(在撰写答案时是相关的)并且它有效,但现在相关版本是7.*,并且这些版本之间存在许多重大变化。
    • 嗨@SameeraDeSilva,我用 ES 版本 7.x 的示例更新了我的答案。由于breaking changes 在 6.x 和 7.x 之间,原始示例在 7.x 中不起作用。
    【解决方案2】:
    As you mentioned that you are new to Elastic Search, better start with the basic and use the default settings of ElasticSearch. Use the following mapping:
    
    curl -XPUT localhost:9200/company -d '{
        "mappings": {
            "employee": {
                "properties": {
                    "age": {"type": "long"},
                    "experience": {"type": "long"},
                    "name": {"type": "string","index": "not_analyzed"}
                }
            }
        }
    }'
    

    【讨论】:

    • 请注意,您的示例仅适用于 5.0 之前的 Elasticsearch 版本,因为语法中的 breaking changes - 数据类型 string 被拆分为 textkeyword 数据类型。
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多