Lucene.Net最高版本为3.0.3,并且apache已经不再提供Lucene.Net的更新,没仔细研究过Lucene.Net的所有版本,Lucene.Net3.0.3遍历TokenStream获取Token对象,已经和以前的版本有了很大的区别,很多方法都已经删除了或者过时。
以前版本的Lucene.Net从TokenStream中获取Token时调用Next方法就行了,源代码如下
public void ReusableTokenStream2() { string testwords = "web开发网"; SimpleAnalyzer simple = new SimpleAnalyzer(); TokenStream ts = simple.ReusableTokenStream("", new StringReader(testwords)); Token token; while ((token = ts.Next()) != null) { Console.WriteLine(token.TermText()); } ts.Close(); }
但是在Lucene.Net3.0.3中TokenStream已经不存在Next方法,而是需要ITermAttribute属性接口来遍历。源代码如下
public void ReusableTokenStream3() { Lucene.Net.Analysis.Standard.StandardAnalyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); string s = "web开发网"; System.IO.StringReader reader = new System.IO.StringReader(s); Lucene.Net.Analysis.TokenStream ts = a.TokenStream(s, reader); bool hasnext = ts.IncrementToken(); Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita; while (hasnext) { ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>(); Console.WriteLine(ita.Term); hasnext = ts.IncrementToken(); } ts.CloneAttributes(); reader.Close(); a.Close(); Console.ReadKey(); }