刚学lucene,索引建立好了,搜索不到为什么呢?

肖邦 2012-02-18 04:51:13
1.indexer代码部分
public class Indexer {
public static void main(String[] args) throws Exception {

File indexDir = new File("E:\\build\\index");
File dataDir = new File("E:\\lucene");

long start = new Date().getTime();
int numIndexed = index(indexDir, dataDir);
long end = new Date().getTime();

System.out.println("Indexing" + numIndexed + "files took "+ (end - start) + " milliseconds");
}

// open an index and start file directory traversal
public static int index(File indexDir, File dataDir)
throws IOException {
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir
+ "does not exist or is not a directory");
}

IndexWriter writer = new IndexWriter(indexDir,new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);

indexDirectory(writer, dataDir);

int numIndexed = writer.docCount();
writer.optimize();
writer.close();
return numIndexed;
}

private static void indexDirectory(IndexWriter writer, File dir)
throws IOException {

File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(writer, f);
} else if (f.getName().endsWith(".txt")) {
indexFile(writer, f);
}
}
}

private static void indexFile(IndexWriter writer, File f)
throws IOException {

if (f.isHidden() || !f.exists() || !f.canRead()) {
return;
}
System.out.println("Indexing " + f.getCanonicalPath());

Document doc = new Document();

Reader txtReader = new FileReader(f);

doc.add(new Field("path",f.getCanonicalPath(),Field.Store.YES,Field.Index.UN_TOKENIZED));
doc.add(new Field("contents",txtReader));

writer.addDocument(doc); }
}

以上应该是执行正常的2.searcher代码部分 目录里已经有了我创建好的索引
public class Searcher {
public static void main(String[] args) throws Exception {

File indexDir = new File("E:\\build\\index");
String q = "Document";

if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir +
"does not exist or is not a directory.");
}
search(indexDir, q);
}
public static void search(File indexDir, String querystring)
throws Exception {
Directory fsDir = FSDirectory.getDirectory(indexDir, false);
IndexSearcher is = new IndexSearcher(fsDir);// ① 打开索引

// Query query = QueryParser.parse(q, "contents",new StandardAnalyzer()); // ② 分析查询

QueryParser parser = new QueryParser("content", new StandardAnalyzer());
Query query = parser.parse(querystring);

long start = new Date().getTime();
Hits hits = is.search(query); // ③ 搜索索引
long end = new Date().getTime();

System.err.println("Found " + hits.length() +
" document(s) (in "+ (end - start) +
"milliseconds) that matched query ‘" +
querystring + "’:");

for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i); // ④ 得到匹配的文档
System.out.println(doc.get("filename"));
}
}
}

searcher代码执行后查找的目录文件里(E:\lucene)我挑了很多有的单词搜索都输出没有找到.求大虾看看哪里出了问题?和字符编码应该没关系吧?坐等。。。
...全文
125 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqltwslt 2012-04-24
  • 打赏
  • 举报
回复
同求啊

62,614

社区成员

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

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