edgengram-tokenizer.asciidoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. [[analysis-edgengram-tokenizer]]
  2. === Edge NGram Tokenizer
  3. The `edge_ngram` tokenizer first breaks text down into words whenever it
  4. encounters one of a list of specified characters, then it emits
  5. https://en.wikipedia.org/wiki/N-gram[N-grams] of each word where the start of
  6. the N-gram is anchored to the beginning of the word.
  7. Edge N-Grams are useful for _search-as-you-type_ queries.
  8. TIP: When you need _search-as-you-type_ for text which has a widely known
  9. order, such as movie or song titles, the
  10. <<completion-suggester,completion suggester>> is a much more efficient
  11. choice than edge N-grams. Edge N-grams have the advantage when trying to
  12. autocomplete words that can appear in any order.
  13. [float]
  14. === Example output
  15. With the default settings, the `edge_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,js]
  19. ---------------------------
  20. POST _analyze
  21. {
  22. "tokenizer": "edge_ngram",
  23. "text": "Quick Fox"
  24. }
  25. ---------------------------
  26. // CONSOLE
  27. /////////////////////
  28. [source,console-result]
  29. ----------------------------
  30. {
  31. "tokens": [
  32. {
  33. "token": "Q",
  34. "start_offset": 0,
  35. "end_offset": 1,
  36. "type": "word",
  37. "position": 0
  38. },
  39. {
  40. "token": "Qu",
  41. "start_offset": 0,
  42. "end_offset": 2,
  43. "type": "word",
  44. "position": 1
  45. }
  46. ]
  47. }
  48. ----------------------------
  49. /////////////////////
  50. The above sentence would produce the following terms:
  51. [source,text]
  52. ---------------------------
  53. [ Q, Qu ]
  54. ---------------------------
  55. NOTE: These default gram lengths are almost entirely useless. You need to
  56. configure the `edge_ngram` before using it.
  57. [float]
  58. === Configuration
  59. The `edge_ngram` tokenizer accepts the following parameters:
  60. [horizontal]
  61. `min_gram`::
  62. Minimum length of characters in a gram. Defaults to `1`.
  63. `max_gram`::
  64. Maximum length of characters in a gram. Defaults to `2`.
  65. `token_chars`::
  66. Character classes that should be included in a token. Elasticsearch
  67. will split on characters that don't belong to the classes specified.
  68. Defaults to `[]` (keep all characters).
  69. +
  70. Character classes may be any of the following:
  71. +
  72. * `letter` -- for example `a`, `b`, `ï` or `京`
  73. * `digit` -- for example `3` or `7`
  74. * `whitespace` -- for example `" "` or `"\n"`
  75. * `punctuation` -- for example `!` or `"`
  76. * `symbol` -- for example `$` or `√`
  77. [float]
  78. === Example configuration
  79. In this example, we configure the `edge_ngram` tokenizer to treat letters and
  80. digits as tokens, and to produce grams with minimum length `2` and maximum
  81. length `10`:
  82. [source,js]
  83. ----------------------------
  84. PUT my_index
  85. {
  86. "settings": {
  87. "analysis": {
  88. "analyzer": {
  89. "my_analyzer": {
  90. "tokenizer": "my_tokenizer"
  91. }
  92. },
  93. "tokenizer": {
  94. "my_tokenizer": {
  95. "type": "edge_ngram",
  96. "min_gram": 2,
  97. "max_gram": 10,
  98. "token_chars": [
  99. "letter",
  100. "digit"
  101. ]
  102. }
  103. }
  104. }
  105. }
  106. }
  107. POST my_index/_analyze
  108. {
  109. "analyzer": "my_analyzer",
  110. "text": "2 Quick Foxes."
  111. }
  112. ----------------------------
  113. // CONSOLE
  114. /////////////////////
  115. [source,console-result]
  116. ----------------------------
  117. {
  118. "tokens": [
  119. {
  120. "token": "Qu",
  121. "start_offset": 2,
  122. "end_offset": 4,
  123. "type": "word",
  124. "position": 0
  125. },
  126. {
  127. "token": "Qui",
  128. "start_offset": 2,
  129. "end_offset": 5,
  130. "type": "word",
  131. "position": 1
  132. },
  133. {
  134. "token": "Quic",
  135. "start_offset": 2,
  136. "end_offset": 6,
  137. "type": "word",
  138. "position": 2
  139. },
  140. {
  141. "token": "Quick",
  142. "start_offset": 2,
  143. "end_offset": 7,
  144. "type": "word",
  145. "position": 3
  146. },
  147. {
  148. "token": "Fo",
  149. "start_offset": 8,
  150. "end_offset": 10,
  151. "type": "word",
  152. "position": 4
  153. },
  154. {
  155. "token": "Fox",
  156. "start_offset": 8,
  157. "end_offset": 11,
  158. "type": "word",
  159. "position": 5
  160. },
  161. {
  162. "token": "Foxe",
  163. "start_offset": 8,
  164. "end_offset": 12,
  165. "type": "word",
  166. "position": 6
  167. },
  168. {
  169. "token": "Foxes",
  170. "start_offset": 8,
  171. "end_offset": 13,
  172. "type": "word",
  173. "position": 7
  174. }
  175. ]
  176. }
  177. ----------------------------
  178. /////////////////////
  179. The above example produces the following terms:
  180. [source,text]
  181. ---------------------------
  182. [ Qu, Qui, Quic, Quick, Fo, Fox, Foxe, Foxes ]
  183. ---------------------------
  184. Usually we recommend using the same `analyzer` at index time and at search
  185. time. In the case of the `edge_ngram` tokenizer, the advice is different. It
  186. only makes sense to use the `edge_ngram` tokenizer at index time, to ensure
  187. that partial words are available for matching in the index. At search time,
  188. just search for the terms the user has typed in, for instance: `Quick Fo`.
  189. Below is an example of how to set up a field for _search-as-you-type_:
  190. [source,js]
  191. -----------------------------------
  192. PUT my_index
  193. {
  194. "settings": {
  195. "analysis": {
  196. "analyzer": {
  197. "autocomplete": {
  198. "tokenizer": "autocomplete",
  199. "filter": [
  200. "lowercase"
  201. ]
  202. },
  203. "autocomplete_search": {
  204. "tokenizer": "lowercase"
  205. }
  206. },
  207. "tokenizer": {
  208. "autocomplete": {
  209. "type": "edge_ngram",
  210. "min_gram": 2,
  211. "max_gram": 10,
  212. "token_chars": [
  213. "letter"
  214. ]
  215. }
  216. }
  217. }
  218. },
  219. "mappings": {
  220. "properties": {
  221. "title": {
  222. "type": "text",
  223. "analyzer": "autocomplete",
  224. "search_analyzer": "autocomplete_search"
  225. }
  226. }
  227. }
  228. }
  229. PUT my_index/_doc/1
  230. {
  231. "title": "Quick Foxes" <1>
  232. }
  233. POST my_index/_refresh
  234. GET my_index/_search
  235. {
  236. "query": {
  237. "match": {
  238. "title": {
  239. "query": "Quick Fo", <2>
  240. "operator": "and"
  241. }
  242. }
  243. }
  244. }
  245. -----------------------------------
  246. // CONSOLE
  247. <1> The `autocomplete` analyzer indexes the terms `[qu, qui, quic, quick, fo, fox, foxe, foxes]`.
  248. <2> The `autocomplete_search` analyzer searches for the terms `[quick, fo]`, both of which appear in the index.
  249. /////////////////////
  250. [source,js]
  251. ----------------------------
  252. {
  253. "took": $body.took,
  254. "timed_out": false,
  255. "_shards": {
  256. "total": 1,
  257. "successful": 1,
  258. "skipped" : 0,
  259. "failed": 0
  260. },
  261. "hits": {
  262. "total" : {
  263. "value": 1,
  264. "relation": "eq"
  265. },
  266. "max_score": 0.5753642,
  267. "hits": [
  268. {
  269. "_index": "my_index",
  270. "_type": "_doc",
  271. "_id": "1",
  272. "_score": 0.5753642,
  273. "_source": {
  274. "title": "Quick Foxes"
  275. }
  276. }
  277. ]
  278. }
  279. }
  280. ----------------------------
  281. // TESTRESPONSE[s/"took".*/"took": "$body.took",/]
  282. /////////////////////