analysis-icu.asciidoc 14 KB

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