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)我挑了很多有的单词搜索都输出没有找到.求大虾看看哪里出了问题?和字符编码应该没关系吧?坐等
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)我挑了很多有的单词搜索都输出没有找到.求大虾看看哪里出了问题?和字符编码应该没关系吧?坐等
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货