提问者:小点点

精确的ID搜索与Lucene.net3.0.3


我正在尝试通过索引值(PAR-17-252)查找文档。我使用

    Dim d As Lucene.Net.Store.Directory = FSDirectory.Open(New DirectoryInfo(p))
        Dim a As Analyzer = New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)
        Dim indexWriter As IndexWriter = New IndexWriter(d, a, True, indexWriter.MaxFieldLength.UNLIMITED)            

        doc.Add(New Field("GrantID", dr("GrantID").ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED))

我寻找着

        term = term.Replace("-", " ")
        term = term.Replace("/", " ")

            Dim phases As String() = Nothing
            phases = Split(term, ",")
            For Each phase As String In phases
                q.Add(parser.Parse(phase), Occur.SHOULD)
            Next

现在我知道“-”导致了一个问题,但我不知道如何处理它。如果我不把它从搜索词中取出,我什么也得不到,如果我把它留在搜索词中,我什么也得不到。PAR-17-252是一个记录名称索引。如果我把它取出并尝试搜索短语“PAR17 252”,我仍然一无所获。

任何帮助都很感激。我已经读了这里关于Lucene.net和仍然有一些麻烦的一切。


共1个答案

匿名用户

您想使用KeywordAnalyzer搜索该字段。这里有一些C#为您的用例演示KeywordAnalyzer,抱歉它不vb.net-但您应该了解要点。

var field_GrantID = "GrantID";
var field_value = "PAR-17-252";
var luceneVer = Lucene.Net.Util.Version.LUCENE_30;

using (var writer = new IndexWriter(new RAMDirectory(), new StandardAnalyzer(luceneVer), IndexWriter.MaxFieldLength.UNLIMITED))
{
    var doc = new Document();
    // NOT_ANALYZED means index the field as presented.
    doc.Add(new Field(field_GrantID, field_value, Field.Store.YES, Field.Index.NOT_ANALYZED));
    writer.AddDocument(doc);
    writer.Commit();

    using (var searcher = new IndexSearcher(writer.GetReader()))
    {            
        var parser = new QueryParser(luceneVer, field_GrantID, new KeywordAnalyzer());
        var queryText = String.Format("{0}:{1}", field_GrantID, field_value);
        var query = parser.Parse(queryText);
        var topDocs = searcher.Search(query, null, 100);
        Console.WriteLine("Total Hits for query {0} : {1}", query, topDocs.TotalHits);
    }
}

您可能需要搜索多个字段,请查看MultiFieldQueryParser