Search code examples
elasticsearchelasticsearch-dsl

how to create dynamic mapping with elasticsearch_dsl


I have to create mapping which should look something like this.

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

I would normally do something like this

class RetailerProductGeneric(Document):
    product_id = Keyword()
    

how do I do it when I have a dynamic schema in the document ?

I looked at the docs but not very clear to me. It goes as follows:

class Post(Document):
    title = Text()

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

not sure what is happening here. Can someone please explain how to do it?

EDIT 1:

After some research, I figured how to create the dynamic mapping.

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

but now expected mapping turns out to be like this

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

what I am looking for is

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

What difference does it make? How Can I make the mapping as expected.


Solution

  • 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.