论坛首页 入门讨论版 其他综合

懂Lucene的请进

浏览 528 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-11-22
最近在看Lucene,并且尝试了去做,遇到一个问题

在index的时候,使用的是FSDirectory 和 RAMDirectory相互使用来提高index效率。

可是我发现使用这种方法比我以前使用单一的FSDirectory搜索出来的东西要少很多(对于一个keyword所搜索出来的条目来说)。

不知道这是为什么,而且更改了临界条件的时候,搜索出来的结果也不同。

使用的算法如下:
FSDirectory fsDir = FSDirectory.getDirectory("/tmp/index",
true);
RAMDirectory ramDir = new RAMDirectory();
IndexWriter fsWriter = IndexWriter(fsDir,new SimpleAnalyzer(), true);
IndexWriter ramWriter = new IndexWriter(ramDir,new SimpleAnalyzer(), true);
while (there are documents to index) {
... create Document ...
ramWriter.addDocument(doc);
if (condition for flushing memory to disk has been met) {
fsWriter.addIndexes(Directory[] {ramDir});
ramWriter.close();
ramWriter = new IndexWriter(ramDir, new SimpleAnalyzer(),
true);
}
}
   
时间:2007-11-29
楼主举的例子是《lucene in action》书里的例子吧?写得很不详细麻,怎么帮你呢。
   
0 请登录后投票
时间:2007-11-29
是这样的,我自己有一个程序也是按照这个思路做的。
但是我发现使用了这种方法以后,index目录里面的文件的大小和原来使用fsDirectory的不一样了,要小很多了。是不是把他丢掉了?
   
0 请登录后投票
时间:2007-11-29
public void addAllHtml2Document(String dataDir) throws Exception
{
File dir = new File(dataDir);
File[] htmls = dir.listFiles();
IndexWriter ramWriter = new IndexWriter(ramDir,analyzer,true);

for(int i=0;i<htmls.length;i++)
{
String htmlPath = htmls[i].getAbsolutePath();

if(htmls[i].isDirectory())
{
addAllHtml2Document(htmlPath);
}
else
{
if(htmlPath.endsWith(".html") || htmlPath.endsWith(".htm"))
{
this.filesNum++;
addDocument(htmlPath,ramWriter);
if(this.filesNum > 200)
{
fsWriter.addIndexes(new Directory[]{ramDir});
ramWriter.close();
ramWriter = new IndexWriter(ramDir,analyzer,true);
}

}
}
}
}

public void addDocument(String htmlPath, IndexWriter indexWriter)
{
HTMLDocParser htmlParser = new HTMLDocParser(htmlPath);
String path = htmlParser.getPath();
String title = htmlParser.getTitle();
Reader content = htmlParser.getContent();

Document document = new Document();
document.add(new Field("path",path,Field.Store.YES,Field.Index.NO));
document.add(new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED));
document.add(new Field("content",content));
try
{
indexWriter.addDocument(document);
}
catch (IOException e)
{
e.printStackTrace();
}
}
   
0 请登录后投票
时间:2007-11-30
可能是ramWriter.close()之后,再用ramWriter = new IndexWriter(ramDir,analyzer,true)打开,旧的索引文件被替换了吧,没仔细瞧过,回去研究研究。
   
0 请登录后投票
时间:2007-12-12
还没人回答你吗?我看应该是这里的问题--
IndexWriter ramWriter = new IndexWriter(ramDir,analyzer,true);

通常,我们往indexer里添加document的时候都用false,而不是true,否则,原来的indexer文件会被覆盖。
   
0 请登录后投票
论坛首页 入门讨论版 其他综合

跳转论坛:
JavaEye推荐