2013-02-06 3 views
0

index() 메서드는 메서드를 호출 할 때마다 필드를 덮어 씁니다. 예를 들어, keywordHash 필드에 "1", "2", "3"을 추가하고 싶습니다. 그러나 index() 메서드를 호출하면 "3"만 저장됩니다. 어떻게 모든 데이터를 덮어 쓰지 않고 필드에 추가합니까? GetView() 메서드 내에서 Index() 메서드를 호출하는 곳입니다.Lucene.Net의 기존 필드에 데이터를 추가하는 방법

public virtual string GetView(TokenStream tokenStream, out int numberOfTokens, string filePath, string encryptedPath, string password, string fileName) 
{ 
    StringBuilder sb = new StringBuilder(); 

    Token token = tokenStream.Next(); 

    numberOfTokens = 0; 

    FileEncryption fileEnc = new FileEncryption(); 


    while (token != null) 
    { 
     numberOfTokens++; 
     sb.Append("[" + token.TermText() + " = " + GetTokenView(token) + "]" + System.Environment.NewLine); 
     fileEnc.Index(GetTokenView(token), filePath, encryptedPath, password, fileName); 
     token = tokenStream.Next(); 


    } 

    return sb.ToString(); 

} 

public void Index(string strHash, string filePath, string encryptedPath, string password, string fileName) 
{ 
    string indexFileLocation = @"C:\Index"; 
    Lucene.Net.Store.Directory dir = 
     Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, true); 

    //create an analyzer to process the text 
    Lucene.Net.Analysis.Analyzer analyzer = new 
    Lucene.Net.Analysis.Standard.StandardAnalyzer(); 

    //create the index writer with the directory and analyzer defined. 
    Lucene.Net.Index.IndexWriter indexWriter = new 
    Lucene.Net.Index.IndexWriter(dir, analyzer, 
     /*true to create a new index*/ true); 

    //create a document, add in a single field 
    Lucene.Net.Documents.Document doc = new 
    Lucene.Net.Documents.Document(); 

    doc.Add(new Field("keywordHash", strHash, Field.Store.YES, Field.Index.TOKENIZED)); 
    doc.Add(new Field("keywordPath", filePath, Field.Store.YES, Field.Index.NO)); 
    doc.Add(new Field("keywordEncPath", encryptedPath, Field.Store.YES, Field.Index.NO)); 
    doc.Add(new Field("keywordPassword", password, Field.Store.YES, Field.Index.TOKENIZED)); 
    //doc.Add(new Field("keywordEncryptedFile", encryptedFile, Field.Store.YES, Field.Index.ANALYZED)); 
    doc.Add(new Field("keywordFileName", fileName, Field.Store.YES, Field.Index.NO)); 


    //write the document to the index 
    indexWriter.AddDocument(doc); 

    //optimize and close the writer 
    indexWriter.Optimize(); 
    indexWriter.Close(); 


} 


public void LuceneSearch() 
{ 
    HashAlg hashAlg = new HashAlg(); 
    string keywordLower = tbSearchEnc.Text.ToLower(); 
    string keywordHash; 

    if(rbMD5Search.Checked == true) 
    { 
     keywordHash = hashAlg.GenerateHashMD5(keywordLower); 
    } 
    else 
    { 
     keywordHash = hashAlg.GenerateHashSHA1(keywordLower); 
    } 

    string indexFileLocation = @"C:\Index"; 
    Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, false); 
    Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(indexFileLocation); 
    //create an index searcher that will perform the search 
    //Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir); 

    //build a query object 
    Lucene.Net.Index.Term searchTerm = new Lucene.Net.Index.Term("keywordHash", keywordHash); 
    Lucene.Net.Search.Query query = new Lucene.Net.Search.TermQuery(searchTerm); 

    //execute the query 
    Lucene.Net.Search.Hits hits = searcher.Search(query); 

    //iterate over the results. 
    for (int i = 0; i < hits.Length(); i++) 
    { 
     Document doc = hits.Doc(i); 
     string hashValue = doc.Get("keywordHash"); 
     string path = doc.Get("keywordPath"); 
     string encPath = doc.Get("keywordEncPath"); 
     string fileName = doc.Get("keywordFileName"); 

     listBoxSearch.Items.Add(encPath); 
     Console.WriteLine(hashValue + " " + path + " " + encPath + " " + fileName); 

    } 
} 

답변

0

당신은 FSDirectory.GetDirectory(..., create: true)new IndexWriter(..., create: true) 모두를 호출하고 있습니다. true을 전달하면 기존 색인이 지워 지므로 이전에 색인 된 문서가 제거됩니다.

+0

실제로이 코드는 사용되지 않으며 "다음"을 여기에 대치하는 방법을 찾습니다. http://stackoverflow.com/q/22907008/185593 – serhio

관련 문제