我开始使用Lucene.Net(卡在2.3.1版本上)。我用这个添加示例文档:
Dim indexWriter = New IndexWriter(indexDir, New Standard.StandardAnalyzer(), True)
Dim doc = Document()
doc.Add(New Field("Title", "foo", Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.NO))
doc.Add(New Field("Date", DateTime.UtcNow.ToString, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.NO))
indexWriter.AddDocument(doc)
indexWriter.Close()
我搜索与“foo”匹配的文件:
Dim searcher = New IndexSearcher(indexDir)
Dim parser = New QueryParser("Title", New StandardAnalyzer())
Dim Query = parser.Parse("foo")
Dim hits = searcher.Search(Query)
Console.WriteLine("Number of hits = " + hits.Length.ToString)
无论我运行多少次,我都只得到一个结果。有什么想法吗?
答案 0 :(得分:2)
Mikos对于重建索引是正确的,你的问题就在这里:
Dim indexWriter = New IndexWriter(indexDir, New Standard.StandardAnalyzer(), True)
因为您传递的是true,所以每次都要重新创建索引 - 需要检查是否存在并创建IF NEEDED。
我刚才遇到过这个问题,这就是我如何解决它的问题:
If _writer Is Nothing Then
Dim create As Boolean = Not System.IO.Directory.Exists(path) OrElse System.IO.Directory.GetFiles(path).Length = 0
_directory = FSDirectory.GetDirectory(path, _lockFactory)
_writer = New IndexWriter(_directory, _analyzer, create)
End If
其中path是索引的路径。不确定这是否是最好的方法,但它对我有用(也使用lucene.net 2.3)。
另外,如果可以的话,你应该每次都避免创作作者 - 如果你遇到的情况是lucene不喜欢> 1作者在特定索引上打开
答案 1 :(得分:1)
使用Luke检查索引中的文档数量。很可能是你的文档添加例程。