2016-09-01 3 views
1

lucene.net을 사용하여 PDF 파일의 색인을 생성하고 있습니다. 색인을 새로 고친 후 동일한 documnet을 여러 번 표시합니다 (= 색인을 새로 고치는 횟수).lucene.net 색인의 중복 문서

최신 lucene.net 색인 생성 (Lucene.net 3.0.3)을 사용하고 있습니다.

다음은 색인 생성을위한 코드입니다.

public void refreshIndexes() 
    { 
     // Create Index Writer 
     string strIndexDir = @"Z:\Munavvar\LuceneTest\index"; 
     IndexWriter writer = new IndexWriter(Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED); 

     writer.DeleteAll(); 
     // Find all files in root folder create index on them 
     List<string> lstFiles = searchFiles(@"Z:\Munavvar\LuceneTest\PDFs"); 
     foreach (string strFile in lstFiles) 
     { 
      Document doc = new Document(); 
      string FileName = System.IO.Path.GetFileNameWithoutExtension(strFile); 
      string Text = ExtractTextFromPdf(strFile); 
      string Path = strFile; 
      string ModifiedDate = Convert.ToString(File.GetLastWriteTime(strFile)); 
      string DocumentType = string.Empty; 
      string Vault = string.Empty; 

      string headerText = Text.Substring(0, Text.Length < 150 ? Text.Length : 150); 
      foreach (var docs in ltDocumentTypes) 
      { 
       if (headerText.ToUpper().Contains(docs.searchText.ToUpper())) 
       { 
        DocumentType = docs.DocumentType; 
        Vault = docs.VaultName; ; 
       } 
      } 

      if (string.IsNullOrEmpty(DocumentType)) 
      { 
       DocumentType = "Default"; 
       Vault = "Default"; 
      } 

      doc.Add(new Field("filename", FileName, Field.Store.YES, Field.Index.ANALYZED)); 
      doc.Add(new Field("text", Text, Field.Store.YES, Field.Index.ANALYZED)); 
      doc.Add(new Field("path", Path, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
      doc.Add(new Field("modifieddate", ModifiedDate, Field.Store.YES, Field.Index.ANALYZED)); 
      doc.Add(new Field("documenttype", DocumentType, Field.Store.YES, Field.Index.ANALYZED)); 
      doc.Add(new Field("vault", Vault, Field.Store.YES, Field.Index.ANALYZED)); 

      writer.AddDocument(doc); 
     } 
     writer.Optimize(); 
     writer.Dispose(); 
    } 

는 여기에 내가 refreshIndexes 메소드를 호출 창에서 하나 개의 버튼이 내 인덱스 찾는 것과 코드

public List<IndexDocument> searchFromIndexes(string searchText) 
    { 
     try 
     { 
      #region search in indexes and fill list 
      // Create list 
      List<IndexDocument> searchResult = new List<IndexDocument>(); 

      if (!string.IsNullOrEmpty(searchText)) 
      { 
       string strIndexDir = @"Z:\Munavvar\LuceneTest\index"; 
       var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); 
       IndexSearcher searcher = new IndexSearcher(Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir))); 

       // parse the query, "text" is the default field to search 
       Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", analyzer); 


       Query query = parser.Parse(searchText); 

       // search 
       TopDocs hits = searcher.Search(query, searcher.MaxDoc); 

       // showing first TotalHits results 
       for (int i = 0; i < hits.TotalHits; i++) 
       { 
        // get the document from index 
        Document doc = searcher.Doc(hits.ScoreDocs[i].Doc); 

        // create a new row with the result data 
        searchResult.Add(new IndexDocument() 
         { 
          FileName = doc.Get("filename"), 
          Text = doc.Get("text"), 
          Path = doc.Get("path"), 
          ModifiedDate = doc.Get("modifieddate"), 
          Vault = doc.Get("vault"), 
          DocumentType = doc.Get("documenttype"), 
         }); 

       } 
       searcher.Dispose(); 
      } 
      return searchResult; 
      #endregion 

     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

UPDATE

입니다.

내가 닫을 때 이전 인덱스를 삭제하고 다시 응용 프로그램을 실행하고

+0

의 제 3 인수에 따라 새 문서 를 만들 수 없습니다. 그런데'ltDocumentTypes'는 무엇입니까 –

+1

@RichaGarg - IndexWriter ctor의 세 번째 인수는 기존 인덱스 (있는 경우)를 덮어 쓰거나 첨부해야하는지 여부를 지정합니다. 그것은 '사실'이며, 이전 색인 *은 삭제되어야합니다. – femtoRgon

+1

이 색인을 검색하는 방법에 대한 정보를 제공 할 수 있습니까? 내가 궁금해하는 한 가지는 오래된 독자를 어딘가에서 열어 두는 것이 가능하면 가능한가하는 것입니다. 당신이'MultiReader' 또는 무엇인가 그들을 수집해야 할 것처럼 보이지만 ... – femtoRgon

답변

0

이 솔루션을 함께 와서 그 버튼을 클릭합니다.

문제점 : 글로벌 클래스 개체에서 refreshIndexes 메서드를 호출하려고합니다.

VaultIndexes vIndexes = new VaultIndexes(); 
private void btnRefreshIndex_Click(object sender, RoutedEventArgs e) 
{ 
    vIndexes.refreshIndexes(); 
} 

해결책 : 때마다 새 개체 개체를 만듭니다.

private void btnRefreshIndex_Click(object sender, RoutedEventArgs e) 
{ 
    VaultIndexes vIndexes = new VaultIndexes(); 
    vIndexes.refreshIndexes(); 
} 

나는 그것이 글로벌 클래스 객체와 중복 문서를 작성하는 이유를 잘 모릅니다. 코멘트에서

으로 @RichaGarg 상태는, 당신이 이전 데이터를 삭제해야하기 때문이다 IndexWriter

IndexWriter writer = new IndexWriter(Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED);