ngram-tokenizer.asciidoc 6.4 KB

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