问题
Solr 4.x has this nice new feature that lets you specify how, when doing an update on an existing document, the multiValued fields will be updated. Specifically, you can say if the update document will replace the old values of a multivalued field with the new ones, or if it should append the new values to the existing ones.
I've tried this using the request handler, as described here:
http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22field.22
I've used curl to send xml where some fields used the update=add
option:
<field name="skills" update="add">Python</field>
This works as expected.
However I can't get how to do this the Java API (SolrJ).
If I do something like this:
SolrInputDocument doc1 = new SolrInputDocument();
doc1.setField("ID_VENTE", "idv1");
doc1.setField("FACTURES_PRODUIT", "fp_initial");
solrServer.add(doc1);
solrServer.commit();
SolrInputDocument doc2 = new SolrInputDocument();
doc2.setField("ID_VENTE", "idv1");
doc2.setField("FACTURES_PRODUIT", "fp_2");
solrServer.add(doc2);
solrServer.commit();
The value for the field "FACTURES_PRODUIT" becomes "fp_2" (the initial value is lost). I've also tried:
doc2.addField("FACTURES_PRODUIT", "fp_2");
but the result is the same. I've also looked into the SolrInputField class but haven't found anything similar to this.
So, my question is, how can I use the Solr 4 Java API to updated values into a multiValued field by appening (not replacing) the new values?
回答1:
Ok, I solved it after debugging a bit the SolrJ code. You have to do this:
SolrInputDocument doc2 = new SolrInputDocument();
Map<String,String> fpValue2 = new HashMap<String, String>();
fpValue2.put("add","fp2");
doc2.setField("FACTURES_PRODUIT", fpValue2);
来源:https://stackoverflow.com/questions/16234045/solr-how-to-use-the-new-field-update-modes-atomic-updates-with-solrj