categorize-text-aggregation.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. [[search-aggregations-bucket-categorize-text-aggregation]]
  2. === Categorize text aggregation
  3. ++++
  4. <titleabbrev>Categorize text</titleabbrev>
  5. ++++
  6. experimental::[]
  7. A multi-bucket aggregation that groups semi-structured text into buckets. Each `text` field is re-analyzed
  8. using a custom analyzer. The resulting tokens are then categorized creating buckets of similarly formatted
  9. text values. This aggregation works best with machine generated text like system logs. Only the first 100 analyzed
  10. tokens are used to categorize the text.
  11. NOTE: If you have considerable memory allocated to your JVM but are receiving circuit breaker exceptions from this
  12. aggregation, you may be attempting to categorize text that is poorly formatted for categorization. Consider
  13. adding `categorization_filters` or running under <<search-aggregations-bucket-sampler-aggregation,sampler>> or
  14. <<search-aggregations-bucket-diversified-sampler-aggregation,diversified sampler>> to explore the created categories.
  15. [[bucket-categorize-text-agg-syntax]]
  16. ==== Parameters
  17. `field`::
  18. (Required, string)
  19. The semi-structured text field to categorize.
  20. `max_unique_tokens`::
  21. (Optional, integer, default: `50`)
  22. The maximum number of unique tokens at any position up to `max_matched_tokens`.
  23. Must be larger than 1. Smaller values use less memory and create fewer categories.
  24. Larger values will use more memory and create narrower categories.
  25. Max allowed value is `100`.
  26. `max_matched_tokens`::
  27. (Optional, integer, default: `5`)
  28. The maximum number of token positions to match on before attempting to merge categories.
  29. Larger values will use more memory and create narrower categories.
  30. Max allowed value is `100`.
  31. Example:
  32. `max_matched_tokens` of 2 would disallow merging of the categories
  33. [`foo` `bar` `baz`]
  34. [`foo` `baz` `bozo`]
  35. As the first 2 tokens are required to match for the category.
  36. NOTE: Once `max_unique_tokens` is reached at a given position, a new `*` token is
  37. added and all new tokens at that position are matched by the `*` token.
  38. `similarity_threshold`::
  39. (Optional, integer, default: `50`)
  40. The minimum percentage of tokens that must match for text to be added to the
  41. category bucket.
  42. Must be between 1 and 100. The larger the value the narrower the categories.
  43. Larger values will increase memory usage and create narrower categories.
  44. `categorization_filters`::
  45. (Optional, array of strings)
  46. This property expects an array of regular expressions. The expressions
  47. are used to filter out matching sequences from the categorization field values.
  48. You can use this functionality to fine tune the categorization by excluding
  49. sequences from consideration when categories are defined. For example, you can
  50. exclude SQL statements that appear in your log files. This
  51. property cannot be used at the same time as `categorization_analyzer`. If you
  52. only want to define simple regular expression filters that are applied prior to
  53. tokenization, setting this property is the easiest method. If you also want to
  54. customize the tokenizer or post-tokenization filtering, use the
  55. `categorization_analyzer` property instead and include the filters as
  56. `pattern_replace` character filters.
  57. `categorization_analyzer`::
  58. (Optional, object or string)
  59. The categorization analyzer specifies how the text is analyzed and tokenized before
  60. being categorized. The syntax is very similar to that used to define the `analyzer` in the
  61. <<indices-analyze,Analyze endpoint>>. This
  62. property cannot be used at the same time as `categorization_filters`.
  63. +
  64. The `categorization_analyzer` field can be specified either as a string or as an
  65. object. If it is a string it must refer to a
  66. <<analysis-analyzers,built-in analyzer>> or one added by another plugin. If it
  67. is an object it has the following properties:
  68. +
  69. .Properties of `categorization_analyzer`
  70. [%collapsible%open]
  71. =====
  72. `char_filter`::::
  73. (array of strings or objects)
  74. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=char-filter]
  75. `tokenizer`::::
  76. (string or object)
  77. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=tokenizer]
  78. `filter`::::
  79. (array of strings or objects)
  80. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=filter]
  81. =====
  82. `shard_size`::
  83. (Optional, integer)
  84. The number of categorization buckets to return from each shard before merging
  85. all the results.
  86. `size`::
  87. (Optional, integer, default: `10`)
  88. The number of buckets to return.
  89. `min_doc_count`::
  90. (Optional, integer)
  91. The minimum number of documents for a bucket to be returned to the results.
  92. `shard_min_doc_count`::
  93. (Optional, integer)
  94. The minimum number of documents for a bucket to be returned from the shard before
  95. merging.
  96. ==== Basic use
  97. WARNING: Re-analyzing _large_ result sets will require a lot of time and memory. This aggregation should be
  98. used in conjunction with <<async-search, Async search>>. Additionally, you may consider
  99. using the aggregation as a child of either the <<search-aggregations-bucket-sampler-aggregation,sampler>> or
  100. <<search-aggregations-bucket-diversified-sampler-aggregation,diversified sampler>> aggregation.
  101. This will typically improve speed and memory use.
  102. Example:
  103. [source,console]
  104. --------------------------------------------------
  105. POST log-messages/_search?filter_path=aggregations
  106. {
  107. "aggs": {
  108. "categories": {
  109. "categorize_text": {
  110. "field": "message"
  111. }
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // TEST[setup:categorize_text]
  117. Response:
  118. [source,console-result]
  119. --------------------------------------------------
  120. {
  121. "aggregations" : {
  122. "categories" : {
  123. "buckets" : [
  124. {
  125. "doc_count" : 3,
  126. "key" : "Node shutting down"
  127. },
  128. {
  129. "doc_count" : 1,
  130. "key" : "Node starting up"
  131. },
  132. {
  133. "doc_count" : 1,
  134. "key" : "User foo_325 logging on"
  135. },
  136. {
  137. "doc_count" : 1,
  138. "key" : "User foo_864 logged off"
  139. }
  140. ]
  141. }
  142. }
  143. }
  144. --------------------------------------------------
  145. Here is an example using `categorization_filters`
  146. [source,console]
  147. --------------------------------------------------
  148. POST log-messages/_search?filter_path=aggregations
  149. {
  150. "aggs": {
  151. "categories": {
  152. "categorize_text": {
  153. "field": "message",
  154. "categorization_filters": ["\\w+\\_\\d{3}"] <1>
  155. }
  156. }
  157. }
  158. }
  159. --------------------------------------------------
  160. // TEST[setup:categorize_text]
  161. <1> The filters to apply to the analyzed tokens. It filters
  162. out tokens like `bar_123`.
  163. Note how the `foo_<number>` tokens are not part of the
  164. category results
  165. [source,console-result]
  166. --------------------------------------------------
  167. {
  168. "aggregations" : {
  169. "categories" : {
  170. "buckets" : [
  171. {
  172. "doc_count" : 3,
  173. "key" : "Node shutting down"
  174. },
  175. {
  176. "doc_count" : 1,
  177. "key" : "Node starting up"
  178. },
  179. {
  180. "doc_count" : 1,
  181. "key" : "User logged off"
  182. },
  183. {
  184. "doc_count" : 1,
  185. "key" : "User logging on"
  186. }
  187. ]
  188. }
  189. }
  190. }
  191. --------------------------------------------------
  192. Here is an example using `categorization_filters`.
  193. The default analyzer is a whitespace analyzer with a custom token filter
  194. which filters out tokens that start with any number.
  195. But, it may be that a token is a known highly-variable token (formatted usernames, emails, etc.). In that case, it is good to supply
  196. custom `categorization_filters` to filter out those tokens for better categories. These filters will also reduce memory usage as fewer
  197. tokens are held in memory for the categories.
  198. [source,console]
  199. --------------------------------------------------
  200. POST log-messages/_search?filter_path=aggregations
  201. {
  202. "aggs": {
  203. "categories": {
  204. "categorize_text": {
  205. "field": "message",
  206. "categorization_filters": ["\\w+\\_\\d{3}"], <1>
  207. "max_matched_tokens": 2, <2>
  208. "similarity_threshold": 30 <3>
  209. }
  210. }
  211. }
  212. }
  213. --------------------------------------------------
  214. // TEST[setup:categorize_text]
  215. <1> The filters to apply to the analyzed tokens. It filters
  216. out tokens like `bar_123`.
  217. <2> Require at least 2 tokens before the log categories attempt to merge together
  218. <3> Require 30% of the tokens to match before expanding a log categories
  219. to add a new log entry
  220. The resulting categories are now broad, matching the first token
  221. and merging the log groups.
  222. [source,console-result]
  223. --------------------------------------------------
  224. {
  225. "aggregations" : {
  226. "categories" : {
  227. "buckets" : [
  228. {
  229. "doc_count" : 4,
  230. "key" : "Node *"
  231. },
  232. {
  233. "doc_count" : 2,
  234. "key" : "User *"
  235. }
  236. ]
  237. }
  238. }
  239. }
  240. --------------------------------------------------
  241. This aggregation can have both sub-aggregations and itself be a sub-aggregation. This allows gathering the top daily categories and the
  242. top sample doc as below.
  243. [source,console]
  244. --------------------------------------------------
  245. POST log-messages/_search?filter_path=aggregations
  246. {
  247. "aggs": {
  248. "daily": {
  249. "date_histogram": {
  250. "field": "time",
  251. "fixed_interval": "1d"
  252. },
  253. "aggs": {
  254. "categories": {
  255. "categorize_text": {
  256. "field": "message",
  257. "categorization_filters": ["\\w+\\_\\d{3}"]
  258. },
  259. "aggs": {
  260. "hit": {
  261. "top_hits": {
  262. "size": 1,
  263. "sort": ["time"],
  264. "_source": "message"
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. --------------------------------------------------
  274. // TEST[setup:categorize_text]
  275. [source,console-result]
  276. --------------------------------------------------
  277. {
  278. "aggregations" : {
  279. "daily" : {
  280. "buckets" : [
  281. {
  282. "key_as_string" : "2016-02-07T00:00:00.000Z",
  283. "key" : 1454803200000,
  284. "doc_count" : 3,
  285. "categories" : {
  286. "buckets" : [
  287. {
  288. "doc_count" : 2,
  289. "key" : "Node shutting down",
  290. "hit" : {
  291. "hits" : {
  292. "total" : {
  293. "value" : 2,
  294. "relation" : "eq"
  295. },
  296. "max_score" : null,
  297. "hits" : [
  298. {
  299. "_index" : "log-messages",
  300. "_id" : "1",
  301. "_score" : null,
  302. "_source" : {
  303. "message" : "2016-02-07T00:00:00+0000 Node 3 shutting down"
  304. },
  305. "sort" : [
  306. 1454803260000
  307. ]
  308. }
  309. ]
  310. }
  311. }
  312. },
  313. {
  314. "doc_count" : 1,
  315. "key" : "Node starting up",
  316. "hit" : {
  317. "hits" : {
  318. "total" : {
  319. "value" : 1,
  320. "relation" : "eq"
  321. },
  322. "max_score" : null,
  323. "hits" : [
  324. {
  325. "_index" : "log-messages",
  326. "_id" : "2",
  327. "_score" : null,
  328. "_source" : {
  329. "message" : "2016-02-07T00:00:00+0000 Node 5 starting up"
  330. },
  331. "sort" : [
  332. 1454803320000
  333. ]
  334. }
  335. ]
  336. }
  337. }
  338. }
  339. ]
  340. }
  341. },
  342. {
  343. "key_as_string" : "2016-02-08T00:00:00.000Z",
  344. "key" : 1454889600000,
  345. "doc_count" : 3,
  346. "categories" : {
  347. "buckets" : [
  348. {
  349. "doc_count" : 1,
  350. "key" : "Node shutting down",
  351. "hit" : {
  352. "hits" : {
  353. "total" : {
  354. "value" : 1,
  355. "relation" : "eq"
  356. },
  357. "max_score" : null,
  358. "hits" : [
  359. {
  360. "_index" : "log-messages",
  361. "_id" : "4",
  362. "_score" : null,
  363. "_source" : {
  364. "message" : "2016-02-08T00:00:00+0000 Node 5 shutting down"
  365. },
  366. "sort" : [
  367. 1454889660000
  368. ]
  369. }
  370. ]
  371. }
  372. }
  373. },
  374. {
  375. "doc_count" : 1,
  376. "key" : "User logged off",
  377. "hit" : {
  378. "hits" : {
  379. "total" : {
  380. "value" : 1,
  381. "relation" : "eq"
  382. },
  383. "max_score" : null,
  384. "hits" : [
  385. {
  386. "_index" : "log-messages",
  387. "_id" : "6",
  388. "_score" : null,
  389. "_source" : {
  390. "message" : "2016-02-08T00:00:00+0000 User foo_864 logged off"
  391. },
  392. "sort" : [
  393. 1454889840000
  394. ]
  395. }
  396. ]
  397. }
  398. }
  399. },
  400. {
  401. "doc_count" : 1,
  402. "key" : "User logging on",
  403. "hit" : {
  404. "hits" : {
  405. "total" : {
  406. "value" : 1,
  407. "relation" : "eq"
  408. },
  409. "max_score" : null,
  410. "hits" : [
  411. {
  412. "_index" : "log-messages",
  413. "_id" : "5",
  414. "_score" : null,
  415. "_source" : {
  416. "message" : "2016-02-08T00:00:00+0000 User foo_325 logging on"
  417. },
  418. "sort" : [
  419. 1454889720000
  420. ]
  421. }
  422. ]
  423. }
  424. }
  425. }
  426. ]
  427. }
  428. }
  429. ]
  430. }
  431. }
  432. }
  433. --------------------------------------------------