关于Lucene搜索不到结果问题!!这是一个入门的小栗子...我被难住了..求指点迷津

qq_21369103 2015-12-06 12:00:37
实在是不知道要发在那个版块了!!

出现的问题:我想要搜索fileContent力的内容!但是却搜不到文档对象......
topDocs长度为0

首先是创建索引!

public void createIndex()
{
boolean create = true;
Directory directory = null;
IndexWriter indexWriter = null;
try
{
File fold = new File("e:\\TEMP\\lucene\\db");
// //1.创建目录
// directory = new SimpleFSDirectory(fold);
directory = FSDirectory.open(fold);

//2.创建索引
/*2.2*/
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
/*2.1*/
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
if (create)
{
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
} else
{
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
}
/*2.0*/
indexWriter = new IndexWriter(directory, indexWriterConfig);
//3.创建Document
Document document = null;

File file = new File("E:\\TEMP\\Lucene");

for (File txtFile : file.listFiles())
{
if (txtFile.isFile())
{
//4.为Document 添加Field
document = new Document();
document.add(new Field("fileName", txtFile.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("filePath", txtFile.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("fileContent", new BufferedReader(new FileReader(txtFile))));
}
}
//5.通过IndexWriter 添加到文档索引中
indexWriter.addDocument(document);
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (indexWriter != null)
{
indexWriter.close();
}

if (directory != null)
{
directory.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}


}

public void searcher()
{
Directory d = null;
IndexReader ir = null;
IndexSearcher is = null;
OutputStreamWriter osw = null;

try
{
//1.创建Directory
d = FSDirectory.open(new File("e:\\TEMP\\lucene\\db"));

//2.创建IndexReader
ir = IndexReader.open(d);

//3.根据IndexReader 创建 IndexSearcher
is = new IndexSearcher(ir);
//3.创建搜索对象 Query
StandardAnalyzer sdr = new StandardAnalyzer(Version.LUCENE_35);
QueryParser qp = new QueryParser(Version.LUCENE_35, "fileName", sdr);
Query qr = qp.parse("t");
//4.根据serarch并且返回TopDocs
TopDocs topDocs = is.search(qr, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("文档数:" + scoreDocs.length);
//5.遍历TopDocs
osw = new OutputStreamWriter(System.out);
// for (ScoreDoc sd : scoreDocs)
// {
// //5.1得到该document对象
// Document doc = is.doc(sd.doc);
// List<Fieldable> fieldableList = doc.getFields();
// for (Iterator<Fieldable> iterator = fieldableList.iterator(); iterator.hasNext(); )
// {
// Fieldable next = iterator.next();
// Reader reader = next.readerValue();
// //6.获取所要的值
// char[] cbf = new char[1024];
// while(reader.read(cbf) > 0)
// {
// osw.write(cbf);
// System.out.println(1);
// }
// }
// }
//请忽略掉以上注释部分
//6.获取所要的值

} catch (Exception e)
{
e.printStackTrace();
} finally
{
//7.关闭Reader流
try
{
if (osw != null)
{
osw.close();
}

if (is != null)
{
is.close();
}

if (ir != null)
{
ir.close();
}
if (d != null) {
d.close();
}
} catch (IOException e)
{
System.out.println(e.toString());
}
}

}
}



然后是搜索

public void searcher()
{
Directory d = null;
IndexReader ir = null;
IndexSearcher is = null;
OutputStreamWriter osw = null;

try
{
//1.创建Directory
d = FSDirectory.open(new File("e:\\TEMP\\lucene\\db"));

//2.创建IndexReader
ir = IndexReader.open(d);

//3.根据IndexReader 创建 IndexSearcher
is = new IndexSearcher(ir);
//3.创建搜索对象 Query
StandardAnalyzer sdr = new StandardAnalyzer(Version.LUCENE_35);
QueryParser qp = new QueryParser(Version.LUCENE_35, "fileName", sdr);
Query qr = qp.parse("t");
//4.根据serarch并且返回TopDocs
TopDocs topDocs = is.search(qr, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("文档数:" + scoreDocs.length);
//........
} catch (Exception e)
{
e.printStackTrace();
} finally
{
//7.关闭流
try
{
if (osw != null)
{
osw.close();
}

if (is != null)
{
is.close();
}

if (ir != null)
{
ir.close();
}
if (d != null) {
d.close();
}
} catch (IOException e)
{
System.out.println(e.toString());
}
}

}
}


出现的问题:我想要搜索fileContent力的内容!但是却搜不到文档对象......
topDocs长度为0

最后我向请问的是,我写的这个代码是哪里出错了呢?
关于Lucene实在是了解的太少了!!

我只想从fileContent里查询到想要的结果!!!!
但是在视频里的老师确实是可以查询到的!!!
琢磨了好几小时没效果,现在发帖求助..
如果你可以帮助到我,且有闲暇时间,我下方提供了我现在学习Lucene的资源:

http://pan.baidu.com/s/1c1w6Uvm


...全文
55 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,091

社区成员

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

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