8.10 版本之前
1.設定同義詞檔案 config/analysis/synonyms.txt
a,b => c
a,b,c
第一種在分詞的時候,a,b都會解析成為c,然後把c存入索引中
第二種在分詞的時候,有a的地方,都會解析成a,b,c,把a,b,c存入索引中
第一種方法相比之下有個主詞,比較省索引。
2.定義mapping的時候新增到分詞器上
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": { // 自定義過濾器
"type": "synonym",
"updateable": true, // 能否動態更新
"synonyms_path": "analysis/synonyms.txt" // 近義詞檔案地址
}
},
"analyzer": {
"my_synonyms_analyzer": {
"tokenizer": "ik_smart",
"filter": [
"my_synonym_filter"
]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "my_synonyms_analyzer"
},
...
}
}
}
- 注意:如果檔案進行修改需要執行下面命令進行生效,多個document則需要每個都執行生效
POST /your_document_name/_reload_search_analyzers
8.10 版本之後
1.建立同義詞集
PUT _synonyms/my-synonyms
{
"synonyms_set": [
{
"id": "a",
"synonyms": "a,b => c"
},
{
"id": "a,b",
"synonyms": "a,b,c"
}
]
}
更新同義詞集
PUT _synonyms/my-synonyms-set/a
{
"synonyms": "a,b => d"
}
2.配置同義詞集
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": { // 自定義過濾器
"type": "synonym",
"updateable": true, // 能否動態更新
"synonyms_set": "my-synonyms", // 同義詞集
}
},
"analyzer": {
"my_synonyms_analyzer": {
"tokenizer": "ik_smart",
"filter": [
"my_synonym_filter"
]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "my_synonyms_analyzer"
},
...
}
}
}