lucene查询不到结果

zuiaizhujidan 2013-04-21 09:49:33
package edu.usc.uscnetopinion;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

public class HelloLucene {
public void index(){
Directory directory = null;
IndexWriterConfig iwc = null;
IndexWriter indexWriter = null;
try {
directory = FSDirectory.open(new File("E:\\J2EE\\hellolucene\\index0"));
iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
indexWriter = new IndexWriter(directory, iwc);
File files = new File("E:\\J2EE\\hellolucene\\example");
for(File file : files.listFiles()){
Document doc = new Document();
doc.add(new Field("content", new FileReader(file)));
doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.ANALYZED));
System.out.println(doc.getField("path"));
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(indexWriter != null){
try {
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public void searcher(){

try {
Directory directory = FSDirectory.open(new File("E:\\J2EE\\hellolucene\\index0"));
IndexReader reader = IndexReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse("java");
TopDocs tds = searcher.search(query, 10);
ScoreDoc[] sds = tds.scoreDocs;
System.out.println(sds.length);
for(ScoreDoc sd : sds){
Document d = searcher.doc(sd.doc);
System.out.println(d.get("filename") + " + " + d.get("path"));

}
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}

}
}


/*
打印出来的sds.length总是0啊。example文件夹放的是txt的文件,包含有要搜索的关键字的。index0文件夹存放索引
*/






...全文
173 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuiaizhujidan 2013-04-22
  • 打赏
  • 举报
回复
好吧,自己来结贴吧。少了一句话,改为如此即可 for(File file : files.listFiles()){ Document doc = new Document(); doc.add(new Field("content", new FileReader(file))); doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.ANALYZED)); indexWriter.addDocument(doc); System.out.println(doc.getField("path")); }

67,549

社区成员

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

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