使用示例 使用同义词搜索 Elasticsearch 7.x之前的版本和之后的版本,命令有差别,所以分开举例。 7.x之前的版本 1.创建索引“myindex”,配置分词策略。 PUT myindex { "settings": { "analysis": { "filter": { "mysynonym": { "type": "dynamicsynonym" } }, "analyzer": { "iksynonym": { "filter": [ "mysynonym" ], "type": "custom", "tokenizer": "iksmart" } } } }, "mappings": { "mytype" :{ "properties": { "desc": { "type": "text", "analyzer": "iksynonym" } } } } } 2.导入数据,将文本信息导入“myindex”索引中。 PUT /myindex/mytype/1 { "desc": "我今天获奖了我很开心" } 3.使用同义词“高兴”进行文本搜索,并查看搜索结果。 GET /myindex/search { "query": { "match": { "desc": "高兴" } } } 搜索结果: { "took" : 2, "timedout" : false, "shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "maxscore" : 0.49445358, "hits" : [ { "index" : "myindex", "type" : "mytype", "id" : "1", "score" : 0.49445358, "source" : { "desc" : "我今天获奖了我很开心" } } ] } } 7.x及之后的版本 1.创建索引“myindex”,配置分词策略。 PUT myindex { "settings": { "analysis": { "filter": { "mysynonym": { "type": "dynamicsynonym" } }, "analyzer": { "iksynonym": { "filter": [ "mysynonym" ], "type": "custom", "tokenizer": "iksmart" } } } }, "mappings": { "properties": { "desc": { "type": "text", "analyzer": "iksynonym" } } } } 2.导入数据,将文本信息导入“myindex”索引中。 PUT /myindex/doc/1 { "desc": "我今天获奖了我很开心" } 3.使用同义词“高兴”进行文本搜索,并查看搜索结果。 GET /myindex/search { "query": { "match": { "desc": "高兴" } } } 搜索结果: { "took" : 1, "timedout" : false, "shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "maxscore" : 0.1519955, "hits" : [ { "index" : "myindex", "type" : "doc", "id" : "1", "score" : 0.1519955, "source" : { "desc" : "我今天获奖了我很开心" } } ] } }