Skip to content

Instantly share code, notes, and snippets.

@SergeiMeza
Created August 28, 2022 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergeiMeza/4eef702a14551c04c74de9b4020b7210 to your computer and use it in GitHub Desktop.
Save SergeiMeza/4eef702a14551c04c74de9b4020b7210 to your computer and use it in GitHub Desktop.
Elastic Search Japanese Index with autocomplete suggestions and full-text search
PUT ja
{
"settings": {
"analysis": {
// 1. custom character filters...
"char_filter": {
// 1-1. icu_normalizer nfkc: This handles the variation of full-width and half-width characters
"ja_normalize": {
"type": "icu_normalizer",
"name": "nfkc",
"mode": "compose"
},
// 1-2. mapping of kana to romaji
"kana_to_romaji": {
"type": "mapping",
"mappings": [
"あ=>a",
"い=>i",
"う=>u",
"え=>e",
"お=>o",
"か=>ka",
"き=>ki",
"く=>ku",
"け=>ke",
"こ=>ko",
"さ=>sa",
"し=>shi",
"す=>su",
"せ=>se",
"そ=>so",
"た=>ta",
"ち=>chi",
"つ=>tsu",
"て=>te",
"と=>to",
"な=>na",
"に=>ni",
"ぬ=>nu",
"ね=>ne",
"の=>no",
"は=>ha",
"ひ=>hi",
"ふ=>fu",
"へ=>he",
"ほ=>ho",
"ま=>ma",
"み=>mi",
"む=>mu",
"め=>me",
"も=>mo",
"や=>ya",
"ゆ=>yu",
"よ=>yo",
"ら=>ra",
"り=>ri",
"る=>ru",
"れ=>re",
"ろ=>ro",
"わ=>wa",
"を=>o",
"ん=>n",
"が=>ga",
"ぎ=>gi",
"ぐ=>gu",
"げ=>ge",
"ご=>go",
"ざ=>za",
"じ=>ji",
"ず=>zu",
"ぜ=>ze",
"ぞ=>zo",
"だ=>da",
"ぢ=>ji",
"づ=>zu",
"で=>de",
"ど=>do",
"ば=>ba",
"び=>bi",
"ぶ=>bu",
"べ=>be",
"ぼ=>bo",
"ぱ=>pa",
"ぴ=>pi",
"ぷ=>pu",
"ぺ=>pe",
"ぽ=>po",
"きゃ=>kya",
"きゅ=>kyu",
"きょ=>kyo",
"しゃ=>sha",
"しゅ=>shu",
"しょ=>sho",
"ちゃ=>cha",
"ちゅ=>chu",
"ちょ=>cho",
"にゃ=>nya",
"にゅ=>nyu",
"にょ=>nyo",
"ひゃ=>hya",
"ひゅ=>hyu",
"ひょ=>hyo",
"みゃ=>mya",
"みゅ=>myu",
"みょ=>myo",
"りゃ=>rya",
"りゅ=>ryu",
"りょ=>ryo",
"ぎゃ=>gya",
"ぎゅ=>gyu",
"ぎょ=>gyo",
"じゃ=>ja",
"じゅ=>ju",
"じょ=>jo",
"びゃ=>bya",
"びゅ=>byu",
"びょ=>byo",
"ぴゃ=>pya",
"ぴゅ=>pyu",
"ぴょ=>pyo",
"ふぁ=>fa",
"ふぃ=>fi",
"ふぇ=>fe",
"ふぉ=>fo",
"ふゅ=>fyu",
"うぃ=>wi",
"うぇ=>we",
"うぉ=>wo",
"つぁ=>tsa",
"つぃ=>tsi",
"つぇ=>tse",
"つぉ=>tso",
"ちぇ=>che",
"しぇ=>she",
"じぇ=>je",
"てぃ=>ti",
"でぃ=>di",
"でゅ=>du",
"とぅ=>tu",
"ぢゃ=>ja",
"ぢゅ=>ju",
"ぢょ=>jo",
"ぁ=>a",
"ぃ=>i",
"ぅ=>u",
"ぇ=>e",
"ぉ=>o",
"っ=>t",
"ゃ=>ya",
"ゅ=>yu",
"ょ=>yo",
"ア=>a",
"イ=>i",
"ウ=>u",
"エ=>e",
"オ=>o",
"カ=>ka",
"キ=>ki",
"ク=>ku",
"ケ=>ke",
"コ=>ko",
"サ=>sa",
"シ=>shi",
"ス=>su",
"セ=>se",
"ソ=>so",
"タ=>ta",
"チ=>chi",
"ツ=>tsu",
"テ=>te",
"ト=>to",
"ナ=>na",
"ニ=>ni",
"ヌ=>nu",
"ネ=>ne",
"ノ=>no",
"ハ=>ha",
"ヒ=>hi",
"フ=>fu",
"ヘ=>he",
"ホ=>ho",
"マ=>ma",
"ミ=>mi",
"ム=>mu",
"メ=>me",
"モ=>mo",
"ヤ=>ya",
"ユ=>yu",
"ヨ=>yo",
"ラ=>ra",
"リ=>ri",
"ル=>ru",
"レ=>re",
"ロ=>ro",
"ワ=>wa",
"ヲ=>o",
"ン=>n",
"ガ=>ga",
"ギ=>gi",
"グ=>gu",
"ゲ=>ge",
"ゴ=>go",
"ザ=>za",
"ジ=>ji",
"ズ=>zu",
"ゼ=>ze",
"ゾ=>zo",
"ダ=>da",
"ヂ=>ji",
"ヅ=>zu",
"デ=>de",
"ド=>do",
"バ=>ba",
"ビ=>bi",
"ブ=>bu",
"ベ=>be",
"ボ=>bo",
"パ=>pa",
"ピ=>pi",
"プ=>pu",
"ペ=>pe",
"ポ=>po",
"キャ=>kya",
"キュ=>kyu",
"キョ=>kyo",
"シャ=>sha",
"シュ=>shu",
"ショ=>sho",
"チャ=>cha",
"チュ=>chu",
"チョ=>cho",
"ニャ=>nya",
"ニュ=>nyu",
"ニョ=>nyo",
"ヒャ=>hya",
"ヒュ=>hyu",
"ヒョ=>hyo",
"ミャ=>mya",
"ミュ=>myu",
"ミョ=>myo",
"リャ=>rya",
"リュ=>ryu",
"リョ=>ryo",
"ギャ=>gya",
"ギュ=>gyu",
"ギョ=>gyo",
"ジャ=>ja",
"ジュ=>ju",
"ジョ=>jo",
"ビャ=>bya",
"ビュ=>byu",
"ビョ=>byo",
"ピャ=>pya",
"ピュ=>pyu",
"ピョ=>pyo",
"ファ=>fa",
"フィ=>fi",
"フェ=>fe",
"フォ=>fo",
"フュ=>fyu",
"ウィ=>wi",
"ウェ=>we",
"ウォ=>wo",
"ヴァ=>va",
"ヴィ=>vi",
"ヴ=>v",
"ヴェ=>ve",
"ヴォ=>vo",
"ツァ=>tsa",
"ツィ=>tsi",
"ツェ=>tse",
"ツォ=>tso",
"チェ=>che",
"シェ=>she",
"ジェ=>je",
"ティ=>ti",
"ディ=>di",
"デュ=>du",
"トゥ=>tu",
"ヂャ=>ja",
"ヂュ=>ju",
"ヂョ=>jo",
"ァ=>a",
"ィ=>i",
"ゥ=>u",
"ェ=>e",
"ォ=>o",
"ッ=>t",
"ャ=>ya",
"ュ=>yu",
"ョ=>yo"
]
}
},
// 2. custom tokenizers...
"tokenizer": {
// 2-1. kuromoji_tokenizer in normal mode to segment text
"ja_kuromoji_normal": {
"type": "kuromoji_tokenizer",
"mode": "normal"
},
// 2-2. kuromoji_tokenizer in search mode to divide words into smaller pieces for search
"ja_kuromoji_search": {
"type": "kuromoji_tokenizer",
"mode": "search",
"discard_compound_token": true
},
// 2-3. ngram for search
"ja_ngram": {
"type": "ngram",
"min_gram": 2,
"max_gram": 2,
"token_chars": [
"letter",
"digit"
]
}
},
// 3. custom token filters...
"filter": {
// 3-1. Transform token to an n-gram
"edge_ngram": {
"type": "edge_ngram",
"min_gram": "1",
"max_gram": "10"
},
// 3-2. Transform Japanese to readingform romaji
"readingform": {
"type": "kuromoji_readingform",
"use_romaji": "true"
}
},
// 4. custom analyzers...
"analyzer": {
// 4-1. standard autocomplete analyzer (used in usernames etc)
"standard_autocomplete_analyzer": {
"type": "custom",
"tokenizer": "standard", // standard tokenizer
"filter": [
"lowercase", // normalize alphabet to lowercase
"edge_ngram" // prefix search
]
},
// 4-2. Japanese suggestions index analyzer
"ja_suggest_index_analyzer": {
"type": "custom",
"char_filter": [
"ja_normalize"
],
"tokenizer": "ja_kuromoji_normal",
"filter": [
"lowercase", // normalize alphabet to lowercase
"edge_ngram" // for prefix search
]
},
// 4-3. Japanese suggestions search analyzer
"ja_suggest_search_analyzer": {
"type": "custom",
"char_filter": [
"ja_normalize"
],
"tokenizer": "ja_kuromoji_normal",
"filter": [
"lowercase"
]
},
// 4-4. Japanese readingform index analyzer
"ja_readingform_index_analyzer": {
"type": "custom",
"char_filter": [
"normalize",
"kana_to_romaji"
],
"tokenizer": "ja_kuromoji_normal",
"filter": [
"lowercase",
"readingform",
"asciifolding",
"edge_ngram"
]
},
// 4-5. Japanese readingform search analyzer
"ja_readingform_search_analyzer": {
"type": "custom",
"char_filter": [
"normalize",
"kana_to_romaji"
],
"tokenizer": "ja_kuromoji_normal",
"filter": [
"lowercase",
"readingform",
"asciifolding"
]
},
// 4-6. Full-text kuromoji Japanese index analyzer
"ja_kuromoji_index_analyzer": {
"type": "custom",
"char_filter": [
"ja_normalize"
],
"tokenizer": "ja_kuromoji_search",
"filter": [
"kuromoji_baseform", // handle Japanese adjectives and verbs
"kuromoji_part_of_speech", // handle Japanese stoptags
"cjk_width", // normalizes half-width and full-width characters
"ja_stop", // handles Japanese stop words
"kuromoji_stemmer", // handles katakana variations
"lowercase" // lowercases alphabet
]
},
// 4-7. Full-text kuromoji Japanese search analyzer
"ja_kuromoji_search_analyzer": {
"type": "custom",
"char_filter": [
"ja_normalize"
],
"tokenizer": "ja_kuromoji_search",
"filter": [
"kuromoji_baseform",
"kuromoji_part_of_speech",
"cjk_width",
"ja_stop",
"kuromoji_stemmer",
"lowercase"
]
},
// 4-8. Full-text ngram Japanese index analyzer
"ja_ngram_index_analyzer": {
"type": "custom",
"char_filter": [
"normalize"
],
"tokenizer": "ja_ngram",
"filter": [
"lowercase"
]
},
// 4-9. Full-text ngram Japanese search analyzer
"ja_ngram_search_analyzer": {
"type": "custom",
"char_filter": [
"normalize"
],
"tokenizer": "ja_ngram",
"filter": [
"lowercase"
]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment