ngram-tokenizer.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. [[analysis-ngram-tokenizer]]
  2. === NGram Tokenizer
  3. The `ngram` tokenizer first breaks text down into words whenever it encounters
  4. one of a list of specified characters, then it emits
  5. https://en.wikipedia.org/wiki/N-gram[N-grams] of each word of the specified
  6. length.
  7. N-grams are like a sliding window that moves across the word - a continuous
  8. sequence of characters of the specified length. They are useful for querying
  9. languages that don't use spaces or that have long compound words, like German.
  10. [float]
  11. === Example output
  12. With the default settings, the `ngram` tokenizer treats the initial text as a
  13. single token and produces N-grams with minimum length `1` and maximum length
  14. `2`:
  15. [source,js]
  16. ---------------------------
  17. POST _analyze
  18. {
  19. "tokenizer": "ngram",
  20. "text": "Quick Fox"
  21. }
  22. ---------------------------
  23. // CONSOLE
  24. /////////////////////
  25. [source,js]
  26. ----------------------------
  27. {
  28. "tokens": [
  29. {
  30. "token": "Q",
  31. "start_offset": 0,
  32. "end_offset": 1,
  33. "type": "word",
  34. "position": 0
  35. },
  36. {
  37. "token": "Qu",
  38. "start_offset": 0,
  39. "end_offset": 2,
  40. "type": "word",
  41. "position": 1
  42. },
  43. {
  44. "token": "u",
  45. "start_offset": 1,
  46. "end_offset": 2,
  47. "type": "word",
  48. "position": 2
  49. },
  50. {
  51. "token": "ui",
  52. "start_offset": 1,
  53. "end_offset": 3,
  54. "type": "word",
  55. "position": 3
  56. },
  57. {
  58. "token": "i",
  59. "start_offset": 2,
  60. "end_offset": 3,
  61. "type": "word",
  62. "position": 4
  63. },
  64. {
  65. "token": "ic",
  66. "start_offset": 2,
  67. "end_offset": 4,
  68. "type": "word",
  69. "position": 5
  70. },
  71. {
  72. "token": "c",
  73. "start_offset": 3,
  74. "end_offset": 4,
  75. "type": "word",
  76. "position": 6
  77. },
  78. {
  79. "token": "ck",
  80. "start_offset": 3,
  81. "end_offset": 5,
  82. "type": "word",
  83. "position": 7
  84. },
  85. {
  86. "token": "k",
  87. "start_offset": 4,
  88. "end_offset": 5,
  89. "type": "word",
  90. "position": 8
  91. },
  92. {
  93. "token": "k ",
  94. "start_offset": 4,
  95. "end_offset": 6,
  96. "type": "word",
  97. "position": 9
  98. },
  99. {
  100. "token": " ",
  101. "start_offset": 5,
  102. "end_offset": 6,
  103. "type": "word",
  104. "position": 10
  105. },
  106. {
  107. "token": " F",
  108. "start_offset": 5,
  109. "end_offset": 7,
  110. "type": "word",
  111. "position": 11
  112. },
  113. {
  114. "token": "F",
  115. "start_offset": 6,
  116. "end_offset": 7,
  117. "type": "word",
  118. "position": 12
  119. },
  120. {
  121. "token": "Fo",
  122. "start_offset": 6,
  123. "end_offset": 8,
  124. "type": "word",
  125. "position": 13
  126. },
  127. {
  128. "token": "o",
  129. "start_offset": 7,
  130. "end_offset": 8,
  131. "type": "word",
  132. "position": 14
  133. },
  134. {
  135. "token": "ox",
  136. "start_offset": 7,
  137. "end_offset": 9,
  138. "type": "word",
  139. "position": 15
  140. },
  141. {
  142. "token": "x",
  143. "start_offset": 8,
  144. "end_offset": 9,
  145. "type": "word",
  146. "position": 16
  147. }
  148. ]
  149. }
  150. ----------------------------
  151. // TESTRESPONSE
  152. /////////////////////
  153. The above sentence would produce the following terms:
  154. [source,text]
  155. ---------------------------
  156. [ Q, Qu, u, ui, i, ic, c, ck, k, "k ", " ", " F", F, Fo, o, ox, x ]
  157. ---------------------------
  158. [float]
  159. === Configuration
  160. The `ngram` tokenizer accepts the following parameters:
  161. [horizontal]
  162. `min_gram`::
  163. Minimum length of characters in a gram. Defaults to `1`.
  164. `max_gram`::
  165. Maximum length of characters in a gram. Defaults to `2`.
  166. `token_chars`::
  167. Character classes that should be included in a token. Elasticsearch
  168. will split on characters that don't belong to the classes specified.
  169. Defaults to `[]` (keep all characters).
  170. +
  171. Character classes may be any of the following:
  172. +
  173. * `letter` -- for example `a`, `b`, `ï` or `京`
  174. * `digit` -- for example `3` or `7`
  175. * `whitespace` -- for example `" "` or `"\n"`
  176. * `punctuation` -- for example `!` or `"`
  177. * `symbol` -- for example `$` or `√`
  178. TIP: It usually makes sense to set `min_gram` and `max_gram` to the same
  179. value. The smaller the length, the more documents will match but the lower
  180. the quality of the matches. The longer the length, the more specific the
  181. matches. A tri-gram (length `3`) is a good place to start.
  182. [float]
  183. === Example configuration
  184. In this example, we configure the `ngram` tokenizer to treat letters and
  185. digits as tokens, and to produce tri-grams (grams of length `3`):
  186. [source,js]
  187. ----------------------------
  188. PUT my_index
  189. {
  190. "settings": {
  191. "analysis": {
  192. "analyzer": {
  193. "my_analyzer": {
  194. "tokenizer": "my_tokenizer"
  195. }
  196. },
  197. "tokenizer": {
  198. "my_tokenizer": {
  199. "type": "ngram",
  200. "min_gram": 3,
  201. "max_gram": 3,
  202. "token_chars": [
  203. "letter",
  204. "digit"
  205. ]
  206. }
  207. }
  208. }
  209. }
  210. }
  211. POST my_index/_analyze
  212. {
  213. "analyzer": "my_analyzer",
  214. "text": "2 Quick Foxes."
  215. }
  216. ----------------------------
  217. // CONSOLE
  218. /////////////////////
  219. [source,js]
  220. ----------------------------
  221. {
  222. "tokens": [
  223. {
  224. "token": "Qui",
  225. "start_offset": 2,
  226. "end_offset": 5,
  227. "type": "word",
  228. "position": 0
  229. },
  230. {
  231. "token": "uic",
  232. "start_offset": 3,
  233. "end_offset": 6,
  234. "type": "word",
  235. "position": 1
  236. },
  237. {
  238. "token": "ick",
  239. "start_offset": 4,
  240. "end_offset": 7,
  241. "type": "word",
  242. "position": 2
  243. },
  244. {
  245. "token": "Fox",
  246. "start_offset": 8,
  247. "end_offset": 11,
  248. "type": "word",
  249. "position": 3
  250. },
  251. {
  252. "token": "oxe",
  253. "start_offset": 9,
  254. "end_offset": 12,
  255. "type": "word",
  256. "position": 4
  257. },
  258. {
  259. "token": "xes",
  260. "start_offset": 10,
  261. "end_offset": 13,
  262. "type": "word",
  263. "position": 5
  264. }
  265. ]
  266. }
  267. ----------------------------
  268. // TESTRESPONSE
  269. /////////////////////
  270. The above example produces the following terms:
  271. [source,text]
  272. ---------------------------
  273. [ Qui, uic, ick, Fox, oxe, xes ]
  274. ---------------------------