首页 > 解决方案 > 如何使用 elasticsearch_dsl 创建动态映射

问题描述

我必须创建应该看起来像这样的映射。

{
  "mappings": {
    "properties": {
        "product_id": {
            "type": "keyword"
        },
        "attributes": {
            "dynamic": "true",
            "properties": {}
        },
    }
}   

我通常会做这样的事情

class RetailerProductGeneric(Document):
    product_id = Keyword()
    

当我在文档中有动态模式时,我该怎么做?

我查看了文档,但对我来说不是很清楚。它是这样的:

class Post(Document):
    title = Text()

    class Meta:
        all = MetaField(enabled=False)
        dynamic = MetaField('strict')

不知道这里发生了什么。有人可以解释一下怎么做吗?

编辑1:

经过一番研究,我想出了如何创建动态映射。

class RetailerProductGeneric(Document):
    product_id = Keyword()
    attributes = Object(dynamic=True)

但现在预期的映射结果是这样的

  "attributes": {
    "dynamic": true,
    "type": "object"
  },

我正在寻找的是

  "attributes": {
    "dynamic": "true",
    "properties": {}
  },

它有什么区别?如何按预期进行映射。

标签: elasticsearchelasticsearch-dsl

解决方案


Good start!!

"attributes": {
  "dynamic": true,
  "type": "object"
},

and

"attributes": {
  "dynamic": "true",
  "properties": {}
},

are actually exactly the same thing.

When type: object is specified, properties: {} is implied

And when properties: {} is specified and no type is given, then type: object is implied.


推荐阅读