不知道以前, 觉得很难. 实现以后觉得太容易了.![](/Emoticons/tongue_smile.gif)
主要是初始化IndexWriter的时候, 不初始化新的, 而是先读Search目录. 附上源代码.
注释了的是我以前的老代码, 那时候不能进行增量.
using System;
![](/Images/OutliningIndicators/None.gif)
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Analysis.Cn;
using Lucene.Net.Analysis.CJK;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace SearchEnginer
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public sealed class Indexer : IDisposable
{
// Directory dir = null;
IndexWriter writer = null;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private Indexer()
{
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Indexer (string physicalPath, bool create, bool useTempDirectory)
{
// if(useTempDirectory) {
// physicalPath = System.IO.Path.Combine(physicalPath, "tempindex");
// }
![](/Images/OutliningIndicators/InBlock.gif)
ChineseAnalyzer analyzer = new ChineseAnalyzer();
// dir = FSDirectory.GetDirectory(physicalPath, create);
![](/Images/OutliningIndicators/InBlock.gif)
writer = GetWriter(physicalPath);//new IndexWriter(dir, analyzer, create);
writer.mergeFactor = 15;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void AddDocument (Document doc)
{
writer.AddDocument(doc);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private IndexWriter GetWriter(string physicalPath)
{
IndexWriter indexWriter = null;
string segmentFile = System.IO.Path.Combine(physicalPath, "segments");
![](/Images/OutliningIndicators/InBlock.gif)
if ( System.IO.File.Exists(segmentFile) )
indexWriter = new IndexWriter(physicalPath, new Lucene.Net.Analysis.Cn.ChineseAnalyzer(), false);
else
indexWriter = new IndexWriter(physicalPath, new Lucene.Net.Analysis.Cn.ChineseAnalyzer(), true);
![](/Images/OutliningIndicators/InBlock.gif)
return indexWriter;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void Close()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!disposed)
{
// dir.Close();
writer.Optimize();
writer.Close();
disposed = true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private bool disposed = false;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void Dispose()
{
Close();
}
}
}
![](/Images/OutliningIndicators/None.gif)
参考: http://info.hustonline.net/info/document/doc.aspx?ID=639