【问题标题】:Why doesnt' Lucene remove docs?为什么 Lucene 不删除文档?
【发布时间】:2009-07-02 11:03:14
【问题描述】:

我正在使用带有 MultiSearcher 的 Lucene.NET 2.3.1。

出于测试目的,我正在索引一个包含大约 10000 行的数据库。我有两个索引,我随机选择在其中插入每一行。这可以正常工作,但由于 Lucene 没有更新功能,我必须测试该行是否存在(我有一个 Id 字段)然后删除它。

我有一个 List 和一个 List,每个都是用这段代码创建的:

IndexModifier mod = new IndexModifier(path, new StandardAnalyzer(), create);
m_Modifiers.Add(mod);
m_Readers.Add(IndexReader.Open(path));
m_Searchers.Add(new IndexSearcher(path));

现在删除代码:

Hits results = m_Searcher.Search(new TermQuery(t));

for (int i = 0; i < results.Length(); i++)
{
    DocId = results .Id(i);
    Index = m_Searcher.SubSearcher(DocId);
    DocId = m_Searcher.SubDoc(DocId);

    m_Modifiers[Index].DeleteDocument(DocId);
}

搜索是正确的,当行存在时我会得到结果。 SubSearcher 始终返回 0 或 1,如果 Index 为 0,则 SubDoc 返回传递的相同 ID,如果为 1,则返回传递的数字减去我索引数据库的次数的 5000 倍。似乎它没有删除任何内容。

每次我索引数据库时,我都会优化并关闭索引,Luke 说它没有待删除的内容。

可能是什么问题?

【问题讨论】:

    标签: c# lucene


    【解决方案1】:

    我不确定此活动的最终目标是什么,如果以下解决方案不符合您的要求,请见谅。

    首先,如果要删除文档,可以使用已经创建的 IndexReader。 IndexModifier 不是必需的。

    其次,您不需要在该子搜索器中找到子搜索器 ID 和文档 ID。您也可以使用顶级 MultiReader。我将编写等效的java代码如下。

    IndexReader[] readers = new IndexReader[size];
    // Initialize readers
    MultiReader multiReader = new MultiReader(readers);
    
    IndexSearcher searcher = new IndexSearcher(multiReader);
    Hits results = searcher.search(new TermQuery(t));
    for (int i = 0; i < results.length(); i++) {
        int docID = results.id(i);
        multiReader.deleteDocument(docID);
    }
    multiReader.commit(); // Check if this throws an exception.
    multiReader.close();
    searcher.close();
    

    【讨论】:

    • 谢谢,终于我自己解决了。我的问题实际上是一个应该是 UN_TOKENIZED 并且是 TOKENIZED 的字段。如果我没记错的话,为了使用 multiReader 删除,我必须关闭我的修饰符,并且由于大多数删除将作为文档更新的一部分完成,我不想关闭它,因此直接使用修饰符将其删除.我是对的,还是有更好的解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多