Java Code Examples for org.apache.solr.update.AddUpdateCommand#setVersion()
The following examples show how to use
org.apache.solr.update.AddUpdateCommand#setVersion() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocBasedVersionConstraintsProcessor.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public void processAdd(AddUpdateCommand cmd) throws IOException { if (isNotLeader(cmd)) { super.processAdd(cmd); return; } final SolrInputDocument newDoc = cmd.getSolrInputDocument(); Object[] newVersions = getUserVersionsFromDocument(newDoc); validateUserVersions(newVersions, versionFieldNames, "Doc does not have versionField: "); for (int i=0; ;i++) { logOverlyFailedRetries(i, cmd); if (!isVersionNewEnough(cmd.getIndexedId(), newVersions)) { // drop older update return; } try { cmd.setVersion(oldSolrVersion); // use optimistic concurrency to ensure that the doc has not changed in the meantime super.processAdd(cmd); return; } catch (SolrException e) { if (e.code() == 409) { continue; // if a version conflict, retry } throw e; // rethrow } } }
Example 2
Source File: DistributedUpdateProcessor.java From lucene-solr with Apache License 2.0 | 4 votes |
/** * This method is used when an update on which a particular in-place update has been lost for some reason. This method * sends a request to the shard leader to fetch the latest full document as seen on the leader. * @return AddUpdateCommand containing latest full doc at shard leader for the given id, or null if not found. */ private UpdateCommand fetchFullUpdateFromLeader(AddUpdateCommand inplaceAdd, long versionOnUpdate) throws IOException { String id = inplaceAdd.getPrintableId(); UpdateShardHandler updateShardHandler = inplaceAdd.getReq().getCore().getCoreContainer().getUpdateShardHandler(); ModifiableSolrParams params = new ModifiableSolrParams(); params.set(DISTRIB, false); params.set("getInputDocument", id); params.set("onlyIfActive", true); SolrRequest<SimpleSolrResponse> ur = new GenericSolrRequest(METHOD.GET, "/get", params); String leaderUrl = getLeaderUrl(id); if(leaderUrl == null) { throw new SolrException(ErrorCode.SERVER_ERROR, "Can't find document with id=" + id); } NamedList<Object> rsp; try { ur.setBasePath(leaderUrl); rsp = updateShardHandler.getUpdateOnlyHttpClient().request(ur); } catch (SolrServerException e) { throw new SolrException(ErrorCode.SERVER_ERROR, "Error during fetching [" + id + "] from leader (" + leaderUrl + "): ", e); } Object inputDocObj = rsp.get("inputDocument"); Long version = (Long)rsp.get("version"); SolrInputDocument leaderDoc = (SolrInputDocument) inputDocObj; if (leaderDoc == null) { // this doc was not found (deleted) on the leader. Lets delete it here as well. DeleteUpdateCommand del = new DeleteUpdateCommand(inplaceAdd.getReq()); del.setIndexedId(inplaceAdd.getIndexedId()); del.setId(inplaceAdd.getIndexedId().utf8ToString()); del.setVersion((version == null || version == 0)? -versionOnUpdate: version); return del; } AddUpdateCommand cmd = new AddUpdateCommand(req); cmd.solrDoc = leaderDoc; cmd.setVersion((long)leaderDoc.getFieldValue(CommonParams.VERSION_FIELD)); return cmd; }