Elasticsearch-dsl-py: How do I properly create a dynamic template for my index?

Created on 4 Aug 2017  路  1Comment  路  Source: elastic/elasticsearch-dsl-py

This documentation is quite confusing. I assumed I could create a dynamic template as outlined here: https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html via the Index.create() method but for the life of me I'm not sure how I'm supposed to pass in the body correctly.

Here is some sample code:

dynamic_template = {
            "mappings": {
                "page": {
                    "dynamic_templates": [
                        {
                            "strings": {
                                "match_mapping_type": "string",
                                "mapping": {
                                    "type": "text",
                                    "term_vector": "with_positions_offsets",
                                    "fields": {
                                        "raw": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }


index = Index(name='my_index_name', using=myhost)
index.create(body=dyanmic_template)  #  this is obviously wrong...

The last line is obviously wrong. I'm not sure what I'm supposed to pass in to the create call. Could I not pass doc_type= 'page' and body=dynamic_template? I get an error saying the Index.create() retrieves multiple body kwargs (since create passes itself as a dict as the body...). What's the proper way to set my mapping??

Most helpful comment

Currently there is no direct support for dynamic_templates and you have to use the raw dicts to specify those:

class MyDocType(DocType):
    class Meta:
        dynamic_templates = MetaField([
                        {
                            "strings": {
                                "match_mapping_type": "string",
                                "mapping": {
                                    "type": "text",
                                    "term_vector": "with_positions_offsets",
                                    "fields": {
                                        "raw": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
        ])

Hope this helps!

>All comments

Currently there is no direct support for dynamic_templates and you have to use the raw dicts to specify those:

class MyDocType(DocType):
    class Meta:
        dynamic_templates = MetaField([
                        {
                            "strings": {
                                "match_mapping_type": "string",
                                "mapping": {
                                    "type": "text",
                                    "term_vector": "with_positions_offsets",
                                    "fields": {
                                        "raw": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
        ])

Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abuzakaria picture abuzakaria  路  4Comments

gabrielpjordao picture gabrielpjordao  路  3Comments

primoz-k picture primoz-k  路  4Comments

rokcarl picture rokcarl  路  4Comments

ypkkhatri picture ypkkhatri  路  4Comments