我有一个带有此配置的lucene .net搜索器,带有aspnet core2的net应用程序,与RamDirectory我有同样的问题。该信息仅适用于测试porpouse。
Lucene配置!!
public TextEngine(double bufferSizeMB)
{
_analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
_directory = FSDirectory.Open(@"d:\index");
_writer = new IndexWriter(_directory, _analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
_writer.SetRAMBufferSizeMB(bufferSizeMB);
_searcher = new IndexSearcher(_directory);
}
在目录上写入数据 public void Index(出版物, string子类型, 字符串省) { var document = new Document(); document.Add(new Field( "子类型&#34 ;, 亚型 Field.Store.YES, Field.Index.ANALYZED));
document.Add(new Field(
"province",
province,
Field.Store.YES,
Field.Index.ANALYZED));
document.Add(new Field(
"publicationId",
publication.Id.ToString(),
Field.Store.YES,
Field.Index.NO));
_writer.AddDocument(document);
}
在子类型字段
上搜索索引文本public string[] SearchTopBy(string index, int top)
{
var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "subType", _analyzer);
var query = new TermQuery(new Term("subType", index));
var documents = _searcher.Search(query, top);
var response = new List<string>();
foreach (var document in documents.ScoreDocs)
{
var subtype = _searcher.Doc(document.Doc).Get("subType");
var vecinity = _searcher.Doc(document.Doc).Get("vecinity");
response.Add(subtype + " " + vecinity);
}
return response.ToArray();
}
当我编写像
这样的单元测试时var engine = new TextEngine(1024);
var publication = new Publication
{
Id = 1,
IndexedDescription = "Test publication"
};
engine.Index(publication,
"plomero",
"almagro");
publication.Id = 2;
publication.IndexedDescription = "test 2";
engine.Index(publication,
"plomero",
"villa del parque");
var result = engine.SearchTopBy("plomero", 2);
Assert.True(result.Length == 2);
我期待2个结果项,但我有0个项目。 你能救我吗?
答案 0 :(得分:0)
在将文档写入索引后,应关闭编写器。 这会将所有更改提交到索引并关闭所有关联的文件。
_writer.AddDocument(document);
_writer.Close();