<commit_before>[[search-aggregations-bucket-terms-aggregation]]
=== Terms Aggregation

A multi-bucket value source based aggregation where buckets are dynamically built - one per unique value.

//////////////////////////

[source,js]
--------------------------------------------------
PUT /products
{
    "mappings": {
        "product": {
            "properties": {
                "genre": {
                    "type": "keyword"
                },
                "product": {
                    "type": "keyword"
                }
            }
        }
    }
}

POST /products/product/_bulk?refresh
{"index":{"_id":0}}
{"genre": "rock", "product": "Product A"}
{"index":{"_id":1}}
{"genre": "rock"}
{"index":{"_id":2}}
{"genre": "rock"}
{"index":{"_id":3}}
{"genre": "jazz", "product": "Product Z"}
{"index":{"_id":4}}
{"genre": "jazz"}
{"index":{"_id":5}}
{"genre": "electronic"}
{"index":{"_id":6}}
{"genre": "electronic"}
{"index":{"_id":7}}
{"genre": "electronic"}
{"index":{"_id":8}}
{"genre": "electronic"}
{"index":{"_id":9}}
{"genre": "electronic"}
{"index":{"_id":10}}
{"genre": "electronic"}

-------------------------------------------------
// NOTCONSOLE
// TESTSETUP

//////////////////////////

Example:

[source,js]
--------------------------------------------------
GET /_search
{
    "aggs" : {
        "genres" : {
            "terms" : { "field" : "genre" }
        }
    }
}
--------------------------------------------------
// CONSOLE
// TEST[s/_search/_search\?filter_path=aggregations/]

Response:

[source,js]
--------------------------------------------------
{<commit_msg>Improved Terms Aggregation documentation (#38892)<commit_after>[[search-aggregations-bucket-terms-aggregation]]
=== Terms Aggregation

A multi-bucket value source based aggregation where buckets are dynamically built - one per unique value.

//////////////////////////

[source,js]
--------------------------------------------------
PUT /products
{
    "mappings": {
        "product": {
            "properties": {
                "genre": {
                    "type": "keyword"
                },
                "product": {
                    "type": "keyword"
                }
            }
        }
    }
}

POST /products/product/_bulk?refresh
{"index":{"_id":0}}
{"genre": "rock", "product": "Product A"}
{"index":{"_id":1}}
{"genre": "rock"}
{"index":{"_id":2}}
{"genre": "rock"}
{"index":{"_id":3}}
{"genre": "jazz", "product": "Product Z"}
{"index":{"_id":4}}
{"genre": "jazz"}
{"index":{"_id":5}}
{"genre": "electronic"}
{"index":{"_id":6}}
{"genre": "electronic"}
{"index":{"_id":7}}
{"genre": "electronic"}
{"index":{"_id":8}}
{"genre": "electronic"}
{"index":{"_id":9}}
{"genre": "electronic"}
{"index":{"_id":10}}
{"genre": "electronic"}

-------------------------------------------------
// NOTCONSOLE
// TESTSETUP

//////////////////////////

Example:

[source,js]
--------------------------------------------------
GET /_search
{
    "aggs" : {
        "genres" : {
            "terms" : { "field" : "genre" } <1>
        }
    }
}
--------------------------------------------------
// CONSOLE
// TEST[s/_search/_search\?filter_path=aggregations/]
<1> `terms` aggregation should be a field of type `keyword` or any other data type suitable for bucket aggregations. In order to use it with `text` you will need to enable 
<<fielddata, fielddata>>.

Response:

[source,js]
--------------------------------------------------
{
{'fraction_non_alphanumeric': 0.35968722849695917, 'fraction_numerical': 0.008977700550246163, 'mean_word_length': 1.5892053973013494, 'pattern_counts': {'":': 84, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3018178', 'n_tokens_mistral': 1063, 'n_tokens_neox': 989, 'n_words': 210}