1
0

ngram-tokenizer.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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,console-result]
  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. /////////////////////
  152. The above sentence would produce the following terms:
  153. [source,text]
  154. ---------------------------
  155. [ Q, Qu, u, ui, i, ic, c, ck, k, "k ", " ", " F", F, Fo, o, ox, x ]
  156. ---------------------------
  157. [float]
  158. === Configuration
  159. The `ngram` tokenizer accepts the following parameters:
  160. [horizontal]
  161. `min_gram`::
  162. Minimum length of characters in a gram. Defaults to `1`.
  163. `max_gram`::
  164. Maximum length of characters in a gram. Defaults to `2`.
  165. `token_chars`::
  166. Character classes that should be included in a token. Elasticsearch
  167. will split on characters that don't belong to the classes specified.
  168. Defaults to `[]` (keep all characters).
  169. +
  170. Character classes may be any of the following:
  171. +
  172. * `letter` -- for example `a`, `b`, `ï` or `京`
  173. * `digit` -- for example `3` or `7`
  174. * `whitespace` -- for example `" "` or `"\n"`
  175. * `punctuation` -- for example `!` or `"`
  176. * `symbol` -- for example `$` or `√`
  177. TIP: It usually makes sense to set `min_gram` and `max_gram` to the same
  178. value. The smaller the length, the more documents will match but the lower
  179. the quality of the matches. The longer the length, the more specific the
  180. matches. A tri-gram (length `3`) is a good place to start.
  181. The index level setting `index.max_ngram_diff` controls the maximum allowed
  182. difference between `max_gram` and `min_gram`.
  183. [float]
  184. === Example configuration
  185. In this example, we configure the `ngram` tokenizer to treat letters and
  186. digits as tokens, and to produce tri-grams (grams of length `3`):
  187. [source,js]
  188. ----------------------------
  189. PUT my_index
  190. {
  191. "settings": {
  192. "analysis": {
  193. "analyzer": {
  194. "my_analyzer": {
  195. "tokenizer": "my_tokenizer"
  196. }
  197. },
  198. "tokenizer": {
  199. "my_tokenizer": {
  200. "type": "ngram",
  201. "min_gram": 3,
  202. "max_gram": 3,
  203. "token_chars": [
  204. "letter",
  205. "digit"
  206. ]
  207. }
  208. }
  209. }
  210. }
  211. }
  212. POST my_index/_analyze
  213. {
  214. "analyzer": "my_analyzer",
  215. "text": "2 Quick Foxes."
  216. }
  217. ----------------------------
  218. // CONSOLE
  219. /////////////////////
  220. [source,console-result]
  221. ----------------------------
  222. {
  223. "tokens": [
  224. {
  225. "token": "Qui",
  226. "start_offset": 2,
  227. "end_offset": 5,
  228. "type": "word",
  229. "position": 0
  230. },
  231. {
  232. "token": "uic",
  233. "start_offset": 3,
  234. "end_offset": 6,
  235. "type": "word",
  236. "position": 1
  237. },
  238. {
  239. "token": "ick",
  240. "start_offset": 4,
  241. "end_offset": 7,
  242. "type": "word",
  243. "position": 2
  244. },
  245. {
  246. "token": "Fox",
  247. "start_offset": 8,
  248. "end_offset": 11,
  249. "type": "word",
  250. "position": 3
  251. },
  252. {
  253. "token": "oxe",
  254. "start_offset": 9,
  255. "end_offset": 12,
  256. "type": "word",
  257. "position": 4
  258. },
  259. {
  260. "token": "xes",
  261. "start_offset": 10,
  262. "end_offset": 13,
  263. "type": "word",
  264. "position": 5
  265. }
  266. ]
  267. }
  268. ----------------------------
  269. /////////////////////
  270. The above example produces the following terms:
  271. [source,text]
  272. ---------------------------
  273. [ Qui, uic, ick, Fox, oxe, xes ]
  274. ---------------------------