classic-tokenizer.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. [[analysis-classic-tokenizer]]
  2. === Classic Tokenizer
  3. The `classic` tokenizer is a grammar based tokenizer that is good for English
  4. language documents. This tokenizer has heuristics for special treatment of
  5. acronyms, company names, email addresses, and internet host names. However,
  6. these rules don't always work, and the tokenizer doesn't work well for most
  7. languages other than English:
  8. * It splits words at most punctuation characters, removing punctuation. However, a
  9. dot that's not followed by whitespace is considered part of a token.
  10. * It splits words at hyphens, unless there's a number in the token, in which case
  11. the whole token is interpreted as a product number and is not split.
  12. * It recognizes email addresses and internet hostnames as one token.
  13. [float]
  14. === Example output
  15. [source,js]
  16. ---------------------------
  17. POST _analyze
  18. {
  19. "tokenizer": "classic",
  20. "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
  21. }
  22. ---------------------------
  23. // CONSOLE
  24. /////////////////////
  25. [source,console-result]
  26. ----------------------------
  27. {
  28. "tokens": [
  29. {
  30. "token": "The",
  31. "start_offset": 0,
  32. "end_offset": 3,
  33. "type": "<ALPHANUM>",
  34. "position": 0
  35. },
  36. {
  37. "token": "2",
  38. "start_offset": 4,
  39. "end_offset": 5,
  40. "type": "<ALPHANUM>",
  41. "position": 1
  42. },
  43. {
  44. "token": "QUICK",
  45. "start_offset": 6,
  46. "end_offset": 11,
  47. "type": "<ALPHANUM>",
  48. "position": 2
  49. },
  50. {
  51. "token": "Brown",
  52. "start_offset": 12,
  53. "end_offset": 17,
  54. "type": "<ALPHANUM>",
  55. "position": 3
  56. },
  57. {
  58. "token": "Foxes",
  59. "start_offset": 18,
  60. "end_offset": 23,
  61. "type": "<ALPHANUM>",
  62. "position": 4
  63. },
  64. {
  65. "token": "jumped",
  66. "start_offset": 24,
  67. "end_offset": 30,
  68. "type": "<ALPHANUM>",
  69. "position": 5
  70. },
  71. {
  72. "token": "over",
  73. "start_offset": 31,
  74. "end_offset": 35,
  75. "type": "<ALPHANUM>",
  76. "position": 6
  77. },
  78. {
  79. "token": "the",
  80. "start_offset": 36,
  81. "end_offset": 39,
  82. "type": "<ALPHANUM>",
  83. "position": 7
  84. },
  85. {
  86. "token": "lazy",
  87. "start_offset": 40,
  88. "end_offset": 44,
  89. "type": "<ALPHANUM>",
  90. "position": 8
  91. },
  92. {
  93. "token": "dog's",
  94. "start_offset": 45,
  95. "end_offset": 50,
  96. "type": "<APOSTROPHE>",
  97. "position": 9
  98. },
  99. {
  100. "token": "bone",
  101. "start_offset": 51,
  102. "end_offset": 55,
  103. "type": "<ALPHANUM>",
  104. "position": 10
  105. }
  106. ]
  107. }
  108. ----------------------------
  109. /////////////////////
  110. The above sentence would produce the following terms:
  111. [source,text]
  112. ---------------------------
  113. [ The, 2, QUICK, Brown, Foxes, jumped, over, the, lazy, dog's, bone ]
  114. ---------------------------
  115. [float]
  116. === Configuration
  117. The `classic` tokenizer accepts the following parameters:
  118. [horizontal]
  119. `max_token_length`::
  120. The maximum token length. If a token is seen that exceeds this length then
  121. it is split at `max_token_length` intervals. Defaults to `255`.
  122. [float]
  123. === Example configuration
  124. In this example, we configure the `classic` tokenizer to have a
  125. `max_token_length` of 5 (for demonstration purposes):
  126. [source,js]
  127. ----------------------------
  128. PUT my_index
  129. {
  130. "settings": {
  131. "analysis": {
  132. "analyzer": {
  133. "my_analyzer": {
  134. "tokenizer": "my_tokenizer"
  135. }
  136. },
  137. "tokenizer": {
  138. "my_tokenizer": {
  139. "type": "classic",
  140. "max_token_length": 5
  141. }
  142. }
  143. }
  144. }
  145. }
  146. POST my_index/_analyze
  147. {
  148. "analyzer": "my_analyzer",
  149. "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
  150. }
  151. ----------------------------
  152. // CONSOLE
  153. /////////////////////
  154. [source,console-result]
  155. ----------------------------
  156. {
  157. "tokens": [
  158. {
  159. "token": "The",
  160. "start_offset": 0,
  161. "end_offset": 3,
  162. "type": "<ALPHANUM>",
  163. "position": 0
  164. },
  165. {
  166. "token": "2",
  167. "start_offset": 4,
  168. "end_offset": 5,
  169. "type": "<ALPHANUM>",
  170. "position": 1
  171. },
  172. {
  173. "token": "QUICK",
  174. "start_offset": 6,
  175. "end_offset": 11,
  176. "type": "<ALPHANUM>",
  177. "position": 2
  178. },
  179. {
  180. "token": "Brown",
  181. "start_offset": 12,
  182. "end_offset": 17,
  183. "type": "<ALPHANUM>",
  184. "position": 3
  185. },
  186. {
  187. "token": "Foxes",
  188. "start_offset": 18,
  189. "end_offset": 23,
  190. "type": "<ALPHANUM>",
  191. "position": 4
  192. },
  193. {
  194. "token": "over",
  195. "start_offset": 31,
  196. "end_offset": 35,
  197. "type": "<ALPHANUM>",
  198. "position": 6
  199. },
  200. {
  201. "token": "the",
  202. "start_offset": 36,
  203. "end_offset": 39,
  204. "type": "<ALPHANUM>",
  205. "position": 7
  206. },
  207. {
  208. "token": "lazy",
  209. "start_offset": 40,
  210. "end_offset": 44,
  211. "type": "<ALPHANUM>",
  212. "position": 8
  213. },
  214. {
  215. "token": "dog's",
  216. "start_offset": 45,
  217. "end_offset": 50,
  218. "type": "<APOSTROPHE>",
  219. "position": 9
  220. },
  221. {
  222. "token": "bone",
  223. "start_offset": 51,
  224. "end_offset": 55,
  225. "type": "<ALPHANUM>",
  226. "position": 10
  227. }
  228. ]
  229. }
  230. ----------------------------
  231. /////////////////////
  232. The above example produces the following terms:
  233. [source,text]
  234. ---------------------------
  235. [ The, 2, QUICK, Brown, Foxes, jumpe, d, over, the, lazy, dog's, bone ]
  236. ---------------------------