List of usage examples for org.apache.solr.client.solrj.request UpdateRequest getParams
@Override
public ModifiableSolrParams getParams()
From source file:org.apache.nifi.processors.solr.PutSolrRecord.java
License:Apache License
private void index(boolean isSolrCloud, String collection, Long commitWithin, String contentStreamPath, MultiMapSolrParams requestParams, List<SolrInputDocument> inputDocumentList) throws IOException, SolrServerException, SolrException { UpdateRequest request = new UpdateRequest(contentStreamPath); request.setParams(new ModifiableSolrParams()); // add the extra params, don't use 'set' in case of repeating params Iterator<String> paramNames = requestParams.getParameterNamesIterator(); while (paramNames.hasNext()) { String paramName = paramNames.next(); for (String paramValue : requestParams.getParams(paramName)) { request.getParams().add(paramName, paramValue); }/*from w w w . j a va2 s . c om*/ } // specify the collection for SolrCloud if (isSolrCloud) { request.setParam(COLLECTION_PARAM_NAME, collection); } if (commitWithin != null && commitWithin > 0) { request.setParam(COMMIT_WITHIN_PARAM_NAME, commitWithin.toString()); } // if a username and password were provided then pass them for basic auth if (isBasicAuthEnabled()) { request.setBasicAuthCredentials(getUsername(), getPassword()); } request.add(inputDocumentList); UpdateResponse response = request.process(getSolrClient()); getLogger().debug("Got {} response from Solr", new Object[] { response.getStatus() }); inputDocumentList.clear(); }
From source file:org.codelibs.elasticsearch.solr.solr.JavaBinUpdateRequestCodec.java
License:Apache License
/** * Converts an UpdateRequest to a NamedList which can be serialized to the * given OutputStream in the javabin format * * @param updateRequest// ww w . j a v a2 s . c o m * the UpdateRequest to be written out * @param os * the OutputStream to which the request is to be written * * @throws IOException * in case of an exception during marshalling or writing to the * stream */ public void marshal(final UpdateRequest updateRequest, final OutputStream os) throws IOException { final NamedList nl = new NamedList(); final NamedList params = solrParamsToNamedList(updateRequest.getParams()); if (updateRequest.getCommitWithin() != -1) { params.add("commitWithin", updateRequest.getCommitWithin()); } Iterator<SolrInputDocument> docIter = null; if (updateRequest.getDocuments() != null) { docIter = updateRequest.getDocuments().iterator(); } if (updateRequest.getDocIterator() != null) { docIter = updateRequest.getDocIterator(); } nl.add("params", params);// 0: params nl.add("delById", updateRequest.getDeleteById()); nl.add("delByQ", updateRequest.getDeleteQuery()); nl.add("docs", docIter); final JavaBinCodec codec = new JavaBinCodec(); codec.marshal(nl, os); }
From source file:org.codelibs.elasticsearch.solr.solr.JavaBinUpdateRequestCodec.java
License:Apache License
/** * Reads a NamedList from the given InputStream, converts it into a * SolrInputDocument and passes it to the given StreamingUpdateHandler * * @param is//from w ww . j a v a 2 s . co m * the InputStream from which to read * @param handler * an instance of StreamingUpdateHandler to which * SolrInputDocuments are streamed one by one * * @return the UpdateRequest * * @throws IOException * in case of an exception while reading from the input stream * or unmarshalling */ public UpdateRequest unmarshal(final InputStream is, final StreamingUpdateHandler handler) throws IOException { final UpdateRequest updateRequest = new UpdateRequest(); List<Object> doclist; // mocksolrplugin: changed to Object List<String> delById; List<String> delByQ; final NamedList[] namedList = new NamedList[1]; final JavaBinCodec codec = new JavaBinCodec() { // NOTE: this only works because this is an anonymous inner class // which will only ever be used on a single stream -- if this class // is ever refactored, this will not work. private boolean seenOuterMostDocIterator = false; @Override public NamedList readNamedList(final DataInputInputStream dis) throws IOException { final int sz = readSize(dis); final NamedList nl = new NamedList(); if (namedList[0] == null) { namedList[0] = nl; } for (int i = 0; i < sz; i++) { final String name = (String) readVal(dis); final Object val = readVal(dis); nl.add(name, val); } return nl; } @Override public List readIterator(final DataInputInputStream fis) throws IOException { // default behavior for reading any regular Iterator in the // stream if (seenOuterMostDocIterator) { return super.readIterator(fis); } // special treatment for first outermost Iterator // (the list of documents) seenOuterMostDocIterator = true; return readOuterMostDocIterator(fis); } private List readOuterMostDocIterator(final DataInputInputStream fis) throws IOException { final NamedList params = (NamedList) namedList[0].getVal(0); updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params))); if (handler == null) { return super.readIterator(fis); } while (true) { final Object o = readVal(fis); if (o == END_OBJ) { break; } SolrInputDocument sdoc = null; if (o instanceof List) { sdoc = JavaBinUpdateRequestCodec.this.listToSolrInputDocument((List<NamedList>) o); } else if (o instanceof NamedList) { final UpdateRequest req = new UpdateRequest(); req.setParams(new ModifiableSolrParams(SolrParams.toSolrParams((NamedList) o))); handler.update(null, req); } else { sdoc = (SolrInputDocument) o; } handler.update(sdoc, updateRequest); } return Collections.EMPTY_LIST; } }; codec.unmarshal(is); // NOTE: if the update request contains only delete commands the params // must be loaded now if (updateRequest.getParams() == null) { final NamedList params = (NamedList) namedList[0].get("params"); if (params != null) { updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params))); } } delById = (List<String>) namedList[0].get("delById"); delByQ = (List<String>) namedList[0].get("delByQ"); doclist = (List) namedList[0].get("docs"); if (doclist != null && !doclist.isEmpty()) { final List<SolrInputDocument> solrInputDocs = new ArrayList<SolrInputDocument>(); for (final Object o : doclist) { if (o instanceof List) { solrInputDocs.add(listToSolrInputDocument((List<NamedList>) o)); } else { solrInputDocs.add((SolrInputDocument) o); } } updateRequest.add(solrInputDocs); } if (delById != null) { for (final String s : delById) { updateRequest.deleteById(s); } } if (delByQ != null) { for (final String s : delByQ) { updateRequest.deleteByQuery(s); } } return updateRequest; }
From source file:sk.datalan.solr.impl.ConcurrentUpdateSolrServer.java
License:Apache License
@Override public NamedList<Object> request(final SolrRequest request) throws SolrServerException, IOException { if (!(request instanceof UpdateRequest)) { return server.request(request); }// w w w .ja va 2s. c o m UpdateRequest req = (UpdateRequest) request; // this happens for commit... if (streamDeletes) { if ((req.getDocuments() == null || req.getDocuments().isEmpty()) && (req.getDeleteById() == null || req.getDeleteById().isEmpty()) && (req.getDeleteByIdMap() == null || req.getDeleteByIdMap().isEmpty())) { if (req.getDeleteQuery() == null) { blockUntilFinished(); return server.request(request); } } } else { if ((req.getDocuments() == null || req.getDocuments().isEmpty())) { blockUntilFinished(); return server.request(request); } } SolrParams params = req.getParams(); if (params != null) { // check if it is waiting for the searcher if (params.getBool(UpdateParams.WAIT_SEARCHER, false)) { log.info("blocking for commit/optimize"); blockUntilFinished(); // empty the queue return server.request(request); } } try { CountDownLatch tmpLock = lock; if (tmpLock != null) { tmpLock.await(); } boolean success = queue.offer(req); for (;;) { synchronized (runners) { if (runners.isEmpty() || (queue.remainingCapacity() < queue.size() // queue // is // half // full // and // we // can // add // more // runners && runners.size() < threadCount)) { // We need more runners, so start a new one. Runner r = new Runner(); runners.add(r); scheduler.execute(r); } else { // break out of the retry loop if we added the element to the queue // successfully, *and* // while we are still holding the runners lock to prevent race // conditions. if (success) break; } } // Retry to add to the queue w/o the runners lock held (else we risk // temporary deadlock) // This retry could also fail because // 1) existing runners were not able to take off any new elements in the // queue // 2) the queue was filled back up since our last try // If we succeed, the queue may have been completely emptied, and all // runners stopped. // In all cases, we should loop back to the top to see if we need to // start more runners. // if (!success) { success = queue.offer(req, 100, TimeUnit.MILLISECONDS); } } } catch (InterruptedException e) { log.error("interrupted", e); throw new IOException(e.getLocalizedMessage()); } // RETURN A DUMMY result NamedList<Object> dummy = new NamedList<>(); dummy.add("NOTE", "the request is processed in a background stream"); return dummy; }