我对文件夹里的xml文件建立了索引,但是为什么就检索不到呢,请各位大侠指点指点:
建立索引的类:LuceneIndexLocalDisk
package Test;
import java.io.IOException;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.*;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/*******************************************************************
* 本代码完成本地指定目录的遍历和文件查找。对指定后缀的文件进行分析,利用Lucene建立
* 索引,为后续检索使用做好准备。
*******************************************************************/
public class LuceneIndexLocalDisk {
private static String Dest_Index_Path = "D:\\jy\\";
private static String Text_File_Path = "D:\\jy\\";
/*========================================================
* 主函数,指定索引目录和待分析的目录,生成Lucene索引
*========================================================*/
public static void main(String[] args) {
File indexpath = new File(Dest_Index_Path);
File localPath = new File(Text_File_Path);
try {
int nums = indexBuilder(indexpath,localPath);
System.out.println("Index Finished " + nums + " docs");
} catch (IOException e) {
e.printStackTrace();
}
}
/*========================================================
* 索引创建函数,生成IndexWriter创建索引,调用子目录索引函数,并优化
* 存储本地磁盘索引
*========================================================*/
public static int indexBuilder( File indexPath , File localPath )
throws IOException{
if(!localPath.exists() || !localPath.isDirectory() || !localPath.canRead()){
throw new IOException(localPath + "不存在或者不允许访问" );
}
System.out.println("目标路径完好");
IndexWriter FSWriter = new IndexWriter(indexPath,new StandardAnalyzer(),true);
FSWriter.setUseCompoundFile(true);
SubindexBuilder(FSWriter,localPath);
int num = FSWriter.docCount();
FSWriter.optimize();
FSWriter.close();
return num;
}
/*========================================================
* 判断当前文件名是否符合文件后缀要求
*========================================================*/
private static boolean IsValidType(String name){
if(name.endsWith(".xml"))
{
return true;
} else {
return false;
}
}
/*========================================================
* 处理各种不同类型文档,调用相应的参数,合并到本地磁盘索引当中
*========================================================*/
private static void fileindexBuilder(IndexWriter fswriter,File subfile)
throws IOException{
if( subfile.isHidden() || !subfile.exists() || !subfile.canRead()){
return ;
}
String strname = subfile.getName();
int dotpos = strname.indexOf(".");
HandleXml hnxml=new HandleXml();
if( (dotpos >0) && (dotpos < strname.length()))
{
hnxml.handle(fswriter ,subfile);
}
}
/*========================================================
* 递归函数,递归分析目录,如果找到子目录,继续递归;如果找到文件分析索引
*========================================================*/
private static void SubindexBuilder(IndexWriter fswriter,File subPath)