0. 数据准备
1. es什么情况下调用分词?
1、写入
同步对写入的text 字段进行分词,然后进行相关后续存储
断点下到 : org.apache.lucene.analysis.standard.StandardTokenizer#incrementToken 查看调用链。 可以看到从org.apache.lucene.index.DefaultIndexingChain#processField 调用到分词方法。
2、查询:
同步对输入的条件进行分词。 分词后去词典表以及倒排列表进行查询。
断点下到 : org.apache.lucene.analysis.standard.StandardTokenizer#incrementToken 查看调用链。 可以看到从org.elasticsearch.index.search.MatchQueryParser#parse 调用过去。
2. es调用分词返回的结果是什么?
返回结果:是一个对象,包含当前的词、起始位置、结束位置等信息,用于es 建立倒排列表。
1、 自己curl 测试
2、代码测试
查看单个返回的词信息:
3. 自定义自己的分词插件
脱离ES 只用两个组件: Analyzer、Tokenizer。Analyzer是一个更全面的概念,涵盖了文本分析的整个流程,包括但不限于分词(比如过滤等精细化处理)。Tokenizer则专注于分词这一特定步骤。
集成到es需要2个组件:AbstractIndexAnalyzerProvider、AnalysisPlugin,(测试不需要 AbstractTokenizerFactory 也能正常使用)
// 参考项目: https://gitee.com/Qiao-Zhi/custom_analyzer_es_plugin
4. 自己的分词插件调用其他分词插件如何实现-扩展分词?
1、 org.apache.lucene.analysis.Tokenizer#reset 重置的时候可以拿到当前需要分词的词, 然后processAnalyzer(sb.toString()); 分词完成缓存到对象属性
2、org.apache.lucene.analysis.TokenStream#incrementToken 遍历上面分词结果返回
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】