analysis-icu.asciidoc 16 KB

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