edgengram-tokenizer.asciidoc 8.1 KB

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