不知道以前, 觉得很难. 实现以后觉得太容易了.tongue_smile.gif

主要是初始化IndexWriter的时候, 不初始化新的, 而是先读Search目录. 附上源代码.
注释了的是我以前的老代码, 那时候不能进行增量.

None.gif using  System;
None.gif
None.gif
using  Lucene.Net.Documents;
None.gif
using  Lucene.Net.Index;
None.gif
using  Lucene.Net.Store;
None.gif
using  Lucene.Net.Analysis.Cn;
None.gif
using  Lucene.Net.Analysis.CJK;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace  SearchEnginer  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public sealed class Indexer : IDisposable dot.gif{
InBlock.gif    
//    Directory dir = null;
InBlock.gif
        IndexWriter writer = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private Indexer() dot.gif{
ExpandedSubBlockEnd.gif        }

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

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

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void AddDocument (Document doc) dot.gif{
InBlock.gif            writer.AddDocument(doc);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private IndexWriter GetWriter(string physicalPath) dot.gif{
InBlock.gif            IndexWriter indexWriter 
= null;
InBlock.gif            
InBlock.gif            
string segmentFile = System.IO.Path.Combine(physicalPath, "segments");
InBlock.gif
InBlock.gif            
if ( System.IO.File.Exists(segmentFile) )
InBlock.gif                indexWriter 
= new IndexWriter(physicalPath, new Lucene.Net.Analysis.Cn.ChineseAnalyzer(), false);
InBlock.gif            
else
InBlock.gif                indexWriter 
= new IndexWriter(physicalPath, new Lucene.Net.Analysis.Cn.ChineseAnalyzer(), true);
InBlock.gif
InBlock.gif            
return indexWriter;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void Close() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (!disposed) dot.gif{
InBlock.gif            
//    dir.Close();
InBlock.gif
                writer.Optimize();
InBlock.gif                writer.Close();
InBlock.gif                disposed 
= true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private bool disposed = false;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void Dispose() dot.gif{
InBlock.gif            Close();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


转载于:https://www.cnblogs.com/cnnix/archive/2006/07/06/444312.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐