java - Lucene updateDocument不删除文档

标签 java lucene yammer

这似乎是一个常见问题,但我以前没有遇到过这个问题,并且通常的修复不起作用。这可能是一些愚蠢的东西,但我找不到它。

我想为 yammer 站点建立索引,因为 yammer api 的速度不足以满足我的目的,问题是当我尝试使用 updateDocument 功能更新索引时,旧索引不会被删除。但我有一个未分析的存储的唯一 key 。

相关代码如下:

Document newdoc = new Document();
newdoc.add(new Field(YammerMessageFields.URL, resultUrl, Field.Store.YES, Field.Index.NOT_ANALYZED));
newdoc.add(new Field(YammerMessageFields.THREAD_ID, threadID.toString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
newdoc.add(new Field(YammerMessageFields.AUTHOR, senderName, Field.Store.YES, Field.Index.ANALYZED));
newdoc.add(new Field(YammerMessageFields.CONTENTS, resultText, Field.Store.YES, Field.Index.ANALYZED));
Term key = new Term(YammerMessageFields.THREAD_ID, newdoc.getFieldable(YammerMessageFields.THREAD_ID).toString());
logger.debug("updating document with key: " + key);
try {
    IndexWriter writer = getIndexWriter();
    writer.updateDocument(key, newdoc);
    writer.close();
} catch (IOException e) {
}

我在日志中看到的是:

2012-05-11 12:02:29,816 DEBUG [http-8088-2] LuceneIndex - https://www.yammer.com/api/v1/messages/?newer_than=0
2012-05-11 12:02:38,594 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173285202>
2012-05-11 12:02:45,167 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173033239>
2012-05-11 12:02:51,686 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173014568>
2012-05-11 12:02:51,871 DEBUG [http-8088-2] LuceneIndex - new items:3

2012-05-11 12:03:27,393 DEBUG [http-8088-2] YammerResource - return all documents
2012-05-11 12:03:27,405 DEBUG [http-8088-2] YammerResource - nr docs:3
2012-05-11 12:03:27,405 DEBUG [http-8088-2] YammerResource - nr dels:0

...
next update
...

2012-05-11 12:03:35,802 DEBUG [http-8088-2] LuceneIndex - https://www.yammer.com/api/v1/messages/?newer_than=0
2012-05-11 12:03:43,933 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173322760>
2012-05-11 12:03:50,467 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173285202>
2012-05-11 12:03:56,982 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173056406>
2012-05-11 12:04:03,533 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173033239>
2012-05-11 12:04:10,097 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173030769>
2012-05-11 12:04:16,629 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173014568>
2012-05-11 12:04:23,169 DEBUG [http-8088-2] LuceneIndex - updating document with key: threadid:stored,indexed<threadid:173003570>
2012-05-11 12:04:23,341 DEBUG [http-8088-2] LuceneIndex - new items:7

2012-05-11 12:05:09,694 DEBUG [http-8088-1] YammerResource - return all documents
2012-05-11 12:05:09,696 DEBUG [http-8088-1] YammerResource - nr docs:10
2012-05-11 12:05:09,696 DEBUG [http-8088-1] YammerResource - nr dels:0

因此 key 会再次出现(以及 4 个新 key ),但是完成此操作后,我的商店中将有 10 个文档,而不是 7 个(以及 3 个已删除的文档)。

编辑:这是我找到这些元素的方法,但我实际上展示了它们并与卢克一起检查了它们。

IndexReader r = IndexReader.open(searchIndex.getIndex());
                List<Document> docList = new ArrayList<Document>();
                List<Document> delList = new ArrayList<Document>();

                int num = r.numDocs();
                num += r.numDeletedDocs();
                for ( int i = 0; i < num && i < max; i++)
                {
                    if ( ! r.isDeleted( i))
                        docList.add(r.document(i));
                    else
                        delList.add(r.document(i));

                }
                r.close();
                logger.debug("nr docs:" + docList.size());
                logger.debug("nr dels:" + delList.size());

最佳答案

如果不运行一些测试代码,我不确定,但这对我来说看起来是错误的:

Term key = new Term(YammerMessageFields.THREAD_ID, 
   newdoc.getFieldable(YammerMessageFields.THREAD_ID).toString());

您确定不应该这样吗:

Term key = new Term(YammerMessageFields.THREAD_ID, 
   newdoc.getFieldable(YammerMessageFields.THREAD_ID).stringValue());

然后,您可以继续使用该 key 来尝试更新任何匹配的现有文档。如果 key 错误,那么文档更新可能会默默失败。我怀疑该 Term 上的 toString() 实际上只会为您提供一个对象引用,这意味着更新将永远不会起作用。

调用 toString() 进行日志记录或调试以外的任何操作(即其中包含逻辑的任何操作)通常是一个错误。

关于java - Lucene updateDocument不删除文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10552409/

相关文章:

java - 异步写入似乎在 Cassandra 中被破坏

java - 如何在java中使用具有动态参数的mysql存储过程?

java - 具有 objectify 功能的安全嵌入式实体

tomcat - 如何在 Windows 的 solr 上安装 Rich Document 补丁?

Yammer javascript SDK 注销问题

java - 添加带有显示标签的自定义行

java - 我可以使用 Lucene Search 来索引和搜索泰米尔语文档吗?

java - 从句子生成 N 元语法

yammer - 通过 yammer API 创建私有(private)群组

android - 适用于 iOS 和 Android 的 Yammer URL 移动深层链接