// This file is autogenerated, DO NOT EDIT
// aggregations/metrics/top-metrics-aggregation.asciidoc:154

[source, python]
----
resp = client.indices.create(
    index="my-index",
    mappings={
        "properties": {
            "nr": {
                "type": "integer"
            },
            "state": {
                "type": "keyword"
            }
        }
    },
)
print(resp)

resp1 = client.bulk(
    index="my-index",
    refresh=True,
    operations=[
        {
            "index": {}
        },
        {
            "nr": 1,
            "state": "started"
        },
        {
            "index": {}
        },
        {
            "nr": 2,
            "state": "stopped"
        },
        {
            "index": {}
        },
        {
            "nr": 3,
            "state": "N/A"
        },
        {
            "index": {}
        },
        {
            "nr": 4
        }
    ],
)
print(resp1)

resp2 = client.search(
    index="my-index",
    filter_path="aggregations",
    aggs={
        "my_top_metrics": {
            "top_metrics": {
                "metrics": {
                    "field": "state",
                    "missing": "N/A"
                },
                "sort": {
                    "nr": "desc"
                }
            }
        }
    },
)
print(resp2)
----
