使用弹性搜索6.1.1的mapper_parsing_exception

时间:2019-06-02 15:07:36

标签: python elasticsearch

我正在尝试运行以下代码并获取'mapper_parsing_exception'异常。此示例摘自Elasticsearch 6.1.1的文档

from elasticsearch import Elasticsearch
import logging
import sys

logger = logging.getLogger(__name__)
es_host = {'host': 'localhost', 'port': 9200}
elastic_con = Elasticsearch(hosts=[es_host])

mapping = '''
{
    "mappings": {
        "article": {
            "properties": {
                "id": { "type": "text" },
                "title":  { "type": "text"},
                "abstract": { "type": "text"},
                "author": {
                    "properties": {
                        "id": { "type": "text" },
                        "name": { "type": "text" }
                    }
                }
            }
        }
    }
}
'''
res = elastic_con.indices.create(index='test-index', ignore=400, body=mapping)

if 'error' in res and res['status'] == 400:
    # NOTE: Illegal argument errors are also being masked here, so test the index creation
    error_type = res['error']['root_cause'][0]['type']
    if error_type == 'resource_already_exists_exception':
        logger.debug("Index already exists")
    else:
        logger.error("Error Occurred in Index creation:{0}".format(res))
        print("\n -- Unable to create Index:"+error_type+"--\n")
        sys.exit(1)
elif res['acknowledged'] and res['index'] == __index_name__:
    logger.debug("Index Created")
else:
    logger.error("Index creation failed:{0}".format(res))
    print("\n -- Unable to create Index--\n")
    sys.exit(1)

错误如下:

{
    "error":
        {"root_cause":[{
            "type":"mapper_parsing_exception",
            "reason":"Root mapping definition has unsupported parameters:  [article : {properties={author={properties={name={type=text}, id={type=text}}}, id={type=text}, abstract={type=text}, title={type=text}}}]"}],
        "type":"mapper_parsing_exception",
        "reason":"Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:  [article : {properties={author={properties={name={type=text}, id={type=text}}}, id={type=text}, abstract={type=text}, title={type=text}}}]",
        "caused_by":{"type":"mapper_parsing_exception",
        "reason":"Root mapping definition has unsupported parameters:  [article : {properties={author={properties={name={type=text}, id={type=text}}}, id={type=text}, abstract={type=text}, title={type=text}}}]"}
        },
    "status":400
}

任何帮助将不胜感激! :D

2 个答案:

答案 0 :(得分:0)

如果属性中的任何字段是一个对象, 像这里的“作者” 那么您必须在映射中使用“ type”:“ nested” 像这样...

mapping = '''
{
    "mappings": {
        "article": {
            "properties": {
                "id": { "type": "text" },
                "title":  { "type": "text"},
                "abstract": { "type": "text"},
                "author": {
                    "type": "nested",
                    "properties": {
                        "id": { "type": "text" },
                        "name": { "type": "text" }
                    }
                }
            }
        }
    }
}
'''

答案 1 :(得分:0)

似乎您使用的是Elasticsearch版本7.x,该版本不再支持type。因此,请从映射中删除article并按以下方式使用:

{
  "mappings": {
    "properties": {
      "id": {
        "type": "text"
      },
      "title": {
        "type": "text"
      },
      "abstract": {
        "type": "text"
      },
      "author": {
        "properties": {
          "id": {
            "type": "text"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}