How to change Solr document id by SolrJ?

2019-09-03 21:17发布

问题:

I use the following code to change specific field value.

SolrInputDocument solrdoc=new SolrInputDocument();
solrdoc.addField("id","book001");
Map<String,Object> fieldModifier=new HashMap<>(1);
fieldModifier.put("set","book_new_name");
solrdoc.addField("name",fieldModifier); 

But I cannot use the similar code to change Solr unique document id, exception is thrown.

solrdoc.addField("id",fieldModifier); 

Is there any solution to change the Solr document id?

回答1:

Changing the document id requires removing and re-adding the document, so delete the document (by id), then re-add the document with the new id. You can do this without committing between deleting and adding the document, so it will be seamless for any clients querying the collection.



标签: solr