analysis-icu.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. [[analysis-icu]]
  2. === ICU Analysis Plugin
  3. The ICU Analysis plugin integrates the Lucene ICU module into elasticsearch,
  4. adding extended Unicode support using the http://site.icu-project.org/[ICU]
  5. libraries, including better analysis of Asian languages, Unicode
  6. normalization, Unicode-aware case folding, collation support, and
  7. transliteration.
  8. [[analysis-icu-install]]
  9. [float]
  10. ==== Installation
  11. This plugin can be installed using the plugin manager:
  12. [source,sh]
  13. ----------------------------------------------------------------
  14. sudo bin/elasticsearch-plugin install analysis-icu
  15. ----------------------------------------------------------------
  16. The plugin must be installed on every node in the cluster, and each node must
  17. be restarted after installation.
  18. [[analysis-icu-remove]]
  19. [float]
  20. ==== Removal
  21. The plugin can be removed with the following command:
  22. [source,sh]
  23. ----------------------------------------------------------------
  24. sudo bin/elasticsearch-plugin remove analysis-icu
  25. ----------------------------------------------------------------
  26. The node must be stopped before removing the plugin.
  27. [[analysis-icu-normalization-charfilter]]
  28. ==== ICU Normalization Character Filter
  29. Normalizes characters as explained
  30. http://userguide.icu-project.org/transforms/normalization[here].
  31. It registers itself as the `icu_normalizer` character filter, which is
  32. available to all indices without any further configuration. The type of
  33. normalization can be specified with the `name` parameter, which accepts `nfc`,
  34. `nfkc`, and `nfkc_cf` (default). Set the `mode` parameter to `decompose` to
  35. convert `nfc` to `nfd` or `nfkc` to `nfkd` respectively:
  36. Here are two examples, the default usage and a customised character filter:
  37. [source,js]
  38. --------------------------------------------------
  39. PUT icu_sample
  40. {
  41. "settings": {
  42. "index": {
  43. "analysis": {
  44. "analyzer": {
  45. "nfkc_cf_normalized": { <1>
  46. "tokenizer": "icu_tokenizer",
  47. "char_filter": [
  48. "icu_normalizer"
  49. ]
  50. },
  51. "nfd_normalized": { <2>
  52. "tokenizer": "icu_tokenizer",
  53. "char_filter": [
  54. "nfd_normalizer"
  55. ]
  56. }
  57. },
  58. "char_filter": {
  59. "nfd_normalizer": {
  60. "type": "icu_normalizer",
  61. "name": "nfc",
  62. "mode": "decompose"
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. // CONSOLE
  71. <1> Uses the default `nfkc_cf` normalization.
  72. <2> Uses the customized `nfd_normalizer` token filter, which is set to use `nfc` normalization with decomposition.
  73. [[analysis-icu-tokenizer]]
  74. ==== ICU Tokenizer
  75. Tokenizes text into words on word boundaries, as defined in
  76. http://www.unicode.org/reports/tr29/[UAX #29: Unicode Text Segmentation].
  77. It behaves much like the {ref}/analysis-standard-tokenizer.html[`standard` tokenizer],
  78. but adds better support for some Asian languages by using a dictionary-based
  79. approach to identify words in Thai, Lao, Chinese, Japanese, and Korean, and
  80. using custom rules to break Myanmar and Khmer text into syllables.
  81. [source,js]
  82. --------------------------------------------------
  83. PUT icu_sample
  84. {
  85. "settings": {
  86. "index": {
  87. "analysis": {
  88. "analyzer": {
  89. "my_icu_analyzer": {
  90. "tokenizer": "icu_tokenizer"
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. --------------------------------------------------
  98. // CONSOLE
  99. ===== Rules customization
  100. experimental[]
  101. You can customize the `icu-tokenizer` behavior by specifying per-script rule files, see the
  102. http://userguide.icu-project.org/boundaryanalysis#TOC-RBBI-Rules[RBBI rules syntax reference]
  103. for a more detailed explanation.
  104. To add icu tokenizer rules, set the `rule_files` settings, which should contain a comma-separated list of
  105. `code:rulefile` pairs in the following format:
  106. http://unicode.org/iso15924/iso15924-codes.html[four-letter ISO 15924 script code],
  107. followed by a colon, then a rule file name. Rule files are placed `ES_HOME/config` directory.
  108. As a demonstration of how the rule files can be used, save the following user file to `$ES_HOME/config/KeywordTokenizer.rbbi`:
  109. [source,text]
  110. -----------------------
  111. .+ {200};
  112. -----------------------
  113. Then create an analyzer to use this rule file as follows:
  114. [source,js]
  115. --------------------------------------------------
  116. PUT icu_sample
  117. {
  118. "settings": {
  119. "index":{
  120. "analysis":{
  121. "tokenizer" : {
  122. "icu_user_file" : {
  123. "type" : "icu_tokenizer",
  124. "rule_files" : "Latn:KeywordTokenizer.rbbi"
  125. }
  126. },
  127. "analyzer" : {
  128. "my_analyzer" : {
  129. "type" : "custom",
  130. "tokenizer" : "icu_user_file"
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. POST icu_sample/_analyze?analyzer=my_analyzer&text=Elasticsearch. Wow!
  138. --------------------------------------------------
  139. // CONSOLE
  140. The above `analyze` request returns the following:
  141. [source,js]
  142. --------------------------------------------------
  143. {
  144. "tokens": [
  145. {
  146. "token": "Elasticsearch. Wow!",
  147. "start_offset": 0,
  148. "end_offset": 19,
  149. "type": "<ALPHANUM>",
  150. "position": 0
  151. }
  152. ]
  153. }
  154. --------------------------------------------------
  155. // TESTRESPONSE
  156. [[analysis-icu-normalization]]
  157. ==== ICU Normalization Token Filter
  158. Normalizes characters as explained
  159. http://userguide.icu-project.org/transforms/normalization[here]. It registers
  160. itself as the `icu_normalizer` token filter, which is available to all indices
  161. without any further configuration. The type of normalization can be specified
  162. with the `name` parameter, which accepts `nfc`, `nfkc`, and `nfkc_cf`
  163. (default).
  164. You should probably prefer the <<analysis-icu-normalization-charfilter,Normalization character filter>>.
  165. Here are two examples, the default usage and a customised token filter:
  166. [source,js]
  167. --------------------------------------------------
  168. PUT icu_sample
  169. {
  170. "settings": {
  171. "index": {
  172. "analysis": {
  173. "analyzer": {
  174. "nfkc_cf_normalized": { <1>
  175. "tokenizer": "icu_tokenizer",
  176. "filter": [
  177. "icu_normalizer"
  178. ]
  179. },
  180. "nfc_normalized": { <2>
  181. "tokenizer": "icu_tokenizer",
  182. "filter": [
  183. "nfc_normalizer"
  184. ]
  185. }
  186. },
  187. "filter": {
  188. "nfc_normalizer": {
  189. "type": "icu_normalizer",
  190. "name": "nfc"
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. --------------------------------------------------
  198. // CONSOLE
  199. <1> Uses the default `nfkc_cf` normalization.
  200. <2> Uses the customized `nfc_normalizer` token filter, which is set to use `nfc` normalization.
  201. [[analysis-icu-folding]]
  202. ==== ICU Folding Token Filter
  203. Case folding of Unicode characters based on `UTR#30`, like the
  204. {ref}/analysis-asciifolding-tokenfilter.html[ASCII-folding token filter]
  205. on steroids. It registers itself as the `icu_folding` token filter and is
  206. available to all indices:
  207. [source,js]
  208. --------------------------------------------------
  209. PUT icu_sample
  210. {
  211. "settings": {
  212. "index": {
  213. "analysis": {
  214. "analyzer": {
  215. "folded": {
  216. "tokenizer": "icu_tokenizer",
  217. "filter": [
  218. "icu_folding"
  219. ]
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. --------------------------------------------------
  227. // CONSOLE
  228. The ICU folding token filter already does Unicode normalization, so there is
  229. no need to use Normalize character or token filter as well.
  230. Which letters are folded can be controlled by specifying the
  231. `unicodeSetFilter` parameter, which accepts a
  232. http://icu-project.org/apiref/icu4j/com/ibm/icu/text/UnicodeSet.html[UnicodeSet].
  233. The following example exempts Swedish characters from folding. It is important
  234. to note that both upper and lowercase forms should be specified, and that
  235. these filtered character are not lowercased which is why we add the
  236. `lowercase` filter as well:
  237. [source,js]
  238. --------------------------------------------------
  239. PUT icu_sample
  240. {
  241. "settings": {
  242. "index": {
  243. "analysis": {
  244. "analyzer": {
  245. "swedish_analyzer": {
  246. "tokenizer": "icu_tokenizer",
  247. "filter": [
  248. "swedish_folding",
  249. "lowercase"
  250. ]
  251. }
  252. },
  253. "filter": {
  254. "swedish_folding": {
  255. "type": "icu_folding",
  256. "unicodeSetFilter": "[^åäöÅÄÖ]"
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. --------------------------------------------------
  264. // CONSOLE
  265. [[analysis-icu-collation]]
  266. ==== ICU Collation Token Filter
  267. Collations are used for sorting documents in a language-specific word order.
  268. The `icu_collation` token filter is available to all indices and defaults to
  269. using the
  270. https://www.elastic.co/guide/en/elasticsearch/guide/current/sorting-collations.html#uca[DUCET collation],
  271. which is a best-effort attempt at language-neutral sorting.
  272. Below is an example of how to set up a field for sorting German names in
  273. ``phonebook'' order:
  274. [source,js]
  275. --------------------------------------------------
  276. PUT /my_index
  277. {
  278. "settings": {
  279. "analysis": {
  280. "filter": {
  281. "german_phonebook": {
  282. "type": "icu_collation",
  283. "language": "de",
  284. "country": "DE",
  285. "variant": "@collation=phonebook"
  286. }
  287. },
  288. "analyzer": {
  289. "german_phonebook": {
  290. "tokenizer": "keyword",
  291. "filter": [ "german_phonebook" ]
  292. }
  293. }
  294. }
  295. },
  296. "mappings": {
  297. "user": {
  298. "properties": {
  299. "name": { <1>
  300. "type": "text",
  301. "fields": {
  302. "sort": { <2>
  303. "type": "text",
  304. "fielddata": true,
  305. "analyzer": "german_phonebook"
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. GET _search <3>
  314. {
  315. "query": {
  316. "match": {
  317. "name": "Fritz"
  318. }
  319. },
  320. "sort": "name.sort"
  321. }
  322. --------------------------------------------------
  323. // CONSOLE
  324. <1> The `name` field uses the `standard` analyzer, and so support full text queries.
  325. <2> The `name.sort` field uses the `keyword` analyzer to preserve the name as
  326. a single token, and applies the `german_phonebook` token filter to index
  327. the value in German phonebook sort order.
  328. <3> An example query which searches the `name` field and sorts on the `name.sort` field.
  329. ===== Collation options
  330. `strength`::
  331. The strength property determines the minimum level of difference considered
  332. significant during comparison. Possible values are : `primary`, `secondary`,
  333. `tertiary`, `quaternary` or `identical`. See the
  334. http://icu-project.org/apiref/icu4j/com/ibm/icu/text/Collator.html[ICU Collation documentation]
  335. for a more detailed explanation for each value. Defaults to `tertiary`
  336. unless otherwise specified in the collation.
  337. `decomposition`::
  338. Possible values: `no` (default, but collation-dependent) or `canonical`.
  339. Setting this decomposition property to `canonical` allows the Collator to
  340. handle unnormalized text properly, producing the same results as if the text
  341. were normalized. If `no` is set, it is the user's responsibility to insure
  342. that all text is already in the appropriate form before a comparison or before
  343. getting a CollationKey. Adjusting decomposition mode allows the user to select
  344. between faster and more complete collation behavior. Since a great many of the
  345. world's languages do not require text normalization, most locales set `no` as
  346. the default decomposition mode.
  347. The following options are expert only:
  348. `alternate`::
  349. Possible values: `shifted` or `non-ignorable`. Sets the alternate handling for
  350. strength `quaternary` to be either shifted or non-ignorable. Which boils down
  351. to ignoring punctuation and whitespace.
  352. `caseLevel`::
  353. Possible values: `true` or `false` (default). Whether case level sorting is
  354. required. When strength is set to `primary` this will ignore accent
  355. differences.
  356. `caseFirst`::
  357. Possible values: `lower` or `upper`. Useful to control which case is sorted
  358. first when case is not ignored for strength `tertiary`. The default depends on
  359. the collation.
  360. `numeric`::
  361. Possible values: `true` or `false` (default) . Whether digits are sorted
  362. according to their numeric representation. For example the value `egg-9` is
  363. sorted before the value `egg-21`.
  364. `variableTop`::
  365. Single character or contraction. Controls what is variable for `alternate`.
  366. `hiraganaQuaternaryMode`::
  367. Possible values: `true` or `false`. Distinguishing between Katakana and
  368. Hiragana characters in `quaternary` strength.
  369. [[analysis-icu-transform]]
  370. ==== ICU Transform Token Filter
  371. Transforms are used to process Unicode text in many different ways, such as
  372. case mapping, normalization, transliteration and bidirectional text handling.
  373. You can define which transformation you want to apply with the `id` parameter
  374. (defaults to `Null`), and specify text direction with the `dir` parameter
  375. which accepts `forward` (default) for LTR and `reverse` for RTL. Custom
  376. rulesets are not yet supported.
  377. For example:
  378. [source,js]
  379. --------------------------------------------------
  380. PUT icu_sample
  381. {
  382. "settings": {
  383. "index": {
  384. "analysis": {
  385. "analyzer": {
  386. "latin": {
  387. "tokenizer": "keyword",
  388. "filter": [
  389. "myLatinTransform"
  390. ]
  391. }
  392. },
  393. "filter": {
  394. "myLatinTransform": {
  395. "type": "icu_transform",
  396. "id": "Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC" <1>
  397. }
  398. }
  399. }
  400. }
  401. }
  402. }
  403. GET icu_sample/_analyze?analyzer=latin
  404. {
  405. "text": "你好" <2>
  406. }
  407. GET icu_sample/_analyze?analyzer=latin
  408. {
  409. "text": "здравствуйте" <3>
  410. }
  411. GET icu_sample/_analyze?analyzer=latin
  412. {
  413. "text": "こんにちは" <4>
  414. }
  415. --------------------------------------------------
  416. // CONSOLE
  417. <1> This transforms transliterates characters to Latin, and separates accents
  418. from their base characters, removes the accents, and then puts the
  419. remaining text into an unaccented form.
  420. <2> Returns `ni hao`.
  421. <3> Returns `zdravstvujte`.
  422. <4> Returns `kon'nichiha`.
  423. For more documentation, Please see the http://userguide.icu-project.org/transforms/general[user guide of ICU Transform].