搜索Lucene指数

时间:2013-02-07 02:04:46

标签: c# search lucene.net

我正在使用Lucene.Net版本2.3.2.1。当我调用index()方法时,它将索引C:\ Index中的文档。但是当我调用SearchLucene()方法时,什么都没有出来。

这是我的index()和SearchLucene()方法:

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

    //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*/

    //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.TOKENIZED));
    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.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
    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
    //Query query = new TermQuery(new Term("keywordHash", keywordHash));
    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);
    int result = hits.Length();
    //iterate over the results.
    for (int i = 0; i < result; i++)
    {
        Document doc = hits.Doc(i);
        string hashValue = doc.Get("keywordHash");
        Console.WriteLine(hashValue);
        string path = doc.Get("keywordPath");
        Console.WriteLine(path);
        string encPath = doc.Get("keywordEncPath");
        Console.WriteLine(encPath);
        string fileName = doc.Get("keywordFileName");
        Console.WriteLine(fileName);

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

    }
}

1 个答案:

答案 0 :(得分:1)

我认为您需要致电IndexWriter.Commit(),除非您有特殊要求,否则您可能不应该使用Optimize()方法。