如何在Elasticsearch中为嵌套类型插入数据?

时间:2018-09-24 06:20:22

标签: django elasticsearch

我是Elasticsearch的新手。我在Django上使用了Elasticsearch。我成功创建了Elasticsearch索引,

{
"productcategories": {
    "aliases": {},
    "mappings": {
        "product_categories": {
            "properties": {
                "categoryName": {
                    "type": "text"
                },
                "subCategories": {
                    "type": "nested",
                    "properties": {
                        "partNumbers": {
                            "type": "nested"
                        },
                        "subCategoryName": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    },
    "settings": {
        "index": {
            "creation_date": "1537766526678",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "uuid": "k_8XilbrTn2jM03Djp1pXQ",
            "version": {
                "created": "6040099"
            },
            "provided_name": "productcategories"
        }
    }
}
}

我正在尝试使用格式插入数据,

data={
        "categoryName":'TestConnector',
        "subCategories": [
            {   
                'subCategoryName':'Connector1',
                'partNumbers':['HFBR-2412T','HFBR-2506AMZ']
            },
            {   
                'subCategoryName':'Connector2',
                'partNumbers':['HFBR-2422Z','HFBR-2412Z']
            }
        ]
        }

settings.ES_CLIENT.index(index = ProductCategories._meta.es_index_name,doc_type = ProductCategories._meta.es_type_name,body = data)

它给了我以下错误,

elasticsearch.exceptions.RequestError: RequestError(400, 'mapper_parsing_exception', 'object mapping for [subCategories.partNumbers] tried to parse field [null] as object, but found a concrete value')

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

此错误是由于您尝试索引的数组类型数据引起的。

您无需指定映射是数组。您可以使用方括号将任何映射视为数组:

 "partNumbers": ["HFBR-2412T", "HFBR-2506AMZ"]

那么您的映射应该是

{
    "mappings": {
        "product_categories": {
          "properties": {
                "categoryName": {
                    "type": "text"
                },
                "subCategories": {
                    "type": "nested",
                    "properties": {
                        "subCategoryName": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    }
}

答案 1 :(得分:0)

查看映射,您无需将.定义为partNumbers,因为它将是一个简单的字符串数组。

nested

参考: