classic-tokenizer.asciidoc 5.3 KB

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