我试图通过索引值(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是一个记录名称索引。如果我把它取出并试图搜索“PAR 17 252”这个短语,我仍然一无所获。
感谢任何帮助。我已经阅读了关于Lucene.net的所有内容,但仍然遇到了一些麻烦。
答案 0 :(得分:0)
您想要使用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