当前位置: 首页 > elasticsearch, 搜索 > 正文

ElasticSearchException[failed to analyze]; nested: IOException[Stream closed]

1 星2 星3 星4 星5 星 (1 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

昨天自己开发了一个elasticsearch ik分词的插件,部署在测试环境上,运行一下命令:

http://localhost:9200/test/_analyze?analyzer=ik&text=%E7%BE%8E%E5%9B%BD%E9%81%AD%E9%BE%99%E5%8D%B7%E9%A3%8E

报错:

1
{"error":"ElasticSearchException[failed to analyze]; nested: IOException[Stream closed]; ","status":500}

log日志出现以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[2013-05-30 17:49:31,806][DEBUG][action.admin.indices.analyze] [Whizzer] failed to execute [org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest@b11a9f]
org.elasticsearch.ElasticSearchException: failed to analyze
	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.shardOperation(TransportAnalyzeAction.java:218)
	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.shardOperation(TransportAnalyzeAction.java:57)
	at org.elasticsearch.action.support.single.custom.TransportSingleCustomOperationAction$AsyncSingleAction$2.run(TransportSingleCustomOperationAction.java:175)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:722)
Caused by: java.io.IOException: Stream closed
	at org.elasticsearch.common.io.FastStringReader.ensureOpen(FastStringReader.java:53)
	at org.elasticsearch.common.io.FastStringReader.read(FastStringReader.java:98)
	at java.io.Reader.read(Reader.java:140)
	at org.wltea.analyzer.core.AnalyzeContext.fillBuffer(AnalyzeContext.java:124)
	at org.wltea.analyzer.core.IKSegmenter.next(IKSegmenter.java:122)
	at org.wltea.analyzer.lucene.ESIKtokenizer.incrementToken(ESIKtokenizer.java:42)
	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.shardOperation(TransportAnalyzeAction.java:209)
	... 5 more

TransportAnalyzeAction.java:209 对应的源码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 try {
            stream = analyzer.tokenStream(field, new FastStringReader(request.text()));
            stream.reset();
            CharTermAttribute term = stream.addAttribute(CharTermAttribute.class);
            PositionIncrementAttribute posIncr = stream.addAttribute(PositionIncrementAttribute.class);
            OffsetAttribute offset = stream.addAttribute(OffsetAttribute.class);//209行
            TypeAttribute type = stream.addAttribute(TypeAttribute.class);
 
            int position = 0;
            while (stream.incrementToken()) {
                int increment = posIncr.getPositionIncrement();
                if (increment > 0) {
                    position = position + increment;
                }
                tokens.add(new AnalyzeResponse.AnalyzeToken(term.toString(), position, offset.startOffset(), offset.endOffset(), type.type()));
            }
            stream.end();
        } catch (IOException e) {
            throw new ElasticSearchException("failed to analyze", e);
        }

注意到当时209行,TypeAttribute 属性,感觉好像自己源码没有添加。查看源码,自己封装的ESIKTokenizer按照lucene3的去处理了,按照lucene4的处理就搞定啦。
错误关键代码:

1
2
3
4
5
6
	public ESIKtokenizer(Reader in , boolean useSmart){
	    super(in);
	    offsetAtt = addAttribute(OffsetAttribute.class);
	    termAtt = addAttribute(CharTermAttribute.class);
		_IKImplement = new IKSegmenter(in , ESConfig.getInstance());
	}

正确的代码为:

1
2
3
4
5
6
7
	public ESIKTokenizer(Reader in , boolean useSmart){
	    super(in);
	    offsetAtt = addAttribute(OffsetAttribute.class);
	    termAtt = addAttribute(CharTermAttribute.class);
	    typeAtt = addAttribute(TypeAttribute.class);
		_IKImplement = new IKSegmenter(input , ESConfig.getInstance());
	}

本文固定链接: http://www.chepoo.com/elasticsearch-exception-failed-to-analyze.html | IT技术精华网

ElasticSearchException[failed to analyze]; nested: IOException[Stream closed]:等您坐沙发呢!

发表评论