DEV Community

Cover image for Get OpenSearch mapping with Python
Laysa Uchoa
Laysa Uchoa

Posted on

4 1 1 1 1

Get OpenSearch mapping with Python

When no data structure is specified when loading the data, OpenSearch uses dynamic mapping to automatically detect the fields. To check the mapping definition of your data, OpenSearch client provides a function called get_mapping as shown:

    import pprint

    INDEX_NAME = 'epicurious-recipes'
    mapping_data = os_client.indices.get_mapping(INDEX_NAME)

    # Find index doc_type
    doc_type = list(mapping_data[INDEX_NAME]["mappings"].keys())[0]

    schema = mapping_data[INDEX_NAME]["mappings"][doc_type]
    fields =  list(schema.keys())
    pprint(fields)
    pprint(schema)
Enter fullscreen mode Exit fullscreen mode

You should be able to see the fields's output:


    ['calories',
    'categories',
    'date',
    'desc',
    'directions',
    'fat',
    'ingredients',
    'protein',
    'rating',
    'sodium',
    'title']
Enter fullscreen mode Exit fullscreen mode

And the mapping with the fields and their respective types.


        {'calories': {'type': 'float'},
         'categories': {'fields': {'keyword': {'ignore_above': 256, 'type': 'keyword'}},
                        'type': 'text'},
         'date': {'type': 'date'},
         'desc': {'fields': {'keyword': {'ignore_above': 256, 'type': 'keyword'}},
                  'type': 'text'},
         'directions': {'fields': {'keyword': {'ignore_above': 256, 'type': 'keyword'}},
                        'type': 'text'},
         'fat': {'type': 'float'},
         'ingredients': {'fields': {'keyword': {'ignore_above': 256,
                                                'type': 'keyword'}},
                         'type': 'text'},
         'protein': {'type': 'float'},
         'rating': {'type': 'float'},
         'sodium': {'type': 'float'},
         'title': {'fields': {'keyword': {'ignore_above': 256, 'type': 'keyword'}},
                   'type': 'text'}}

Enter fullscreen mode Exit fullscreen mode

Read more about OpenSearch mapping in the official OpenSearch documentation.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay