ngram-tokenizer.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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,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. TIP: It usually makes sense to set `min_gram` and `max_gram` to the same
  177. value. The smaller the length, the more documents will match but the lower
  178. the quality of the matches. The longer the length, the more specific the
  179. matches. A tri-gram (length `3`) is a good place to start.
  180. The index level setting `index.max_ngram_diff` controls the maximum allowed
  181. difference between `max_gram` and `min_gram`.
  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,console]
  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. /////////////////////
  218. [source,console-result]
  219. ----------------------------
  220. {
  221. "tokens": [
  222. {
  223. "token": "Qui",
  224. "start_offset": 2,
  225. "end_offset": 5,
  226. "type": "word",
  227. "position": 0
  228. },
  229. {
  230. "token": "uic",
  231. "start_offset": 3,
  232. "end_offset": 6,
  233. "type": "word",
  234. "position": 1
  235. },
  236. {
  237. "token": "ick",
  238. "start_offset": 4,
  239. "end_offset": 7,
  240. "type": "word",
  241. "position": 2
  242. },
  243. {
  244. "token": "Fox",
  245. "start_offset": 8,
  246. "end_offset": 11,
  247. "type": "word",
  248. "position": 3
  249. },
  250. {
  251. "token": "oxe",
  252. "start_offset": 9,
  253. "end_offset": 12,
  254. "type": "word",
  255. "position": 4
  256. },
  257. {
  258. "token": "xes",
  259. "start_offset": 10,
  260. "end_offset": 13,
  261. "type": "word",
  262. "position": 5
  263. }
  264. ]
  265. }
  266. ----------------------------
  267. /////////////////////
  268. The above example produces the following terms:
  269. [source,text]
  270. ---------------------------
  271. [ Qui, uic, ick, Fox, oxe, xes ]
  272. ---------------------------