edgengram-tokenizer.asciidoc 6.6 KB

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