您的位置首页  散文随感

这都可以?实现的近义词(实现近义词是什么词)

自定义分词同义词这个细节官方文档解释的不细,死磕一把,探个究竟\x26gt;\x26gt;

这都可以?实现的近义词(实现近义词是什么词)

 

1、问题引出球友认证考试前一天晚上提问:

扩展背景描述:这是 Elasticsearch 自定义分词 Text analysis 章节 Token filter reference 小节的 同义词 token 过滤 (Synonym token filter)的一个参数。

的确,官方文档讲解的不详细我们彻底解读一下2、自定义分词基础认知如果读者们对自定义分词的实现有疑问,推荐阅读:Elasticsearch自定义分词,从一个问题说开去3、给个实战例子,一探究竟3.1 自定义分词 DSL。

PUT test_index{"settings": {"index": {"analysis": {"analyzer": {"synonym": {"tokenizer": "whitespace"

,"filter": ["synonym"            ]          }        },"filter": {"synonym": {"type": "synonym","lenient"

: true,"synonyms": ["ipod, i-pod, i pod"            ]          }        }      }    }  },"mappings": {

"properties": {"title": {"type": "text","analyzer": "synonym"      }    }  }}解读如下:

如图所示,结合前面的 DSL 定义自定义分词器的名字:synonymtokenizer 使用:whitespace 空白分词器filter 也就是 token filter 使用:synonym 同义词实现。

而空白分隔分词器的本质,举例如下:POST _analyze{"analyzer": "whitespace","text": "The 2 QUICK Brown-Foxes jumped over the lazy dogs bone."

}分词后的结果为:The2QUICKBrown-Foxesjumpedoverthelazydogsbone.概括一下:空格分词器在遇到空格字符时会将文本分解为多个词,仅以空格作为分词分隔单元3.2  token filter 细节参数解读。

参数 1 —— "type": "synonym"含义:同义词类型参数 2 ——"lenient": true含义:如果为 true,则在解析同义词配置时会忽略异常重要的是要注意,只有那些无法解析的同义词规则才被忽略。

默认值:false参数 3 —— "synonyms"指定同义词,也就是指定谁和谁成为同义词参数4 ——“expand ”这个就是文章开头提及的参数,缺省值就是 true所以上述 DSL 并没有显示指定。

那参数:expand 到底啥意思呢?4、通过对比,认清 expand 含义4.1 验证 DSLPOST test_index/_doc/1{"title":"ipod is my love"}POST test_index/_doc/2

{"title":"i-pod is my love"}POST test_index/_doc/3{"title":"i pod is my love"}POST test_index/_search

{"query": {"match": {"title": "ipod"    }  }}POST test_index/_search{"query": {"match": {"title": "i-pod"

    }  }}POST test_index/_search{"query": {"match": {"title": "i pod"    }  }}4.2 expand设置为 true默认值,无需指定即可生效。

分词验证一把:POST test_index/_analyze{"text":"i pod is my love","analyzer": "synonym"}结论截图如下:

执行 4.1 的 DSL,检索任何一个,全量三个文档都能返回ipod, i-pod, i pod 的含义如下:具体意思:ipod 等价于  ipod, i-pod, i pod 三个的每一个i-pod等价于  ipod, i-pod, i pod 三个的每一个。

i pod 等价于  ipod, i-pod, i pod 三个的每一个而,"ipod, i-pod, i pod => ipod"的含义如下:POST test_index/_analyze{"text"。

:"i pod is my love","analyzer": "synonym"}

进一步解读为:ipod 仅等价于  ipod,i-pod 仅等价于  ipod,i pod 仅等价于  ipod,4.3 expand设置为 falseDELETE test_indexPUT test_index。

{"settings": {"index": {"analysis": {"analyzer": {"synonym": {"tokenizer": "whitespace","filter": ["synonym"

            ]          }        },"filter": {"synonym": {"type": "synonym","expand":false,"lenient": 

true,"synonyms": ["ipod, i-pod, i pod"            ]          }        }      }    }  },"mappings": {"properties"

: {"title": {"type": "text","analyzer": "synonym"      }    }  }}POST test_index/_analyze{"text":"i pod is my love"

,"analyzer": "synonym"}POST test_index/_analyze{"text":"i-pod is my love","analyzer": "synonym"}上述最后两个 analyze 分词结果为:

ipod, i-pod, i pod 的含义如下:ipod, i-pod, i pod 和 ipod 等价具体意思:ipod  仅 等价于  ipod一个i-pod  仅 等价于  ipod一个i pod 仅 等价于  ipod一个。

"ipod, i-pod, i pod => ipod"的含义同 4.2 解读,不再赘述。5、有了结论再看官方文档

expand 是扩展的意思如果为 true,支持扩展,和每一个同义词是同义词如果为 false,不支持扩展,仅和最后指向的同义词同义6、小结实践一把,探究真相!和你一起,死磕 Elastic!Elastic 核心知识点脑图解读,助力快速建立全局知识体系!。

推荐:如何系统的学习 Elasticsearch ?从实战中来,到实战中去——Elasticsearch 技能更快提升方法论重磅 | 死磕 Elasticsearch 方法论认知清单(2020年国庆更新版)

死磕 Elasticsearch 方法论:普通程序员高效精进的 10 大狠招!

中国最大的 Elastic 非官方公众号更短时间更快习得更多干货点击查看“阅读原文”,更短时间更快习得更多干货和全球 1000 位+ Elastic 爱好者(含中国 50%+ Elastic 认证工程师)一起每日精进 ELK 技能!。

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。联系QQ:1640731186