analysis-icu.asciidoc 15 KB

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