pattern-analyzer.asciidoc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. [[analysis-pattern-analyzer]]
  2. === Pattern analyzer
  3. ++++
  4. <titleabbrev>Pattern</titleabbrev>
  5. ++++
  6. The `pattern` analyzer uses a regular expression to split the text into terms.
  7. The regular expression should match the *token separators* not the tokens
  8. themselves. The regular expression defaults to `\W+` (or all non-word characters).
  9. [WARNING]
  10. .Beware of Pathological Regular Expressions
  11. ========================================
  12. The pattern analyzer uses
  13. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[Java Regular Expressions].
  14. A badly written regular expression could run very slowly or even throw a
  15. StackOverflowError and cause the node it is running on to exit suddenly.
  16. Read more about https://www.regular-expressions.info/catastrophic.html[pathological regular expressions and how to avoid them].
  17. ========================================
  18. [discrete]
  19. === Example output
  20. [source,console]
  21. ---------------------------
  22. POST _analyze
  23. {
  24. "analyzer": "pattern",
  25. "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
  26. }
  27. ---------------------------
  28. /////////////////////
  29. [source,console-result]
  30. ----------------------------
  31. {
  32. "tokens": [
  33. {
  34. "token": "the",
  35. "start_offset": 0,
  36. "end_offset": 3,
  37. "type": "word",
  38. "position": 0
  39. },
  40. {
  41. "token": "2",
  42. "start_offset": 4,
  43. "end_offset": 5,
  44. "type": "word",
  45. "position": 1
  46. },
  47. {
  48. "token": "quick",
  49. "start_offset": 6,
  50. "end_offset": 11,
  51. "type": "word",
  52. "position": 2
  53. },
  54. {
  55. "token": "brown",
  56. "start_offset": 12,
  57. "end_offset": 17,
  58. "type": "word",
  59. "position": 3
  60. },
  61. {
  62. "token": "foxes",
  63. "start_offset": 18,
  64. "end_offset": 23,
  65. "type": "word",
  66. "position": 4
  67. },
  68. {
  69. "token": "jumped",
  70. "start_offset": 24,
  71. "end_offset": 30,
  72. "type": "word",
  73. "position": 5
  74. },
  75. {
  76. "token": "over",
  77. "start_offset": 31,
  78. "end_offset": 35,
  79. "type": "word",
  80. "position": 6
  81. },
  82. {
  83. "token": "the",
  84. "start_offset": 36,
  85. "end_offset": 39,
  86. "type": "word",
  87. "position": 7
  88. },
  89. {
  90. "token": "lazy",
  91. "start_offset": 40,
  92. "end_offset": 44,
  93. "type": "word",
  94. "position": 8
  95. },
  96. {
  97. "token": "dog",
  98. "start_offset": 45,
  99. "end_offset": 48,
  100. "type": "word",
  101. "position": 9
  102. },
  103. {
  104. "token": "s",
  105. "start_offset": 49,
  106. "end_offset": 50,
  107. "type": "word",
  108. "position": 10
  109. },
  110. {
  111. "token": "bone",
  112. "start_offset": 51,
  113. "end_offset": 55,
  114. "type": "word",
  115. "position": 11
  116. }
  117. ]
  118. }
  119. ----------------------------
  120. /////////////////////
  121. The above sentence would produce the following terms:
  122. [source,text]
  123. ---------------------------
  124. [ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
  125. ---------------------------
  126. [discrete]
  127. === Configuration
  128. The `pattern` analyzer accepts the following parameters:
  129. [horizontal]
  130. `pattern`::
  131. A https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[Java regular expression], defaults to `\W+`.
  132. `flags`::
  133. Java regular expression https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#field.summary[flags].
  134. Flags should be pipe-separated, eg `"CASE_INSENSITIVE|COMMENTS"`.
  135. `lowercase`::
  136. Should terms be lowercased or not. Defaults to `true`.
  137. `stopwords`::
  138. A pre-defined stop words list like `_english_` or an array containing a
  139. list of stop words. Defaults to `_none_`.
  140. `stopwords_path`::
  141. The path to a file containing stop words.
  142. See the <<analysis-stop-tokenfilter,Stop Token Filter>> for more information
  143. about stop word configuration.
  144. [discrete]
  145. === Example configuration
  146. In this example, we configure the `pattern` analyzer to split email addresses
  147. on non-word characters or on underscores (`\W|_`), and to lower-case the result:
  148. [source,console]
  149. ----------------------------
  150. PUT my-index-000001
  151. {
  152. "settings": {
  153. "analysis": {
  154. "analyzer": {
  155. "my_email_analyzer": {
  156. "type": "pattern",
  157. "pattern": "\\W|_", <1>
  158. "lowercase": true
  159. }
  160. }
  161. }
  162. }
  163. }
  164. POST my-index-000001/_analyze
  165. {
  166. "analyzer": "my_email_analyzer",
  167. "text": "John_Smith@foo-bar.com"
  168. }
  169. ----------------------------
  170. <1> The backslashes in the pattern need to be escaped when specifying the
  171. pattern as a JSON string.
  172. /////////////////////
  173. [source,console-result]
  174. ----------------------------
  175. {
  176. "tokens": [
  177. {
  178. "token": "john",
  179. "start_offset": 0,
  180. "end_offset": 4,
  181. "type": "word",
  182. "position": 0
  183. },
  184. {
  185. "token": "smith",
  186. "start_offset": 5,
  187. "end_offset": 10,
  188. "type": "word",
  189. "position": 1
  190. },
  191. {
  192. "token": "foo",
  193. "start_offset": 11,
  194. "end_offset": 14,
  195. "type": "word",
  196. "position": 2
  197. },
  198. {
  199. "token": "bar",
  200. "start_offset": 15,
  201. "end_offset": 18,
  202. "type": "word",
  203. "position": 3
  204. },
  205. {
  206. "token": "com",
  207. "start_offset": 19,
  208. "end_offset": 22,
  209. "type": "word",
  210. "position": 4
  211. }
  212. ]
  213. }
  214. ----------------------------
  215. /////////////////////
  216. The above example produces the following terms:
  217. [source,text]
  218. ---------------------------
  219. [ john, smith, foo, bar, com ]
  220. ---------------------------
  221. [discrete]
  222. ==== CamelCase tokenizer
  223. The following more complicated example splits CamelCase text into tokens:
  224. [source,console]
  225. --------------------------------------------------
  226. PUT my-index-000001
  227. {
  228. "settings": {
  229. "analysis": {
  230. "analyzer": {
  231. "camel": {
  232. "type": "pattern",
  233. "pattern": "([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=[\\p{L}&&[^\\p{Lu}]])(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Lu}[\\p{L}&&[^\\p{Lu}]])"
  234. }
  235. }
  236. }
  237. }
  238. }
  239. GET my-index-000001/_analyze
  240. {
  241. "analyzer": "camel",
  242. "text": "MooseX::FTPClass2_beta"
  243. }
  244. --------------------------------------------------
  245. /////////////////////
  246. [source,console-result]
  247. ----------------------------
  248. {
  249. "tokens": [
  250. {
  251. "token": "moose",
  252. "start_offset": 0,
  253. "end_offset": 5,
  254. "type": "word",
  255. "position": 0
  256. },
  257. {
  258. "token": "x",
  259. "start_offset": 5,
  260. "end_offset": 6,
  261. "type": "word",
  262. "position": 1
  263. },
  264. {
  265. "token": "ftp",
  266. "start_offset": 8,
  267. "end_offset": 11,
  268. "type": "word",
  269. "position": 2
  270. },
  271. {
  272. "token": "class",
  273. "start_offset": 11,
  274. "end_offset": 16,
  275. "type": "word",
  276. "position": 3
  277. },
  278. {
  279. "token": "2",
  280. "start_offset": 16,
  281. "end_offset": 17,
  282. "type": "word",
  283. "position": 4
  284. },
  285. {
  286. "token": "beta",
  287. "start_offset": 18,
  288. "end_offset": 22,
  289. "type": "word",
  290. "position": 5
  291. }
  292. ]
  293. }
  294. ----------------------------
  295. /////////////////////
  296. The above example produces the following terms:
  297. [source,text]
  298. ---------------------------
  299. [ moose, x, ftp, class, 2, beta ]
  300. ---------------------------
  301. The regex above is easier to understand as:
  302. [source,regex]
  303. --------------------------------------------------
  304. ([^\p{L}\d]+) # swallow non letters and numbers,
  305. | (?<=\D)(?=\d) # or non-number followed by number,
  306. | (?<=\d)(?=\D) # or number followed by non-number,
  307. | (?<=[ \p{L} && [^\p{Lu}]]) # or lower case
  308. (?=\p{Lu}) # followed by upper case,
  309. | (?<=\p{Lu}) # or upper case
  310. (?=\p{Lu} # followed by upper case
  311. [\p{L}&&[^\p{Lu}]] # then lower case
  312. )
  313. --------------------------------------------------
  314. [discrete]
  315. === Definition
  316. The `pattern` anlayzer consists of:
  317. Tokenizer::
  318. * <<analysis-pattern-tokenizer,Pattern Tokenizer>>
  319. Token Filters::
  320. * <<analysis-lowercase-tokenfilter,Lower Case Token Filter>>
  321. * <<analysis-stop-tokenfilter,Stop Token Filter>> (disabled by default)
  322. If you need to customize the `pattern` analyzer beyond the configuration
  323. parameters then you need to recreate it as a `custom` analyzer and modify
  324. it, usually by adding token filters. This would recreate the built-in
  325. `pattern` analyzer and you can use it as a starting point for further
  326. customization:
  327. [source,console]
  328. ----------------------------------------------------
  329. PUT /pattern_example
  330. {
  331. "settings": {
  332. "analysis": {
  333. "tokenizer": {
  334. "split_on_non_word": {
  335. "type": "pattern",
  336. "pattern": "\\W+" <1>
  337. }
  338. },
  339. "analyzer": {
  340. "rebuilt_pattern": {
  341. "tokenizer": "split_on_non_word",
  342. "filter": [
  343. "lowercase" <2>
  344. ]
  345. }
  346. }
  347. }
  348. }
  349. }
  350. ----------------------------------------------------
  351. // TEST[s/\n$/\nstartyaml\n - compare_analyzers: {index: pattern_example, first: pattern, second: rebuilt_pattern}\nendyaml\n/]
  352. <1> The default pattern is `\W+` which splits on non-word characters
  353. and this is where you'd change it.
  354. <2> You'd add other token filters after `lowercase`.