analysis-icu.asciidoc 15 KB

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