初学 lucene 还望高手指点一二??

xiaoxiao8372 2007-08-01 11:31:52
public class TestLucene{
public static void main(String[] args) throws Exception{
//indexDir is the directory that hosts Lucene's index files
File indexDir = new File("D:\\luceneIndex");
//dataDir is the directory that hosts the text files that to be indexed
File dataDir = new File("D:\\luceneData");
Analyzer luceneAnalyzer = new StandardAnalyzer();
File[] dataFiles = dataDir.listFiles();
IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
long startTime = new Date().getTime();
Document document = new Document();
for(int i = 0; i < dataFiles.length; i++){
if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){
System.out.println("Indexing file " + dataFiles[i].getCanonicalPath());
Reader txtReader = new FileReader(dataFiles[i]);
document.add(Field.Text("path",dataFiles[i].getCanonicalPath()));
document.add(Field.Text("contents",txtReader));
indexWriter.addDocument(document);
}
}
indexWriter.optimize();
indexWriter.close();
long endTime = new Date().getTime();

System.out.println("It takes " + (endTime - startTime)
+ " milliseconds to create index for the files in directory "
+ dataDir.getPath());
}
}
此类为创建索引

public class TxtLuceneFileSearcher {
public static void main(String[] args) throws Exception{
String queryStr = "from";
//This is the directory that hosts the Lucene index
File indexDir = new File("D:\\luceneIndex");
FSDirectory directory = FSDirectory.getDirectory(indexDir,false);
IndexSearcher searcher = new IndexSearcher(directory);
if(!indexDir.exists()){
System.out.println("The Lucene index is not exist");
return;
}
Term term = new Term("contents",queryStr.toLowerCase());
TermQuery luceneQuery = new TermQuery(term);
Hits hits = searcher.search(luceneQuery);
System.out.println(hits.length());
for(int i = 0; i < hits.length(); i++){
Document document = hits.doc(i);
System.out.println("File: " + document.get("path"));
System.out.println("File: " + document.get("contents"));

}
}
}
此类打印出搜索到的文档的路径
1 打印出来的文档路径不是按照搜索关键字出现次数的大小排的序啊!
2 我搜索关键字“to” "the" 等都找不到。文本里绝对有这些个关键字的啊!!!
3 System.out.println("File: " + document.get("contents"));这句话应该是打印出来 文档吧,可是总显示null
高手给指点一下吧。有好的lucene教程给推荐一下啊

...全文
127 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiao8372 2007-08-01
  • 打赏
  • 举报
回复
关于他排序的问题你也没告诉我啊。
xiaoxiao8372 2007-08-01
  • 打赏
  • 举报
回复
呵呵,谢谢了
xuhong_lee 2007-08-01
  • 打赏
  • 举报
回复
StandardAnalyzer将“to” "the" ,"a","an" "for"等一些无实际意义的单词跳过了

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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