private void CreateIndexButton_Click(object sender, EventArgs e) { java.nio.file.Path idxPath = FileSystems.getDefault().getPath(IndexDir); FSDirectory dir = FSDirectory.Open(idxPath); JapaneseAnalyzer analyzer = new JapaneseAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter writer = new IndexWriter(dir, config); string[] files = System.IO.Directory.GetFiles(this.TargetDirText.Text, "*.htm*", System.IO.SearchOption.AllDirectories); try { foreach (string file in files) { string title, content, f; title = ""; content = ""; f = file; HTMLParse(ref title, ref content, ref f); Field fldTitle = new StringField("title", title, FieldStore.YES); Field fldPlace = new StringField("place", f, FieldStore.YES); Field fldContent = new TextField("content", content, FieldStore.YES); Document doc = new Document(); doc.Add(fldTitle); doc.Add(fldPlace); doc.Add(fldContent); writer.AddDocument(doc); } } catch (System.IO.IOException ex) { System.Console.WriteLine(ex.ToString()); } writer.Close(); }
/// <summary> /// RAMDirectoryのインデックスをFSIndexとして保存する /// </summary> /// <param name="ramDir"></param> /// <param name="savePath"></param> /// <param name="analyzer"></param> private static void SaveFSIndexFromRAMIndex(FlexLucene.Store.Directory ramDir, string savePath, Analyzer analyzer) { var fsIndexDir = FSDirectory.Open(FileSystems.getDefault().getPath(savePath)); var fsConfig = new IndexWriterConfig(analyzer); IndexWriter fsIndexWriter = new IndexWriter(fsIndexDir, fsConfig); try { fsIndexWriter.AddIndexes(new FlexLucene.Store.Directory[] { ramDir }); fsIndexWriter.Commit(); } finally { fsIndexWriter.Close(); } }
public int GetDocumentCount(string idxDirPath) { FSDirectory fsIdxDirPath = null; IndexReader ir = null; try { fsIdxDirPath = FSDirectory.Open(FileSystems.getDefault().getPath(idxDirPath)); ir = DirectoryReader.Open(fsIdxDirPath); return(ir.MaxDoc()); } finally { ir.Close(); fsIdxDirPath.Close(); } }
/// <summary> /// インデックスをマージして本番ディレクトリへ移動 /// </summary> /// <param name="analyzer"></param> /// <param name="rootPath"></param> /// <param name="targetDir"></param> public static void MergeAndMove( Analyzer analyzer, string rootPath, string targetDir) { System.IO.Directory.CreateDirectory(rootPath + BuildDirName); FileUtil.DeleteDirectory(new DirectoryInfo(rootPath + BuildDirName)); //2つのインデックスをマージ var fsIndexDir1 = FSDirectory.Open(FileSystems.getDefault().getPath(rootPath + BuildDirName + "1")); var fsIndexDir2 = FSDirectory.Open(FileSystems.getDefault().getPath(rootPath + BuildDirName + "2")); var tmpConf = new IndexWriterConfig(analyzer); var dirs = new FlexLucene.Store.Directory[] { fsIndexDir1, fsIndexDir2 }; var ramIndexDir = MergeIndexAsRAMDir(dirs, tmpConf); //インデックスファイルを一時構築用に保存 SaveFSIndexFromRAMIndex(ramIndexDir, rootPath + BuildDirName, analyzer); //インデックスファイルを一時構築用から検索用に移動 CopyIndexDir(rootPath + BuildDirName, rootPath + IndexDirName); }
public static void CreateIndex() { try { java.nio.file.Path idxPath = FileSystems.getDefault().getPath(IndexDir); _dir = FSDirectory.Open(idxPath); indexWriter = new IndexWriter(_dir, config); string[] files = System.IO.Directory.GetFiles(@"C:\Workspace\MyToolBox\Tool\FullTextSearchCCC\kitei", "*.htm*", System.IO.SearchOption.AllDirectories); try { foreach (string file in files) { string title, content, f; title = ""; content = ""; f = file; HTMLParse(ref title, ref content, ref f); Field fldTitle = new StringField("title", title, FieldStore.YES); Field fldPlace = new StringField("place", f, FieldStore.YES); Field fldContent = new TextField("content", content, FieldStore.YES); Document doc = new Document(); doc.Add(fldTitle); doc.Add(fldPlace); doc.Add(fldContent); indexWriter.AddDocument(doc); } } catch (System.IO.IOException ex) { System.Console.WriteLine(ex.ToString()); } indexWriter.Close(); } catch (java.io.IOException ex) { System.Console.WriteLine("Exception : " + ex.getLocalizedMessage()); } }
/// <summary> /// 1つのスレッドでFSIndexを作成する処理 /// Delete-Insert /// </summary> /// <param name="args"></param> internal void CreateFSIndex( string buildDirPath, List <FileInfo> targetFileList, IProgress <ProgressReport> progress, string threadName) { IndexWriterConfig config = new IndexWriterConfig(AppObject.AppAnalyzer); IndexWriter indexWriter = null; FSDirectory indexBuildDir = null; Dictionary <string, DocInfo> docDic = null; try { indexBuildDir = FSDirectory.Open(FileSystems.getDefault().getPath(buildDirPath)); if (_createMode == CreateModes.Update) { //既存ドキュメントを辞書化 var liru = new LuceneIndexReaderUtil(); docDic = liru.CreateDocumentDic(indexBuildDir); } int bufferSize = Properties.Settings.Default.BufferSizeLimit; config.SetRAMBufferSizeMB(bufferSize); //デフォルト16MB indexWriter = new IndexWriter(indexBuildDir, config); //開始時刻を記録 _startTime = DateTime.Now; _indexedCount = 0; _skippedCount = 0; progress.Report(new ProgressReport() { Percent = 0, ProgressCount = FinishedCount, TargetCount = _targetCount, Status = ProgressReport.ProgressStatus.Start }); //NOTE すぐにログ出力すると、フリーズ現象が発生する。 Thread.Sleep(1000); AppObject.Logger.Info(threadName + "Indexing start."); foreach (FileInfo fi in targetFileList) { Thread.Sleep(10); if (DoStop) { //中断 AppObject.Logger.Info("Stop command executed."); indexWriter.Rollback(); return; } //Officeのテンポラリファイルは無視。 if (fi.Name.StartsWith("~")) { continue; } try { if (AddDocument(fi.FullName, indexWriter, threadName, docDic)) { //インデックス作成ファイル表示 AppObject.Logger.Info(threadName + ":" + fi.FullName); Interlocked.Increment(ref _indexedCount); Interlocked.Exchange(ref _totalBytes, _totalBytes + fi.Length); } } catch (IOException ioe) { AppObject.Logger.Error(ioe.StackTrace); Interlocked.Increment(ref _skippedCount); AppObject.Logger.Info(threadName + ":" + "Skipped: " + fi.FullName); GC.Collect(); } catch (Exception e) { //インデックスが作成できなかったファイルを表示 AppObject.Logger.Warn(e.Message); Interlocked.Increment(ref _skippedCount); AppObject.Logger.Info(threadName + ":" + "Skipped: " + fi.FullName); GC.Collect(); if (e.Message.IndexOf("IndexWriter is closed") >= 0) { AppObject.Logger.Warn(e.GetBaseException().ToString()); throw new AlreadyClosedException("Index file capacity over. Please divide index directory."); } } //進捗度更新を呼び出し。 progress.Report(new ProgressReport() { Percent = GetPercentage(), ProgressCount = FinishedCount, TargetCount = _targetCount, Status = ProgressReport.ProgressStatus.Processing }); } if (docDic != null) { //削除されたファイルをインデックスから除去 foreach (var kvp in docDic) { if (kvp.Value.Exists == false) { //削除 Term t = new Term(LuceneIndexBuilder.Path, kvp.Value.Path); indexWriter.DeleteDocuments(t); } } } indexWriter.Commit(); AppObject.Logger.Info(threadName + ":完了"); } catch (Exception e) { AppObject.Logger.Error(e.Message); if (indexWriter != null) { indexWriter.Rollback(); } throw e; } finally { if (indexWriter != null && indexWriter.IsOpen()) { //クローズ時にIndexファイルがフラッシュされる indexWriter.Close(); } //完了時刻を記録 _endTime = DateTime.Now; progress.Report(new ProgressReport() { Percent = GetPercentage(), ProgressCount = FinishedCount, TargetCount = _targetCount, Status = ProgressReport.ProgressStatus.Finished }); indexBuildDir.Close(); } }