diff --git a/source/net/yacy/cora/federate/solr/connector/AbstractSolrConnector.java b/source/net/yacy/cora/federate/solr/connector/AbstractSolrConnector.java index 7b04f663b..d16d6e9e2 100644 --- a/source/net/yacy/cora/federate/solr/connector/AbstractSolrConnector.java +++ b/source/net/yacy/cora/federate/solr/connector/AbstractSolrConnector.java @@ -21,12 +21,15 @@ package net.yacy.cora.federate.solr.connector; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; @@ -49,6 +52,7 @@ import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.SolrInputField; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.DisMaxParams; import org.apache.solr.common.params.FacetParams; @@ -468,5 +472,46 @@ public abstract class AbstractSolrConnector implements SolrConnector { throw new IOException(e.getMessage(), e); } } + + /** + * Update a solr document. + * This will write only a partial update for all fields given in the SolrInputDocument + * and leaves all other fields untouched. + * @param solrdoc + * @throws IOException + * @throws SolrException + */ + @Override + public void update(final SolrInputDocument solrdoc) throws IOException, SolrException { + this.add(partialUpdatePatch(solrdoc)); + } + + /** + * Update a collection of solr input documents. + * This will write only a partial update for all fields given in the SolrInputDocuments + * and leaves all other fields untouched. + * @param solrdocs + * @throws IOException + * @throws SolrException + */ + @Override + public void update(final Collection solrdoc) throws IOException, SolrException { + Collection docs = new ArrayList<>(solrdoc.size()); + for (SolrInputDocument doc: solrdoc) docs.add(partialUpdatePatch(doc)); + this.add(docs); + } + + private SolrInputDocument partialUpdatePatch(final SolrInputDocument docIn) { + SolrInputDocument docOut = new SolrInputDocument(); + docOut.setField(CollectionSchema.id.name(), docIn.getFieldValue(CollectionSchema.id.name())); + for (Entry entry: docIn.entrySet()) { + if (entry.getKey().equals(CollectionSchema.id.name())) continue; + Map partialUpdate = new HashMap<>(1); + partialUpdate.put("set", entry.getValue()); + docOut.setField(entry.getKey(), partialUpdate); + } + return docOut; + } + } diff --git a/source/net/yacy/cora/federate/solr/connector/ConcurrentUpdateSolrConnector.java b/source/net/yacy/cora/federate/solr/connector/ConcurrentUpdateSolrConnector.java index 3bfc4f2e8..dc329f9e4 100644 --- a/source/net/yacy/cora/federate/solr/connector/ConcurrentUpdateSolrConnector.java +++ b/source/net/yacy/cora/federate/solr/connector/ConcurrentUpdateSolrConnector.java @@ -370,4 +370,15 @@ public class ConcurrentUpdateSolrConnector implements SolrConnector { return this.connector.concurrentIDsByQuery(querystring, sort, offset, maxcount, maxtime, buffersize, concurrency); } + @Override + public void update(final SolrInputDocument solrdoc) throws IOException, SolrException { + commitDocBuffer(); + this.connector.update(solrdoc); + } + + @Override + public void update(final Collection solrdoc) throws IOException, SolrException { + commitDocBuffer(); + this.connector.update(solrdoc); + } } diff --git a/source/net/yacy/cora/federate/solr/connector/SolrConnector.java b/source/net/yacy/cora/federate/solr/connector/SolrConnector.java index b5ddc001c..f4c1dfd0f 100644 --- a/source/net/yacy/cora/federate/solr/connector/SolrConnector.java +++ b/source/net/yacy/cora/federate/solr/connector/SolrConnector.java @@ -123,7 +123,7 @@ public interface SolrConnector extends Iterable /* Iterable of document * @throws IOException */ public LoadTimeURL getLoadTimeURL(final String id) throws IOException; - + /** * add a solr input document * @param solrdoc @@ -132,6 +132,16 @@ public interface SolrConnector extends Iterable /* Iterable of document */ public void add(final SolrInputDocument solrdoc) throws IOException, SolrException; + /** + * Update a solr document. + * This will write only a partial update for all fields given in the SolrInputDocument + * and leaves all other fields untouched. + * @param solrdoc + * @throws IOException + * @throws SolrException + */ + public void update(final SolrInputDocument solrdoc) throws IOException, SolrException; + /** * add a collection of solr input documents * @param solrdocs @@ -139,6 +149,16 @@ public interface SolrConnector extends Iterable /* Iterable of document * @throws SolrException */ public void add(final Collection solrdoc) throws IOException, SolrException; + + /** + * Update a collection of solr input documents. + * This will write only a partial update for all fields given in the SolrInputDocuments + * and leaves all other fields untouched. + * @param solrdocs + * @throws IOException + * @throws SolrException + */ + public void update(final Collection solrdoc) throws IOException, SolrException; /** * get a document from solr by given key for the id-field