1
0

pattern-analyzer.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. [[analysis-pattern-analyzer]]
  2. === Pattern Analyzer
  3. The `pattern` analyzer uses a regular expression to split the text into terms.
  4. The regular expression should match the *token separators* not the tokens
  5. themselves. The regular expression defaults to `\W+` (or all non-word characters).
  6. [WARNING]
  7. .Beware of Pathological Regular Expressions
  8. ========================================
  9. The pattern analyzer uses
  10. http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[Java Regular Expressions].
  11. A badly written regular expression could run very slowly or even throw a
  12. StackOverflowError and cause the node it is running on to exit suddenly.
  13. Read more about http://www.regular-expressions.info/catastrophic.html[pathological regular expressions and how to avoid them].
  14. ========================================
  15. [float]
  16. === Example output
  17. [source,js]
  18. ---------------------------
  19. POST _analyze
  20. {
  21. "analyzer": "pattern",
  22. "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
  23. }
  24. ---------------------------
  25. // CONSOLE
  26. /////////////////////
  27. [source,console-result]
  28. ----------------------------
  29. {
  30. "tokens": [
  31. {
  32. "token": "the",
  33. "start_offset": 0,
  34. "end_offset": 3,
  35. "type": "word",
  36. "position": 0
  37. },
  38. {
  39. "token": "2",
  40. "start_offset": 4,
  41. "end_offset": 5,
  42. "type": "word",
  43. "position": 1
  44. },
  45. {
  46. "token": "quick",
  47. "start_offset": 6,
  48. "end_offset": 11,
  49. "type": "word",
  50. "position": 2
  51. },
  52. {
  53. "token": "brown",
  54. "start_offset": 12,
  55. "end_offset": 17,
  56. "type": "word",
  57. "position": 3
  58. },
  59. {
  60. "token": "foxes",
  61. "start_offset": 18,
  62. "end_offset": 23,
  63. "type": "word",
  64. "position": 4
  65. },
  66. {
  67. "token": "jumped",
  68. "start_offset": 24,
  69. "end_offset": 30,
  70. "type": "word",
  71. "position": 5
  72. },
  73. {
  74. "token": "over",
  75. "start_offset": 31,
  76. "end_offset": 35,
  77. "type": "word",
  78. "position": 6
  79. },
  80. {
  81. "token": "the",
  82. "start_offset": 36,
  83. "end_offset": 39,
  84. "type": "word",
  85. "position": 7
  86. },
  87. {
  88. "token": "lazy",
  89. "start_offset": 40,
  90. "end_offset": 44,
  91. "type": "word",
  92. "position": 8
  93. },
  94. {
  95. "token": "dog",
  96. "start_offset": 45,
  97. "end_offset": 48,
  98. "type": "word",
  99. "position": 9
  100. },
  101. {
  102. "token": "s",
  103. "start_offset": 49,
  104. "end_offset": 50,
  105. "type": "word",
  106. "position": 10
  107. },
  108. {
  109. "token": "bone",
  110. "start_offset": 51,
  111. "end_offset": 55,
  112. "type": "word",
  113. "position": 11
  114. }
  115. ]
  116. }
  117. ----------------------------
  118. /////////////////////
  119. The above sentence would produce the following terms:
  120. [source,text]
  121. ---------------------------
  122. [ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
  123. ---------------------------
  124. [float]
  125. === Configuration
  126. The `pattern` analyzer accepts the following parameters:
  127. [horizontal]
  128. `pattern`::
  129. A http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[Java regular expression], defaults to `\W+`.
  130. `flags`::
  131. Java regular expression http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#field.summary[flags].
  132. Flags should be pipe-separated, eg `"CASE_INSENSITIVE|COMMENTS"`.
  133. `lowercase`::
  134. Should terms be lowercased or not. Defaults to `true`.
  135. `stopwords`::
  136. A pre-defined stop words list like `_english_` or an array containing a
  137. list of stop words. Defaults to `_none_`.
  138. `stopwords_path`::
  139. The path to a file containing stop words.
  140. See the <<analysis-stop-tokenfilter,Stop Token Filter>> for more information
  141. about stop word configuration.
  142. [float]
  143. === Example configuration
  144. In this example, we configure the `pattern` analyzer to split email addresses
  145. on non-word characters or on underscores (`\W|_`), and to lower-case the result:
  146. [source,js]
  147. ----------------------------
  148. PUT my_index
  149. {
  150. "settings": {
  151. "analysis": {
  152. "analyzer": {
  153. "my_email_analyzer": {
  154. "type": "pattern",
  155. "pattern": "\\W|_", <1>
  156. "lowercase": true
  157. }
  158. }
  159. }
  160. }
  161. }
  162. POST my_index/_analyze
  163. {
  164. "analyzer": "my_email_analyzer",
  165. "text": "John_Smith@foo-bar.com"
  166. }
  167. ----------------------------
  168. // CONSOLE
  169. <1> The backslashes in the pattern need to be escaped when specifying the
  170. pattern as a JSON string.
  171. /////////////////////
  172. [source,console-result]
  173. ----------------------------
  174. {
  175. "tokens": [
  176. {
  177. "token": "john",
  178. "start_offset": 0,
  179. "end_offset": 4,
  180. "type": "word",
  181. "position": 0
  182. },
  183. {
  184. "token": "smith",
  185. "start_offset": 5,
  186. "end_offset": 10,
  187. "type": "word",
  188. "position": 1
  189. },
  190. {
  191. "token": "foo",
  192. "start_offset": 11,
  193. "end_offset": 14,
  194. "type": "word",
  195. "position": 2
  196. },
  197. {
  198. "token": "bar",
  199. "start_offset": 15,
  200. "end_offset": 18,
  201. "type": "word",
  202. "position": 3
  203. },
  204. {
  205. "token": "com",
  206. "start_offset": 19,
  207. "end_offset": 22,
  208. "type": "word",
  209. "position": 4
  210. }
  211. ]
  212. }
  213. ----------------------------
  214. /////////////////////
  215. The above example produces the following terms:
  216. [source,text]
  217. ---------------------------
  218. [ john, smith, foo, bar, com ]
  219. ---------------------------
  220. [float]
  221. ==== CamelCase tokenizer
  222. The following more complicated example splits CamelCase text into tokens:
  223. [source,js]
  224. --------------------------------------------------
  225. PUT my_index
  226. {
  227. "settings": {
  228. "analysis": {
  229. "analyzer": {
  230. "camel": {
  231. "type": "pattern",
  232. "pattern": "([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=[\\p{L}&&[^\\p{Lu}]])(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Lu}[\\p{L}&&[^\\p{Lu}]])"
  233. }
  234. }
  235. }
  236. }
  237. }
  238. GET my_index/_analyze
  239. {
  240. "analyzer": "camel",
  241. "text": "MooseX::FTPClass2_beta"
  242. }
  243. --------------------------------------------------
  244. // CONSOLE
  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. [float]
  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,js]
  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. // CONSOLE
  352. // TEST[s/\n$/\nstartyaml\n - compare_analyzers: {index: pattern_example, first: pattern, second: rebuilt_pattern}\nendyaml\n/]
  353. <1> The default pattern is `\W+` which splits on non-word characters
  354. and this is where you'd change it.
  355. <2> You'd add other token filters after `lowercase`.