ElasticSearch::Exception 同时创建索引和文档

标签 elasticsearch

我是 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 body ->

{
  "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"
          }
        }
      }
    }
  }  
}

如何修复错误?

最佳答案

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

  1. 节点 settings 必须只有两个 child :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"
              }
            }
          }
        }
      }
    }
    

关于ElasticSearch::Exception 同时创建索引和文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52001376/

相关文章:

java - 如何启动嵌入到我的 java 应用程序中的 elasticsearch 5.1?

symfony - 调试失败的Elasticsearch查询

java - 有效地将oracle表数据缓存到 Elasticsearch 数据库中

python - 使用 python 从 JSON 文件中删除一些特定对象,然后将文件推送到 Elasticsearch Index

elasticsearch - ElasticSearch在搜索结果的末尾附加不匹配的文档

elasticsearch - 是否可以从非存储的Lucene重构数据?

elasticsearch - 休眠搜索:Elasticsearch和Lucene产生不同的搜索结果

java - 枚举作为映射键转换在 Spring Boot 2.4.1 和 Elastic Search 7.10 中不起作用

elasticsearch - 哪种最佳做法是在python3中的混合编码文本中跳过非ascii字符?

algorithm - 当段落包含Elasticsearch索引中的句子时匹配