IKAnalyzer分词测试不成功!

whos2002110 2014-07-17 10:02:03
lucene是最新版本4.9, demo代码基本没有改动, 但查询结果却不正确


public class IKAnalyzerDemo {
public static void main(String[] args) {
String fieldName = "text";

String text = "IK Analyzer是一个结合词典分词和文法分词的中文分词开源工具包。它使用了全新的正向迭代最细粒度切分算法。";

Analyzer analyzer = new IKAnalyzer(true);
//Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);

Directory directory = null;
IndexWriter iwriter = null;
IndexReader ireader = null;
IndexSearcher isearcher = null;
try {
directory = new RAMDirectory();

IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);
iwConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
iwriter = new IndexWriter(directory, iwConfig);

Document doc = new Document();
doc.add(new StringField(fieldName, text, Field.Store.YES));
iwriter.addDocument(doc);
iwriter.close();

ireader = DirectoryReader.open(directory);
isearcher = new IndexSearcher(ireader);

// String keyword = "Analyzer";
String keyword = "中文分词工具包";

QueryParser qp = new QueryParser(Version.LUCENE_4_9, fieldName, analyzer);
qp.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query = qp.parse(keyword);
System.out.println("Query = " + query);

TopDocs topDocs = isearcher.search(query, 5);
System.out.println("命中:" + topDocs.totalHits);

ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < topDocs.totalHits; i++) {
Document targetDoc = isearcher.doc(scoreDocs[i].doc);
System.out.println("内容:" + targetDoc.toString());
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
if (ireader != null) {
try {
ireader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

不管怎么改, 结果总是:

命中:0

不知道问题出在哪里!!
...全文
243 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LipipiFighting 2016-05-12
  • 打赏
  • 举报
回复
https://github.com/kweima/IKAnalyzer5 亲测,lucene6.0可用。
whos2002110 2014-07-17
  • 打赏
  • 举报
回复
引用 3 楼 shixitong 的回复:
doc.add(new StringField(fieldName, text, Field.Store.YES));
替换为
doc.add(new Field(fieldName, text, Field.Store.YES, Index.ANALYZED));
引用 4 楼 luotitan 的回复:
lucene4.9 StringField默认不分词 document.add(new StringField("name", user.getName(), Store.YES)); 改成 document.add(new TextField("name", user.getName(), Store.YES)); TextField默认分词
是这个问题,不知道是不是网络的原因, 我结贴的时候还没有看到你俩的回复, 所以没分了
luotitan 2014-07-17
  • 打赏
  • 举报
回复
lucene4.9 StringField默认不分词 document.add(new StringField("name", user.getName(), Store.YES)); 改成 document.add(new TextField("name", user.getName(), Store.YES)); TextField默认分词
shixitong 2014-07-17
  • 打赏
  • 举报
回复
doc.add(new StringField(fieldName, text, Field.Store.YES));
替换为
doc.add(new Field(fieldName, text, Field.Store.YES, Index.ANALYZED));
whos2002110 2014-07-17
  • 打赏
  • 举报
回复

isearcher = new IndexSearcher(ireader);
System.out.println(isearcher.doc(0).get(fieldName));
这样可以把text内容打印出来, 说明数据已经索引了, 但是isearcher.search(query, 5); 却查询不到! 我猜想不是中文分词不正确的原因, 因为当我查询英文的时候也一样的结果。 甚至把分词器换为StandardAnalyzer后 也同样查询不到数据,不管中文还是英文
tony4geek 2014-07-17
  • 打赏
  • 举报
回复
lucene 没用过。帮顶。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧