0

I have a Solr index in a core having 3000 documents.

I want to modify the value of a single field in the entire core based on unique key PaperID.

I'm using the following java code but instead of updating the existing value it adds new documents.

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();

}

can anyone guide me accordingly...Best Regards

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
Shah Khalid
  • 31
  • 1
  • 10

1 Answers1

0

Your code isn't changing anything, since you've commented out the .add command. And you have to use the actual ID in the ID field, since Solr won't know what document to update otherwise. The 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
  • 49,529
  • 4
  • 53
  • 84
  • Thanks @MatsLindh, Now its working by using Unique Key id field. But it misses the first record of the index, also does not print the first record. i don't know where is the problem between solrj and solr. – Shah Khalid Sep 20 '17 at 04:11
  • That's impossible to say without seeing the code that retrieves solrDocument1 and solrDocument2, but my guess would be that since you're comparing the values of two documents, you're not comparing against anything useful in your first iteration. – MatsLindh Sep 20 '17 at 07:16
  • Thanks @MatsLindh the code is long that is why check your email. I am comparing the ids of both the index and updates the field accordingly. – Shah Khalid Sep 20 '17 at 08:04
  • Sorry, but please do not send questions or details on email unless we have a prior agreement in place. Keep any information related to the question on Stack Overflow, either in the relevant question or as a new question if the original question has been answered. – MatsLindh Sep 20 '17 at 09:39
  • the update command now works, but its looking unstable: 1) missing the first record of the index. 2) delete the Text field from the indexed which has been already index through Tika.. – Shah Khalid Sep 20 '17 at 10:16
  • All fields in a document being update must be set as stored, or if available for the field type, have docValues set. Otherwise the document will lose the value for the field, since it's not available. This is documented under [Field Storage in the reference guide](https://lucene.apache.org/solr/guide/6_6/updating-parts-of-documents.html#UpdatingPartsofDocuments-FieldStorage). – MatsLindh Sep 20 '17 at 12:18