categorize-text-aggregation.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. [[search-aggregations-bucket-categorize-text-aggregation]]
  2. === Categorize text aggregation
  3. ++++
  4. <titleabbrev>Categorize text</titleabbrev>
  5. ++++
  6. A multi-bucket aggregation that groups semi-structured text into buckets. Each `text` field is re-analyzed
  7. using a custom analyzer. The resulting tokens are then categorized creating buckets of similarly formatted
  8. text values. This aggregation works best with machine generated text like system logs. Only the first 100 analyzed
  9. tokens are used to categorize the text.
  10. NOTE: If you have considerable memory allocated to your JVM but are receiving circuit breaker exceptions from this
  11. aggregation, you may be attempting to categorize text that is poorly formatted for categorization. Consider
  12. adding `categorization_filters` or running under <<search-aggregations-bucket-sampler-aggregation,sampler>>,
  13. <<search-aggregations-bucket-diversified-sampler-aggregation,diversified sampler>>, or
  14. <<search-aggregations-random-sampler-aggregation,random sampler>> to explore the created categories.
  15. NOTE: The algorithm used for categorization was completely changed in version 8.3.0. As a result this aggregation
  16. will not work in a mixed version cluster where some nodes are on version 8.3.0 or higher and others are
  17. on a version older than 8.3.0. Upgrade all nodes in your cluster to the same version if you experience
  18. an error related to this change.
  19. [[bucket-categorize-text-agg-syntax]]
  20. ==== Parameters
  21. `categorization_analyzer`::
  22. (Optional, object or string)
  23. The categorization analyzer specifies how the text is analyzed and tokenized before
  24. being categorized. The syntax is very similar to that used to define the `analyzer` in the
  25. <<indices-analyze,Analyze endpoint>>. This
  26. property cannot be used at the same time as `categorization_filters`.
  27. +
  28. The `categorization_analyzer` field can be specified either as a string or as an
  29. object. If it is a string it must refer to a
  30. <<analysis-analyzers,built-in analyzer>> or one added by another plugin. If it
  31. is an object it has the following properties:
  32. +
  33. .Properties of `categorization_analyzer`
  34. [%collapsible%open]
  35. =====
  36. `char_filter`::::
  37. (array of strings or objects)
  38. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=char-filter]
  39. `tokenizer`::::
  40. (string or object)
  41. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=tokenizer]
  42. `filter`::::
  43. (array of strings or objects)
  44. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=filter]
  45. =====
  46. `categorization_filters`::
  47. (Optional, array of strings)
  48. This property expects an array of regular expressions. The expressions
  49. are used to filter out matching sequences from the categorization field values.
  50. You can use this functionality to fine tune the categorization by excluding
  51. sequences from consideration when categories are defined. For example, you can
  52. exclude SQL statements that appear in your log files. This
  53. property cannot be used at the same time as `categorization_analyzer`. If you
  54. only want to define simple regular expression filters that are applied prior to
  55. tokenization, setting this property is the easiest method. If you also want to
  56. customize the tokenizer or post-tokenization filtering, use the
  57. `categorization_analyzer` property instead and include the filters as
  58. `pattern_replace` character filters.
  59. `field`::
  60. (Required, string)
  61. The semi-structured text field to categorize.
  62. `max_matched_tokens`::
  63. (Optional, integer)
  64. This parameter does nothing now, but is permitted for compatibility with the original
  65. pre-8.3.0 implementation.
  66. `max_unique_tokens`::
  67. (Optional, integer)
  68. This parameter does nothing now, but is permitted for compatibility with the original
  69. pre-8.3.0 implementation.
  70. `min_doc_count`::
  71. (Optional, integer)
  72. The minimum number of documents for a bucket to be returned to the results.
  73. `shard_min_doc_count`::
  74. (Optional, integer)
  75. The minimum number of documents for a bucket to be returned from the shard before
  76. merging.
  77. `shard_size`::
  78. (Optional, integer)
  79. The number of categorization buckets to return from each shard before merging
  80. all the results.
  81. `similarity_threshold`::
  82. (Optional, integer, default: `70`)
  83. The minimum percentage of token weight that must match for text to be added to the
  84. category bucket.
  85. Must be between 1 and 100. The larger the value the narrower the categories.
  86. Larger values will increase memory usage and create narrower categories.
  87. `size`::
  88. (Optional, integer, default: `10`)
  89. The number of buckets to return.
  90. [[bucket-categorize-text-agg-response]]
  91. ==== Response body
  92. `key`::
  93. (string)
  94. Consists of the tokens (extracted by the `categorization_analyzer`)
  95. that are common to all values of the input field included in the category.
  96. `doc_count`::
  97. (integer)
  98. Number of documents matching the category.
  99. `max_matching_length`::
  100. (integer)
  101. Categories from short messages containing few tokens may also match
  102. categories containing many tokens derived from much longer messages.
  103. `max_matching_length` is an indication of the maximum length of messages
  104. that should be considered to belong to the category. When searching for
  105. messages that match the category, any messages longer than
  106. `max_matching_length` should be excluded. Use this field to prevent a
  107. search for members of a category of short messages from matching much longer
  108. ones.
  109. `regex`::
  110. (string)
  111. A regular expression that will match all values of the input field included
  112. in the category. It is possible that the `regex` does not incorporate every
  113. term in `key`, if ordering varies between the values included in the
  114. category. However, in simple cases the `regex` will be the ordered terms
  115. concatenated into a regular expression that allows for arbitrary sections
  116. in between them. It is not recommended to use the `regex` as the primary
  117. mechanism for searching for the original documents that were categorized.
  118. Search using a regular expression is very slow. Instead the terms in the
  119. `key` field should be used to search for matching documents, as a terms
  120. search can use the inverted index and hence be much faster. However, there
  121. may be situations where it is useful to use the `regex` field to test whether
  122. a small set of messages that have not been indexed match the category, or to
  123. confirm that the terms in the `key` occur in the correct order in all the
  124. matched documents.
  125. ==== Basic use
  126. WARNING: Re-analyzing _large_ result sets will require a lot of time and memory. This aggregation should be
  127. used in conjunction with <<async-search, Async search>>. Additionally, you may consider
  128. using the aggregation as a child of either the <<search-aggregations-bucket-sampler-aggregation,sampler>> or
  129. <<search-aggregations-bucket-diversified-sampler-aggregation,diversified sampler>> aggregation.
  130. This will typically improve speed and memory use.
  131. Example:
  132. [source,console]
  133. --------------------------------------------------
  134. POST log-messages/_search?filter_path=aggregations
  135. {
  136. "aggs": {
  137. "categories": {
  138. "categorize_text": {
  139. "field": "message"
  140. }
  141. }
  142. }
  143. }
  144. --------------------------------------------------
  145. // TEST[setup:categorize_text]
  146. Response:
  147. [source,console-result]
  148. --------------------------------------------------
  149. {
  150. "aggregations" : {
  151. "categories" : {
  152. "buckets" : [
  153. {
  154. "doc_count" : 3,
  155. "key" : "Node shutting down",
  156. "regex" : ".*?Node.+?shutting.+?down.*?",
  157. "max_matching_length" : 49
  158. },
  159. {
  160. "doc_count" : 1,
  161. "key" : "Node starting up",
  162. "regex" : ".*?Node.+?starting.+?up.*?",
  163. "max_matching_length" : 47
  164. },
  165. {
  166. "doc_count" : 1,
  167. "key" : "User foo_325 logging on",
  168. "regex" : ".*?User.+?foo_325.+?logging.+?on.*?",
  169. "max_matching_length" : 52
  170. },
  171. {
  172. "doc_count" : 1,
  173. "key" : "User foo_864 logged off",
  174. "regex" : ".*?User.+?foo_864.+?logged.+?off.*?",
  175. "max_matching_length" : 52
  176. }
  177. ]
  178. }
  179. }
  180. }
  181. --------------------------------------------------
  182. Here is an example using `categorization_filters`
  183. [source,console]
  184. --------------------------------------------------
  185. POST log-messages/_search?filter_path=aggregations
  186. {
  187. "aggs": {
  188. "categories": {
  189. "categorize_text": {
  190. "field": "message",
  191. "categorization_filters": ["\\w+\\_\\d{3}"] <1>
  192. }
  193. }
  194. }
  195. }
  196. --------------------------------------------------
  197. // TEST[setup:categorize_text]
  198. <1> The filters to apply to the analyzed tokens. It filters
  199. out tokens like `bar_123`.
  200. Note how the `foo_<number>` tokens are not part of the
  201. category results
  202. [source,console-result]
  203. --------------------------------------------------
  204. {
  205. "aggregations" : {
  206. "categories" : {
  207. "buckets" : [
  208. {
  209. "doc_count" : 3,
  210. "key" : "Node shutting down",
  211. "regex" : ".*?Node.+?shutting.+?down.*?",
  212. "max_matching_length" : 49
  213. },
  214. {
  215. "doc_count" : 1,
  216. "key" : "Node starting up",
  217. "regex" : ".*?Node.+?starting.+?up.*?",
  218. "max_matching_length" : 47
  219. },
  220. {
  221. "doc_count" : 1,
  222. "key" : "User logged off",
  223. "regex" : ".*?User.+?logged.+?off.*?",
  224. "max_matching_length" : 52
  225. },
  226. {
  227. "doc_count" : 1,
  228. "key" : "User logging on",
  229. "regex" : ".*?User.+?logging.+?on.*?",
  230. "max_matching_length" : 52
  231. }
  232. ]
  233. }
  234. }
  235. }
  236. --------------------------------------------------
  237. Here is an example using `categorization_filters`.
  238. The default analyzer uses the `ml_standard` tokenizer which is similar to a whitespace tokenizer
  239. but filters out tokens that could be interpreted as hexadecimal numbers. The default analyzer
  240. also uses the `first_line_with_letters` character filter, so that only the first meaningful line
  241. of multi-line messages is considered.
  242. 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
  243. custom `categorization_filters` to filter out those tokens for better categories. These filters may also reduce memory usage as fewer
  244. tokens are held in memory for the categories. (If there are sufficient examples of different usernames, emails, etc., then
  245. categories will form that naturally discard them as variables, but for small input data where only one example exists this won't
  246. happen.)
  247. [source,console]
  248. --------------------------------------------------
  249. POST log-messages/_search?filter_path=aggregations
  250. {
  251. "aggs": {
  252. "categories": {
  253. "categorize_text": {
  254. "field": "message",
  255. "categorization_filters": ["\\w+\\_\\d{3}"], <1>
  256. "similarity_threshold": 11 <2>
  257. }
  258. }
  259. }
  260. }
  261. --------------------------------------------------
  262. // TEST[setup:categorize_text]
  263. <1> The filters to apply to the analyzed tokens. It filters
  264. out tokens like `bar_123`.
  265. <2> Require 11% of token weight to match before adding a message to an
  266. existing category rather than creating a new one.
  267. The resulting categories are now very broad, merging the log groups.
  268. (A `similarity_threshold` of 11% is generally too low. Settings over
  269. 50% are usually better.)
  270. [source,console-result]
  271. --------------------------------------------------
  272. {
  273. "aggregations" : {
  274. "categories" : {
  275. "buckets" : [
  276. {
  277. "doc_count" : 4,
  278. "key" : "Node",
  279. "regex" : ".*?Node.*?",
  280. "max_matching_length" : 49
  281. },
  282. {
  283. "doc_count" : 2,
  284. "key" : "User",
  285. "regex" : ".*?User.*?",
  286. "max_matching_length" : 52
  287. }
  288. ]
  289. }
  290. }
  291. }
  292. --------------------------------------------------
  293. This aggregation can have both sub-aggregations and itself be a sub-aggregation. This allows gathering the top daily categories and the
  294. top sample doc as below.
  295. [source,console]
  296. --------------------------------------------------
  297. POST log-messages/_search?filter_path=aggregations
  298. {
  299. "aggs": {
  300. "daily": {
  301. "date_histogram": {
  302. "field": "time",
  303. "fixed_interval": "1d"
  304. },
  305. "aggs": {
  306. "categories": {
  307. "categorize_text": {
  308. "field": "message",
  309. "categorization_filters": ["\\w+\\_\\d{3}"]
  310. },
  311. "aggs": {
  312. "hit": {
  313. "top_hits": {
  314. "size": 1,
  315. "sort": ["time"],
  316. "_source": "message"
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. --------------------------------------------------
  326. // TEST[setup:categorize_text]
  327. [source,console-result]
  328. --------------------------------------------------
  329. {
  330. "aggregations" : {
  331. "daily" : {
  332. "buckets" : [
  333. {
  334. "key_as_string" : "2016-02-07T00:00:00.000Z",
  335. "key" : 1454803200000,
  336. "doc_count" : 3,
  337. "categories" : {
  338. "buckets" : [
  339. {
  340. "doc_count" : 2,
  341. "key" : "Node shutting down",
  342. "regex" : ".*?Node.+?shutting.+?down.*?",
  343. "max_matching_length" : 49,
  344. "hit" : {
  345. "hits" : {
  346. "total" : {
  347. "value" : 2,
  348. "relation" : "eq"
  349. },
  350. "max_score" : null,
  351. "hits" : [
  352. {
  353. "_index" : "log-messages",
  354. "_id" : "1",
  355. "_score" : null,
  356. "_source" : {
  357. "message" : "2016-02-07T00:00:00+0000 Node 3 shutting down"
  358. },
  359. "sort" : [
  360. 1454803260000
  361. ]
  362. }
  363. ]
  364. }
  365. }
  366. },
  367. {
  368. "doc_count" : 1,
  369. "key" : "Node starting up",
  370. "regex" : ".*?Node.+?starting.+?up.*?",
  371. "max_matching_length" : 47,
  372. "hit" : {
  373. "hits" : {
  374. "total" : {
  375. "value" : 1,
  376. "relation" : "eq"
  377. },
  378. "max_score" : null,
  379. "hits" : [
  380. {
  381. "_index" : "log-messages",
  382. "_id" : "2",
  383. "_score" : null,
  384. "_source" : {
  385. "message" : "2016-02-07T00:00:00+0000 Node 5 starting up"
  386. },
  387. "sort" : [
  388. 1454803320000
  389. ]
  390. }
  391. ]
  392. }
  393. }
  394. }
  395. ]
  396. }
  397. },
  398. {
  399. "key_as_string" : "2016-02-08T00:00:00.000Z",
  400. "key" : 1454889600000,
  401. "doc_count" : 3,
  402. "categories" : {
  403. "buckets" : [
  404. {
  405. "doc_count" : 1,
  406. "key" : "Node shutting down",
  407. "regex" : ".*?Node.+?shutting.+?down.*?",
  408. "max_matching_length" : 49,
  409. "hit" : {
  410. "hits" : {
  411. "total" : {
  412. "value" : 1,
  413. "relation" : "eq"
  414. },
  415. "max_score" : null,
  416. "hits" : [
  417. {
  418. "_index" : "log-messages",
  419. "_id" : "4",
  420. "_score" : null,
  421. "_source" : {
  422. "message" : "2016-02-08T00:00:00+0000 Node 5 shutting down"
  423. },
  424. "sort" : [
  425. 1454889660000
  426. ]
  427. }
  428. ]
  429. }
  430. }
  431. },
  432. {
  433. "doc_count" : 1,
  434. "key" : "User logged off",
  435. "regex" : ".*?User.+?logged.+?off.*?",
  436. "max_matching_length" : 52,
  437. "hit" : {
  438. "hits" : {
  439. "total" : {
  440. "value" : 1,
  441. "relation" : "eq"
  442. },
  443. "max_score" : null,
  444. "hits" : [
  445. {
  446. "_index" : "log-messages",
  447. "_id" : "6",
  448. "_score" : null,
  449. "_source" : {
  450. "message" : "2016-02-08T00:00:00+0000 User foo_864 logged off"
  451. },
  452. "sort" : [
  453. 1454889840000
  454. ]
  455. }
  456. ]
  457. }
  458. }
  459. },
  460. {
  461. "doc_count" : 1,
  462. "key" : "User logging on",
  463. "regex" : ".*?User.+?logging.+?on.*?",
  464. "max_matching_length" : 52,
  465. "hit" : {
  466. "hits" : {
  467. "total" : {
  468. "value" : 1,
  469. "relation" : "eq"
  470. },
  471. "max_score" : null,
  472. "hits" : [
  473. {
  474. "_index" : "log-messages",
  475. "_id" : "5",
  476. "_score" : null,
  477. "_source" : {
  478. "message" : "2016-02-08T00:00:00+0000 User foo_325 logging on"
  479. },
  480. "sort" : [
  481. 1454889720000
  482. ]
  483. }
  484. ]
  485. }
  486. }
  487. }
  488. ]
  489. }
  490. }
  491. ]
  492. }
  493. }
  494. }
  495. --------------------------------------------------