【问题标题】:Why doesnt lucene return results为什么lucene不返回结果
【发布时间】:2018-07-26 11:37:37
【问题描述】:

我使用 lucene.net 来索引文档。我的主要目标是搜索并在文档中返回行号和文本行。

这是索引的代码

using (TextReader contentsReader = new StreamReader(fi.FullName))
{
    doc.Add(new StringField("FullFileName", fi.FullName, Field.Store.YES));
    doc.Add(new StringField("LastModifiedDate", modDate, Field.Store.YES));
    //doc.Add(new TextField("Contents", contentsReader.ReadToEnd(), Field.Store.YES));

    int lineCount = 1;
    string line = String.Empty;
    while ((line = contentsReader.ReadLine()) != null)
    {
        doc.Add(new Int32Field("LineNo", lineCount, Field.Store.YES));
        doc.Add(new TextField("Contents", line, Field.Store.YES));
        lineCount++;
    }

    Console.ForegroundColor = ConsoleColor.Blue;
    Console.WriteLine("adding " + fi.Name);
    Console.ResetColor();
    writer.AddDocument(doc);
}

如您所见,我添加了文件名、修改日期,然后循环遍历文件中的所有行并为每一行添加 TextField。

这就是我的搜索方式:

  Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48);
            QueryParser parser = new QueryParser(Lucene.Net.Util.LuceneVersion.LUCENE_48, "Contents", analyzer);
            Lucene.Net.Search.Query query = parser.Parse(searchString);

    Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(indexDir));
    Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.DirectoryReader.Open(directory));
    TopScoreDocCollector collector = TopScoreDocCollector.Create(100, true);
    searcher.Search(query, collector);
    ScoreDoc[] hits1 = collector.GetTopDocs().ScoreDocs;
    for (int i = 0; i < hits1.Length; i++)
    {
        int docId = hits1[i].Doc;
        float score = hits1[i].Score;

        Lucene.Net.Documents.Document doc = searcher.Doc(docId);

        string result = "FileName: " + doc.Get("FullFileName") + "\n"+
        " Line No: " + doc.Get("LineNo") + "\n"+
        " Contents: " + doc.Get("Contents");
    }

然而。我的搜索结果返回 0 次点击,而如果我只是注释掉 while 循环并取消注释上面的注释行,我会得到结果。

可能是什么问题?

【问题讨论】:

  • 我刚刚运行了与您类似的示例,一切正常。您是如何创建 query 变量的?你的文件有多大?以 MB 和行数为单位?
  • 我编辑了代码。总共大约 27mb。
  • 目前我索引了大约 2000 个文本文件
  • @eminem 我强烈建议您下载 Luke for Lucene 4.8 并查看您的索引。将它们与使用 while 循环和不使用循环进行比较。如果不看下面我的答案,令牌的数量应该是相同的

标签: java c# lucene lucene.net


【解决方案1】:

这可能是因为 Lucene 4.0+ 中分析器的重用策略发生了变化。重用策略是将令牌缓存在字典中,因此对于每次迭代,索引都可能仅存储一些令牌,因为一次传递所有令牌会处理所有内容。可能需要覆盖重用策略,我直接覆盖它以使其行为与 Lucene 3.0.5 中的方式相同。让我知道这是否有帮助

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多