1
votes

I am trying to find a document by the indexed value (PAR-17-252). I indexed the field using

    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))

And I search with

        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

Now I know that the "-" causes a problem but I don't know how to handle it. If I don't take it out of the search term I get back nothing if I leave it in I get back nothing. The PAR-17-252 is a record name index. If I take it out and try to search for the phrase "PAR 17 252" I still get nothing.

Any help is appreciated. I've read just about everything here about Lucene.net and still having some trouble.

1

1 Answers

0
votes

You want to search that field with KeywordAnalyzer. Here's some C# that demonstrates KeywordAnalyzer for your use case, sorry it's not vb.net - but you should get the gist.

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);
    }
}

You'll likely want to search more than one field, check out MultiFieldQueryParser