主要是初始化IndexWriter的时候, 不初始化新的, 而是先读Search目录. 附上源代码. 注释了的是我以前的老代码, 那时候不能进行增量. 470)this.style.width=470" align=top> using System; 470)this.style.width
主要是初始化IndexWriter的时候, 不初始化新的, 而是先读Search目录. 附上源代码.
注释了的是我以前的老代码, 那时候不能进行增量.
using System;

using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Analysis.Cn;
using Lucene.Net.Analysis.CJK;


namespace SearchEnginer
{

public sealed class Indexer : IDisposable
{
// Directory dir = null;
IndexWriter writer = null;


private Indexer()
{
}


public Indexer (string physicalPath, bool create, bool useTempDirectory)
{
// if(useTempDirectory) {
// physicalPath = System.IO.Path.Combine(physicalPath, "tempindex");
// }

ChineseAnalyzer analyzer = new ChineseAnalyzer();
// dir = FSDirectory.GetDirectory(physicalPath, create);

writer = GetWriter(physicalPath);//new IndexWriter(dir, analyzer, create);
writer.mergeFactor = 15;
}


public void AddDocument (Document doc)
{
writer.AddDocument(doc);
}


private IndexWriter GetWriter(string physicalPath)
{
IndexWriter indexWriter = null;
string segmentFile = System.IO.Path.Combine(physicalPath, "segments");

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);

return indexWriter;
}


public void Close()
{

if (!disposed)
{
// dir.Close();
writer.Optimize();
writer.Close();
disposed = true;
}
}

private bool disposed = false;


public void Dispose()
{
Close();
}
}
}
编辑推荐DotLucene搜索引擎文章列表:
全文搜索解决方案:DotLucene搜索引擎之创建索引
http://www.xueit.com/html/2009-02/21_606_00.html
DotLucene搜索引擎之搜索索引Demo
http://www.xueit.com/html/2009-02/21_607_00.html
全文搜索技术:dotLucene中文分词的highlight显示
http://www.xueit.com/html/2009-02/21_608_00.html
Lucene.NET增加中文分词
http://www.xueit.com/html/2009-02/21_609_00.html
全文搜索之Lucene增加中文分词功能方法
http://www.xueit.com/html/2009-02/21_610_00.html
简介下基于.NET的全文索引引擎Lucene.NET
http://www.xueit.com/html/2009-02/21_611_00.html
使用dotlucene为数据库建立全文索引
http://www.xueit.com/html/2009-02/21_612_00.html
使用dotlucene多条件检索数据库
http://www.xueit.com/html/2009-02/21_613_00.html
Lucene中文分词实现方法:基于StopWord分割分词
http://www.xueit.com/html/2009-02/21_614_00.html
dotLucene实现增量索引源代码
http://www.xueit.com/html/2009-02/21_615_00.html