ngram-tokenizer.asciidoc 6.4 KB

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