使用Lucene.Net进行精确搜索

时间:2011-07-22 20:11:24

标签: lucene

我已经看过几个类似的问题,但我仍然没有答案。我想我有一个简单的问题。

在句子中

  

在本文中,只有元文件很重要,而且测试生成。   其他任何事都无关紧要

我想仅索引元文件和测试生成。这意味着我需要完全匹配。 有人可以解释一下如何实现这个目标吗?

以下是代码:

Analyzer analyzer = new StandardAnalyzer();                
Lucene.Net.Store.Directory directory = new RAMDirectory();
indexWriter iwriter = new IndexWriter(directory, analyzer, true);
iwriter.SetMaxFieldLength(10000);
Document doc = new Document();
doc.Add(new Field("textFragment", text, Field.Store.YES, Field.Index.TOKENIZED,      Field.TermVector.YES));
iwriter.AddDocument(doc);
iwriter.Close();
IndexSearcher isearcher = new IndexSearcher(directory);
QueryParser parser = new QueryParser("textFragment", analyzer);

foreach (DictionaryEntry de in OntologyLayer.OntologyLayer.HashTable)
{                
List<string> buffer = new List<string>();
double weight = 0;
List<OntologyLayer.Term> list = (List<OntologyLayer.Term>)de.Value;

foreach (OntologyLayer.Term t in list)
{
    Hits hits = null;
    string label = t.Label;
    string[] words = label.Split(' ');                        
    int numOfWords = words.Length;
    double wordWeight = 1 / (double)numOfWords;        
    double localWeight = 0;
    foreach (string a in words)
    {
        try
        {
            if (!buffer.Contains(a))
            {                                    
                Lucene.Net.Search.Query query = parser.Parse(a);
                hits = isearcher.Search(query);
                if (hits != null && hits.Length() > 0)
                {                                                                                                                                                             
                    localWeight = localWeight + t.Weight * wordWeight * hits.Length();
                }
                    buffer.Add(a);
            }
        }
        catch (Exception ex)
        {}
    }
        weight = weight + localWeight;
}

sbWeight.AppendLine(weight.ToString());

if (weight > 0)
{
    string objectURI = (string)de.Key;
    conceptList.Add(objectURI);
}

}

1 个答案:

答案 0 :(得分:0)