我的 Lucene 查询只找到一次命中

发布于 2024-09-01 12:44:21 字数 789 浏览 3 评论 0原文

我正在开始使用 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)

无论我运行这个命令多少次,我只得到一个结果。有什么想法吗?

I'm getting started with Lucene.Net (stuck on version 2.3.1). I add sample documents with this:

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

I search for documents matching "foo" with this:

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)

No matter how many times I run this, I only ever get one result. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2024-09-08 12:44:21

Mikos 关于重新创建索引的说法是正确的,您的问题就在这里:

Dim indexWriter = New IndexWriter(indexDir, New Standard.StandardAnalyzer(), True)

因为您传递的是 true,所以您每次都在重新创建索引 - 需要检查是否存在并在需要时创建。

我不久前遇到过这个问题,以下是我解决这个问题的方法:

   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

其中路径是索引的路径。不确定这是否是最好的方法,但它对我有用(也使用 lucene.net 2.3)。

另外,如果可以的话,你应该避免每次都创建 writer - 如果你遇到 > > 的情况,lucene 不会喜欢。 1 个写入器在特定索引上打开

Mikos is right about recreating the index, your problem is here:

Dim indexWriter = New IndexWriter(indexDir, New Standard.StandardAnalyzer(), True)

Because you are passing true, you're recreating the index each time - need to check for existence and create IF NEEDED.

I ran into this a while ago, here's how I got around it:

   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

Where path is the path to your index. Not sure if this is the best approach but it is working for me (using lucene.net 2.3 also).

Also, you should avoid creating the writer every time if you can - lucene isn't going to like if you get in a situation where you have > 1 writer open on a particular index

作死小能手 2024-09-08 12:44:21

使用 Luke 检查索引中有多少文档。很可能是您的文档添加例程中的某些内容。

Check how many documents are in your index using Luke. Could very well be something in your document add routine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文