| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 | [[search-aggregations-bucket-categorize-text-aggregation]]=== Categorize text aggregation++++<titleabbrev>Categorize text</titleabbrev>++++experimental::[]A multi-bucket aggregation that groups semi-structured text into buckets. Each `text` field is re-analyzedusing a custom analyzer. The resulting tokens are then categorized creating buckets of similarly formattedtext values. This aggregation works best with machine generated text like system logs. Only the first 100 analyzedtokens are used to categorize the text.NOTE: If you have considerable memory allocated to your JVM but are receiving circuit breaker exceptions from this      aggregation, you may be attempting to categorize text that is poorly formatted for categorization. Consider      adding `categorization_filters` or running under <<search-aggregations-bucket-sampler-aggregation,sampler>>,      <<search-aggregations-bucket-diversified-sampler-aggregation,diversified sampler>>, or      <<search-aggregations-random-sampler-aggregation,random sampler>> to explore the created categories.[[bucket-categorize-text-agg-syntax]]==== Parameters`field`::(Required, string)The semi-structured text field to categorize.`max_unique_tokens`::(Optional, integer, default: `50`)The maximum number of unique tokens at any position up to `max_matched_tokens`.Must be larger than 1. Smaller values use less memory and create fewer categories.Larger values will use more memory and create narrower categories.Max allowed value is `100`.`max_matched_tokens`::(Optional, integer, default: `5`)The maximum number of token positions to match on before attempting to merge categories.Larger values will use more memory and create narrower categories.Max allowed value is `100`.Example:`max_matched_tokens` of 2 would disallow merging of the categories[`foo` `bar` `baz`][`foo` `baz` `bozo`]As the first 2 tokens are required to match for the category.NOTE: Once `max_unique_tokens` is reached at a given position, a new `*` token isadded and all new tokens at that position are matched by the `*` token.`similarity_threshold`::(Optional, integer, default: `50`)The minimum percentage of tokens that must match for text to be added to thecategory bucket.Must be between 1 and 100. The larger the value the narrower the categories.Larger values will increase memory usage and create narrower categories.`categorization_filters`::(Optional, array of strings)This property expects an array of regular expressions. The expressionsare used to filter out matching sequences from the categorization field values.You can use this functionality to fine tune the categorization by excludingsequences from consideration when categories are defined. For example, you canexclude SQL statements that appear in your log files. Thisproperty cannot be used at the same time as `categorization_analyzer`. If youonly want to define simple regular expression filters that are applied prior totokenization, setting this property is the easiest method. If you also want tocustomize the tokenizer or post-tokenization filtering, use the`categorization_analyzer` property instead and include the filters as`pattern_replace` character filters.`categorization_analyzer`::(Optional, object or string)The categorization analyzer specifies how the text is analyzed and tokenized beforebeing categorized. The syntax is very similar to that used to define the `analyzer` in the<<indices-analyze,Analyze endpoint>>. Thisproperty cannot be used at the same time as `categorization_filters`.+The `categorization_analyzer` field can be specified either as a string or as anobject. If it is a string it must refer to a<<analysis-analyzers,built-in analyzer>> or one added by another plugin. If itis an object it has the following properties:+.Properties of `categorization_analyzer`[%collapsible%open]=====`char_filter`::::(array of strings or objects)include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=char-filter]`tokenizer`::::(string or object)include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=tokenizer]`filter`::::(array of strings or objects)include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=filter]=====`shard_size`::(Optional, integer)The number of categorization buckets to return from each shard before mergingall the results.`size`::(Optional, integer, default: `10`)The number of buckets to return.`min_doc_count`::(Optional, integer)The minimum number of documents for a bucket to be returned to the results.`shard_min_doc_count`::(Optional, integer)The minimum number of documents for a bucket to be returned from the shard beforemerging.==== Basic useWARNING: Re-analyzing _large_ result sets will require a lot of time and memory. This aggregation should be         used in conjunction with <<async-search, Async search>>. Additionally, you may consider         using the aggregation as a child of either the <<search-aggregations-bucket-sampler-aggregation,sampler>> or         <<search-aggregations-bucket-diversified-sampler-aggregation,diversified sampler>> aggregation.         This will typically improve speed and memory use.Example:[source,console]--------------------------------------------------POST log-messages/_search?filter_path=aggregations{  "aggs": {    "categories": {      "categorize_text": {        "field": "message"      }    }  }}--------------------------------------------------// TEST[setup:categorize_text]Response:[source,console-result]--------------------------------------------------{  "aggregations" : {    "categories" : {      "buckets" : [        {          "doc_count" : 3,          "key" : "Node shutting down"        },        {          "doc_count" : 1,          "key" : "Node starting up"        },        {          "doc_count" : 1,          "key" : "User foo_325 logging on"        },        {          "doc_count" : 1,          "key" : "User foo_864 logged off"        }      ]    }  }}--------------------------------------------------Here is an example using `categorization_filters`[source,console]--------------------------------------------------POST log-messages/_search?filter_path=aggregations{  "aggs": {    "categories": {      "categorize_text": {        "field": "message",        "categorization_filters": ["\\w+\\_\\d{3}"] <1>      }    }  }}--------------------------------------------------// TEST[setup:categorize_text]<1> The filters to apply to the analyzed tokens. It filters    out tokens like `bar_123`.Note how the `foo_<number>` tokens are not part of thecategory results[source,console-result]--------------------------------------------------{  "aggregations" : {    "categories" : {      "buckets" : [        {          "doc_count" : 3,          "key" : "Node shutting down"        },        {          "doc_count" : 1,          "key" : "Node starting up"        },        {          "doc_count" : 1,          "key" : "User logged off"        },        {          "doc_count" : 1,          "key" : "User logging on"        }      ]    }  }}--------------------------------------------------Here is an example using `categorization_filters`.The default analyzer is a whitespace analyzer with a custom token filterwhich filters out tokens that start with any number.But, it may be that a token is a known highly-variable token (formatted usernames, emails, etc.). In that case, it is good to supplycustom `categorization_filters` to filter out those tokens for better categories. These filters will also reduce memory usage as fewertokens are held in memory for the categories.[source,console]--------------------------------------------------POST log-messages/_search?filter_path=aggregations{  "aggs": {    "categories": {      "categorize_text": {        "field": "message",        "categorization_filters": ["\\w+\\_\\d{3}"], <1>        "max_matched_tokens": 2, <2>        "similarity_threshold": 30 <3>      }    }  }}--------------------------------------------------// TEST[setup:categorize_text]<1> The filters to apply to the analyzed tokens. It filtersout tokens like `bar_123`.<2> Require at least 2 tokens before the log categories attempt to merge together<3> Require 30% of the tokens to match before expanding a log categories    to add a new log entryThe resulting categories are now broad, matching the first tokenand merging the log groups.[source,console-result]--------------------------------------------------{  "aggregations" : {    "categories" : {      "buckets" : [        {          "doc_count" : 4,          "key" : "Node *"        },        {          "doc_count" : 2,          "key" : "User *"        }      ]    }  }}--------------------------------------------------This aggregation can have both sub-aggregations and itself be a sub-aggregation. This allows gathering the top daily categories and thetop sample doc as below.[source,console]--------------------------------------------------POST log-messages/_search?filter_path=aggregations{  "aggs": {    "daily": {      "date_histogram": {        "field": "time",        "fixed_interval": "1d"      },      "aggs": {        "categories": {          "categorize_text": {            "field": "message",            "categorization_filters": ["\\w+\\_\\d{3}"]          },          "aggs": {            "hit": {              "top_hits": {                "size": 1,                "sort": ["time"],                "_source": "message"              }            }          }        }      }    }  }}--------------------------------------------------// TEST[setup:categorize_text][source,console-result]--------------------------------------------------{  "aggregations" : {    "daily" : {      "buckets" : [        {          "key_as_string" : "2016-02-07T00:00:00.000Z",          "key" : 1454803200000,          "doc_count" : 3,          "categories" : {            "buckets" : [              {                "doc_count" : 2,                "key" : "Node shutting down",                "hit" : {                  "hits" : {                    "total" : {                      "value" : 2,                      "relation" : "eq"                    },                    "max_score" : null,                    "hits" : [                      {                        "_index" : "log-messages",                        "_id" : "1",                        "_score" : null,                        "_source" : {                          "message" : "2016-02-07T00:00:00+0000 Node 3 shutting down"                        },                        "sort" : [                          1454803260000                        ]                      }                    ]                  }                }              },              {                "doc_count" : 1,                "key" : "Node starting up",                "hit" : {                  "hits" : {                    "total" : {                      "value" : 1,                      "relation" : "eq"                    },                    "max_score" : null,                    "hits" : [                      {                        "_index" : "log-messages",                        "_id" : "2",                        "_score" : null,                        "_source" : {                          "message" : "2016-02-07T00:00:00+0000 Node 5 starting up"                        },                        "sort" : [                          1454803320000                        ]                      }                    ]                  }                }              }            ]          }        },        {          "key_as_string" : "2016-02-08T00:00:00.000Z",          "key" : 1454889600000,          "doc_count" : 3,          "categories" : {            "buckets" : [              {                "doc_count" : 1,                "key" : "Node shutting down",                "hit" : {                  "hits" : {                    "total" : {                      "value" : 1,                      "relation" : "eq"                    },                    "max_score" : null,                    "hits" : [                      {                        "_index" : "log-messages",                        "_id" : "4",                        "_score" : null,                        "_source" : {                          "message" : "2016-02-08T00:00:00+0000 Node 5 shutting down"                        },                        "sort" : [                          1454889660000                        ]                      }                    ]                  }                }              },              {                "doc_count" : 1,                "key" : "User logged off",                "hit" : {                  "hits" : {                    "total" : {                      "value" : 1,                      "relation" : "eq"                    },                    "max_score" : null,                    "hits" : [                      {                        "_index" : "log-messages",                        "_id" : "6",                        "_score" : null,                        "_source" : {                          "message" : "2016-02-08T00:00:00+0000 User foo_864 logged off"                        },                        "sort" : [                          1454889840000                        ]                      }                    ]                  }                }              },              {                "doc_count" : 1,                "key" : "User logging on",                "hit" : {                  "hits" : {                    "total" : {                      "value" : 1,                      "relation" : "eq"                    },                    "max_score" : null,                    "hits" : [                      {                        "_index" : "log-messages",                        "_id" : "5",                        "_score" : null,                        "_source" : {                          "message" : "2016-02-08T00:00:00+0000 User foo_325 logging on"                        },                        "sort" : [                          1454889720000                        ]                      }                    ]                  }                }              }            ]          }        }      ]    }  }}--------------------------------------------------
 |