为什么添加文档和字段更新无法在solrJ中一起工作

时间:2015-04-07 18:45:10

标签: solr solrj

所有

现在,我想向SOLR发送大量文档以便使用SOLRJ进行索引,然后更新某些字段。当文档数量很少或我手动运行这两个作业时,更新有效。但是,当我在一段包含大量文档的代码中运行这两个作业时,更新部分不起作用。

HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr/");
// Suppose here are lots of documents using a for/while loop to add them( or multithread)
{
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", ids[i]);
doc.addField("content", content[i]);
solr.add(doc);
}

solr.commit();

// Similar for/while loop here
{
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", id[i]);
    Map<String, List<String>> oper = new HashMap<String, List<String>>();
    oper.put("set", updatecontent[i]);
// I know I did not update the same content field, but added another. But they should be similar and it works in the case of only a few documents
    doc.addField("updatecontent", oper);
    solr.add(doc);
}
solr.commit();

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

  

虽然更新程序可能是此处的问题,但由于Atomic Updates有很多敏感要求(特别是在哪些字段必须或不得存储),我的答案仅限于如果避免原子更新会有所帮助,这可能不是最好的选择或选择。

你还没有说过当它无法正常工作时是否会出现错误信息,但是第一件事就是你说你试图更新Solr,但Solr没有&#39 ; t始终支持更新。您可以创建完整 Solr文档,从Solr中删除旧文档,然后重新添加文档。你的第二个循环看起来像这样:

// Similar for/while loop here
{
   SolrInputDocument doc = new SolrInputDocument();
   doc.addField("id", id[i]);

   //this is the original content - if you still need it.
   doc.addField("content", content[i]);

   doc.addField("updatecontent", updatecontent[i]);

   solr.deleteById(id[i]); // this will not fail, even if the record isn't in Solr.
   solr.add(doc);
}
solr.commit();

如果无法从原始数据源或通过查询Solr恢复Solr记录中应保持不变的所有数据,则可能无法正确更新Solr记录。 (即使您只通过Atomic Updates发送更新的字段,这个要求仍然适用,这是同一解决方案的服务器端实现.Solr将构建一个完整的,更新的文档,删除文档,然后重新添加它)。