edgengram-tokenizer.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. [[analysis-edgengram-tokenizer]]
  2. === Edge n-gram 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. `min_gram`::
  60. Minimum length of characters in a gram. Defaults to `1`.
  61. `max_gram`::
  62. +
  63. --
  64. Maximum length of characters in a gram. Defaults to `2`.
  65. See <<max-gram-limits>>.
  66. --
  67. `token_chars`::
  68. Character classes that should be included in a token. Elasticsearch
  69. will split on characters that don't belong to the classes specified.
  70. Defaults to `[]` (keep all characters).
  71. +
  72. Character classes may be any of the following:
  73. +
  74. * `letter` -- for example `a`, `b`, `ï` or `京`
  75. * `digit` -- for example `3` or `7`
  76. * `whitespace` -- for example `" "` or `"\n"`
  77. * `punctuation` -- for example `!` or `"`
  78. * `symbol` -- for example `$` or `√`
  79. * `custom` -- custom characters which need to be set using the
  80. `custom_token_chars` setting.
  81. `custom_token_chars`::
  82. Custom characters that should be treated as part of a token. For example,
  83. setting this to `+-_` will make the tokenizer treat the plus, minus and
  84. underscore sign as part of a token.
  85. [float]
  86. [[max-gram-limits]]
  87. === Limitations of the `max_gram` parameter
  88. The `edge_ngram` tokenizer's `max_gram` value limits the character length of
  89. tokens. When the `edge_ngram` tokenizer is used with an index analyzer, this
  90. means search terms longer than the `max_gram` length may not match any indexed
  91. terms.
  92. For example, if the `max_gram` is `3`, searches for `apple` won't match the
  93. indexed term `app`.
  94. To account for this, you can use the
  95. <<analysis-truncate-tokenfilter,`truncate`>> token filter with a search analyzer
  96. to shorten search terms to the `max_gram` character length. However, this could
  97. return irrelevant results.
  98. For example, if the `max_gram` is `3` and search terms are truncated to three
  99. characters, the search term `apple` is shortened to `app`. This means searches
  100. for `apple` return any indexed terms matching `app`, such as `apply`, `snapped`,
  101. and `apple`.
  102. We recommend testing both approaches to see which best fits your
  103. use case and desired search experience.
  104. [float]
  105. === Example configuration
  106. In this example, we configure the `edge_ngram` tokenizer to treat letters and
  107. digits as tokens, and to produce grams with minimum length `2` and maximum
  108. length `10`:
  109. [source,console]
  110. ----------------------------
  111. PUT my_index
  112. {
  113. "settings": {
  114. "analysis": {
  115. "analyzer": {
  116. "my_analyzer": {
  117. "tokenizer": "my_tokenizer"
  118. }
  119. },
  120. "tokenizer": {
  121. "my_tokenizer": {
  122. "type": "edge_ngram",
  123. "min_gram": 2,
  124. "max_gram": 10,
  125. "token_chars": [
  126. "letter",
  127. "digit"
  128. ]
  129. }
  130. }
  131. }
  132. }
  133. }
  134. POST my_index/_analyze
  135. {
  136. "analyzer": "my_analyzer",
  137. "text": "2 Quick Foxes."
  138. }
  139. ----------------------------
  140. /////////////////////
  141. [source,console-result]
  142. ----------------------------
  143. {
  144. "tokens": [
  145. {
  146. "token": "Qu",
  147. "start_offset": 2,
  148. "end_offset": 4,
  149. "type": "word",
  150. "position": 0
  151. },
  152. {
  153. "token": "Qui",
  154. "start_offset": 2,
  155. "end_offset": 5,
  156. "type": "word",
  157. "position": 1
  158. },
  159. {
  160. "token": "Quic",
  161. "start_offset": 2,
  162. "end_offset": 6,
  163. "type": "word",
  164. "position": 2
  165. },
  166. {
  167. "token": "Quick",
  168. "start_offset": 2,
  169. "end_offset": 7,
  170. "type": "word",
  171. "position": 3
  172. },
  173. {
  174. "token": "Fo",
  175. "start_offset": 8,
  176. "end_offset": 10,
  177. "type": "word",
  178. "position": 4
  179. },
  180. {
  181. "token": "Fox",
  182. "start_offset": 8,
  183. "end_offset": 11,
  184. "type": "word",
  185. "position": 5
  186. },
  187. {
  188. "token": "Foxe",
  189. "start_offset": 8,
  190. "end_offset": 12,
  191. "type": "word",
  192. "position": 6
  193. },
  194. {
  195. "token": "Foxes",
  196. "start_offset": 8,
  197. "end_offset": 13,
  198. "type": "word",
  199. "position": 7
  200. }
  201. ]
  202. }
  203. ----------------------------
  204. /////////////////////
  205. The above example produces the following terms:
  206. [source,text]
  207. ---------------------------
  208. [ Qu, Qui, Quic, Quick, Fo, Fox, Foxe, Foxes ]
  209. ---------------------------
  210. Usually we recommend using the same `analyzer` at index time and at search
  211. time. In the case of the `edge_ngram` tokenizer, the advice is different. It
  212. only makes sense to use the `edge_ngram` tokenizer at index time, to ensure
  213. that partial words are available for matching in the index. At search time,
  214. just search for the terms the user has typed in, for instance: `Quick Fo`.
  215. Below is an example of how to set up a field for _search-as-you-type_.
  216. Note that the `max_gram` value for the index analyzer is `10`, which limits
  217. indexed terms to 10 characters. Search terms are not truncated, meaning that
  218. search terms longer than 10 characters may not match any indexed terms.
  219. [source,console]
  220. -----------------------------------
  221. PUT my_index
  222. {
  223. "settings": {
  224. "analysis": {
  225. "analyzer": {
  226. "autocomplete": {
  227. "tokenizer": "autocomplete",
  228. "filter": [
  229. "lowercase"
  230. ]
  231. },
  232. "autocomplete_search": {
  233. "tokenizer": "lowercase"
  234. }
  235. },
  236. "tokenizer": {
  237. "autocomplete": {
  238. "type": "edge_ngram",
  239. "min_gram": 2,
  240. "max_gram": 10,
  241. "token_chars": [
  242. "letter"
  243. ]
  244. }
  245. }
  246. }
  247. },
  248. "mappings": {
  249. "properties": {
  250. "title": {
  251. "type": "text",
  252. "analyzer": "autocomplete",
  253. "search_analyzer": "autocomplete_search"
  254. }
  255. }
  256. }
  257. }
  258. PUT my_index/_doc/1
  259. {
  260. "title": "Quick Foxes" <1>
  261. }
  262. POST my_index/_refresh
  263. GET my_index/_search
  264. {
  265. "query": {
  266. "match": {
  267. "title": {
  268. "query": "Quick Fo", <2>
  269. "operator": "and"
  270. }
  271. }
  272. }
  273. }
  274. -----------------------------------
  275. <1> The `autocomplete` analyzer indexes the terms `[qu, qui, quic, quick, fo, fox, foxe, foxes]`.
  276. <2> The `autocomplete_search` analyzer searches for the terms `[quick, fo]`, both of which appear in the index.
  277. /////////////////////
  278. [source,console-result]
  279. ----------------------------
  280. {
  281. "took": $body.took,
  282. "timed_out": false,
  283. "_shards": {
  284. "total": 1,
  285. "successful": 1,
  286. "skipped" : 0,
  287. "failed": 0
  288. },
  289. "hits": {
  290. "total" : {
  291. "value": 1,
  292. "relation": "eq"
  293. },
  294. "max_score": 0.5753642,
  295. "hits": [
  296. {
  297. "_index": "my_index",
  298. "_id": "1",
  299. "_score": 0.5753642,
  300. "_source": {
  301. "title": "Quick Foxes"
  302. }
  303. }
  304. ]
  305. }
  306. }
  307. ----------------------------
  308. // TESTRESPONSE[s/"took".*/"took": "$body.took",/]
  309. /////////////////////