在将lucene core版本从4.5.1升级到4.7.0后,如下代码使用ik分词器报错

IKAnalyzer analyzer = new IKAnalyzer(true);
StringReader reader=new StringReader(line);
TokenStream ts=analyzer.tokenStream("", reader);
CharTermAttribute term=ts.getAttribute(CharTermAttribute.class);
while(ts.incrementToken()){
...
}

异常信息:

java.lang.IllegalStateException: TokenStream contract violation: reset()/close() call missing, reset() called multiple times, or subclass does not call super.reset(). Please see Javadocs of TokenStream class for more information about the correct consuming workflow.

后来发现是lucene从4.6.0开始TokenStream使用方法更改的问题,在使用incrementToken方法前必须调用reset方法,详见api http://lucene.apache.org/core/4_6_0/core/index.html

The workflow of the new TokenStream API is as follows:

  1. Instantiation of TokenStream/TokenFilters which add/get attributes to/from the AttributeSource.
  2. The consumer calls reset().
  3. The consumer retrieves attributes from the stream and stores local references to all attributes it wants to access.
  4. The consumer calls incrementToken() until it returns false consuming the attributes after each call.
  5. The consumer calls end() so that any end-of-stream operations can be performed.
  6. The consumer calls close() to release any resource when finished using the TokenStream.

更改代码为如下运行正常

IKAnalyzer analyzer = new IKAnalyzer(true);
StringReader reader=new StringReader(line);
TokenStream ts=analyzer.tokenStream("", reader);
CharTermAttribute term=ts.getAttribute(CharTermAttribute.class);
ts.reset();
while(ts.incrementToken()){
...
}

最新文章

  1. linux系统下使用xampp 丢失mysql root密码【xampp的初始密码为空】
  2. iOS开发之第三方库的学习--hpple的使用
  3. CentOS7使用阿里云镜像安装Mongodb
  4. HTML5外包
  5. C#连接数据库的四种方法
  6. 分布式缓存系统热点key解决方案
  7. 使用Flexible 实现手淘H5 页面的终端适配学习
  8. W​i​n​D​B​G​调​试​技​巧
  9. 数据存储(两)--SAX发动机XML记忆(附Demo)
  10. 微信公众号Unauthorized API function
  11. linux中查看和开放端口
  12. RSA算法原理——(3)RSA加解密过程及公式论证
  13. Kafka数据迁移
  14. css格式比较及选择器类型总结
  15. python csv与字典操作
  16. HTML 滚动条样式修改
  17. perl debug
  18. EM算法理解
  19. VBA中字符串连接/字符串拼接中“&”和“+”的区别
  20. 像烟瘾一样的Adobe Flash,真的戒不掉吗?

热门文章

  1. C++ 编译器的安装(MinGW)
  2. 【记录】mybatis mapper.xml 基础
  3. VS2012在解决方案资源管理器显示解决方案名称
  4. Android App渗透测试工具汇总
  5. SpringBoot---事务支持
  6. 【HbuilerX-Bug】终端无法显示打印信息,也无法输入
  7. Task10.Bert
  8. Harbor在安装前的几个注意点
  9. python之面向过程,函数式编程,面向对象浅析
  10. centos mysql初探 -- 配置、基本操作及问题