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

肖邦 肖邦 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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)我挑了很多有的单词搜索都输出没有找到.求大虾看看哪里出了问题?和字符编码应该没关系吧?坐等。。。
...全文
评论 2
收藏 已收藏
分享
返回顶部
肖邦
Java SE 62,621 成员 307,251 内容