DEV Community

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

Posted on

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.

Top comments (0)