【问题标题】:How to update the existing field of solr index using java?如何使用java更新solr索引的现有字段?
【发布时间】:2017-09-19 09:50:54
【问题描述】:

我在一个包含 3000 个文档的核心中有一个 Solr 索引。

我想根据唯一键 PaperID 修改整个核心中单个字段的值。

我正在使用以下 java 代码,但不是更新现有值,而是添加新文档。

if (solrDocument.get("PaperID").equals(solrDocument2.get("PaperID"))) {

    String Mscore = (String) solrDocument.get("ID");
    String ModifyScore = (String) solrDocument.get("Author");

    //solrDocument.setField("ID", ModifyScore);
    //update the field
    System.out.println(Mscore);
    System.out.println(ModifyScore);
    System.out.println(solrDocument2.get("Mscore") + "\n");

    SolrInputDocument sid = new SolrInputDocument();

    Map<String, Object> fieldModifier = new HashMap<String, Object>(1);

    fieldModifier.put("set", ModifyScore);

    sid.setField("ID", fieldModifier);

    //solr.add(sid);
    solr.commit();

}

任何人都可以相应地指导我...最好的问候

【问题讨论】:

    标签: solr solrj


    【解决方案1】:

    您的代码没有改变任何东西,因为您已经注释掉了.add 命令。而且您必须在 ID 字段中使用实际 ID,因为 Solr 不知道要更新什么文档。 field you're changing should have the fieldModifier attached

    SolrInputDocument doc = new SolrInputDocument();
    
    Map<String, String> fieldModifier = new HashMap<String, String>();
    
    // Change ModifyScore to the new value, since you're just using the current value now..
    fieldModifier.put("set", ModifyScore);
    
    doc.addField("ID", Mscore);
    doc.addField("Author", fieldModifier);
    
    solr.add(doc);
    solr.commit();
    

    【讨论】:

    • 感谢@MatsLindh,现在它通过使用唯一密钥 id 字段工作。但是它错过了索引的第一条记录,也没有打印第一条记录。我不知道solrj和solr之间的问题在哪里。
    • 如果没有看到检索 solrDocument1 和 solrDocument2 的代码,这是不可能说的,但我的猜测是,由于您正在比较两个文档的值,因此您没有与第一个文档中有用的任何内容进行比较迭代。
    • 感谢@MatsLindh,代码很长,所以请检查您的电子邮件。我正在比较索引的 id 并相应地更新字段。
    • 抱歉,除非我们事先达成协议,否则请不要通过电子邮件发送问题或详细信息。将与 Stack Overflow 上的问题相关的任何信息保留在相关问题中,或者如果原始问题已得到回答,则作为新问题。
    • update 命令现在可以工作,但它看起来不稳定:1) 缺少索引的第一条记录。 2) 从已通过 Tika 索引的索引中删除 Text 字段。
    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 2015-08-26
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多