elasticsearch维护同义词

使用elasticsearch同义词的方式,可以对ik分词之后的词语进行同义词转化

王校长 => 王晓鹤

保存为txt文件low_weight_words.txt

创建索引

{

    "mappings": {
        "properties": {
            "_class": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "context": {
                "type": "text",
                "analyzer": "ik_low_weight"//下面创建的analyzer名称
            },
            "docDate": {
                "type": "date"
            },
            "docId": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "orgIdPerm": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "publicPerm": {
                "type": "boolean"
            },
            "title": {
                "type": "text",
                "analyzer": "ik_low_weight"
            },
            "userIdPerm": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "uuid": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    },
   "settings": {
        "analysis": {
            "filter": {
                "low_weight_filter": {
                    "type": "synonym",
                    "synonyms_path": "analysis/low_weight_words.txt",
                    "lenient": true
                }
            },
            "analyzer": {
                "ik_low_weight": {
                    "type": "custom",
                    "tokenizer": "ik_smart",
                    "filter": [
                        "low_weight_filter"
                    ]
                }
            }
        }
    }
}

这样我们通过搜索:王校长,就会转换成王晓鹤

同时我们还可以把不参与评分的词语放到stopword.dic文件中,并在IKAnalyzer.cfg.xml中配置路径

 

posted @ 2025-04-03 18:14  炫舞风中  阅读(8)  评论(0)    收藏  举报