Java Code Examples for org.apache.solr.client.solrj.request.UpdateRequest#setParam()

The following examples show how to use org.apache.solr.client.solrj.request.UpdateRequest#setParam() . 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: DistribJoinFromCollectionTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static String indexDoc(String collection, int id, String joinField, String matchField, String getField) throws Exception {
  UpdateRequest up = new UpdateRequest();
  up.setCommitWithin(50);
  up.setParam("collection", collection);
  SolrInputDocument doc = new SolrInputDocument();
  String docId = "" + id;
  doc.addField("id", docId);
  doc.addField("join_s", joinField);
  if (matchField != null)
    doc.addField("match_s", matchField);
  if (getField != null)
    doc.addField("get_s", getField);
  up.add(doc);
  cluster.getSolrClient().request(up);
  return docId;
}
 
Example 2
Source File: TestTlogReplica.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private UpdateRequest simulatedUpdateRequest(Long prevVersion, Object... fields) throws SolrServerException, IOException {
  SolrInputDocument doc = sdoc(fields);

  // get baseUrl of the leader
  String baseUrl = getBaseUrl();

  UpdateRequest ur = new UpdateRequest();
  ur.add(doc);
  ur.setParam("update.distrib", "FROMLEADER");
  if (prevVersion != null) {
    ur.setParam("distrib.inplace.prevversion", String.valueOf(prevVersion));
    ur.setParam("distrib.inplace.update", "true");
  }
  ur.setParam("distrib.from", baseUrl);
  return ur;
}
 
Example 3
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
UpdateRequest simulatedUpdateRequest(Long prevVersion, Object... fields) throws SolrServerException, IOException {
  SolrInputDocument doc = sdoc(fields);
  
  // get baseUrl of the leader
  String baseUrl = getBaseUrl(doc.get("id").toString());

  UpdateRequest ur = new UpdateRequest();
  ur.add(doc);
  ur.setParam("update.distrib", "FROMLEADER");
  if (prevVersion != null) {
    ur.setParam("distrib.inplace.prevversion", String.valueOf(prevVersion));
    ur.setParam("distrib.inplace.update", "true");
  }
  ur.setParam("distrib.from", baseUrl);
  return ur;
}
 
Example 4
Source File: PutSolrRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
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);
        }
    }

    // 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();
}
 
Example 5
Source File: CdcrVersionReplicationTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void vadd(String id, long version, String... params) throws Exception {
  UpdateRequest req = new UpdateRequest();
  req.add(sdoc("id", id, vfield, version));
  for (int i = 0; i < params.length; i += 2) {
    req.setParam(params[i], params[i + 1]);
  }
  solrServer.request(req);
}
 
Example 6
Source File: TestDistribDocBasedVersion.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void vadd(String id, long version, String... params) throws Exception {
  UpdateRequest req = new UpdateRequest();
  req.add(sdoc("id", id, vfield, version));
  for (int i=0; i<params.length; i+=2) {
    req.setParam( params[i], params[i+1]);
  }
  solrClient.request(req);
}
 
Example 7
Source File: TestDistribDocBasedVersion.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void vdelete(String id, long version, String... params) throws Exception {
  UpdateRequest req = new UpdateRequest();
  req.deleteById(id);
  req.setParam("del_version", Long.toString(version));
  for (int i=0; i<params.length; i+=2) {
    req.setParam( params[i], params[i+1]);
  }
  solrClient.request(req);
  // req.process(cloudClient);
}
 
Example 8
Source File: TestTlogReplica.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private UpdateRequest simulatedDBQ(String query, long version) throws SolrServerException, IOException {
  String baseUrl = getBaseUrl();

  UpdateRequest ur = new UpdateRequest();
  ur.deleteByQuery(query);
  ur.setParam("_version_", ""+version);
  ur.setParam("update.distrib", "FROMLEADER");
  ur.setParam("distrib.from", baseUrl);
  return ur;
}
 
Example 9
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
UpdateRequest simulatedDeleteRequest(String query, long version) throws SolrServerException, IOException {
  String baseUrl = getBaseUrl((HttpSolrClient)LEADER);

  UpdateRequest ur = new UpdateRequest();
  ur.deleteByQuery(query);
  ur.setParam("_version_", ""+version);
  ur.setParam("update.distrib", "FROMLEADER");
  ur.setParam("distrib.from", baseUrl + DEFAULT_COLLECTION + "/");
  return ur;
}
 
Example 10
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
UpdateRequest simulatedDeleteRequest(int id, long version) throws SolrServerException, IOException {
  String baseUrl = getBaseUrl(""+id);

  UpdateRequest ur = new UpdateRequest();
  if (random().nextBoolean() || onlyLeaderIndexes) {
    ur.deleteById(""+id);
  } else {
    ur.deleteByQuery("id:"+id);
  }
  ur.setParam("_version_", ""+version);
  ur.setParam("update.distrib", "FROMLEADER");
  ur.setParam("distrib.from", baseUrl);
  return ur;
}
 
Example 11
Source File: SimScenario.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(SimScenario scenario) throws Exception {
  String collection = params.required().get("collection");
  long numDocs = params.required().getLong("numDocs");
  long start = params.getLong("start", 0L);

  UpdateRequest ureq = new UpdateRequest();
  ureq.setParam("collection", collection);
  ureq.setDocIterator(new FakeDocIterator(start, numDocs));
  scenario.cluster.simGetSolrClient().request(ureq);
}
 
Example 12
Source File: StoppableIndexingThread.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void indexDocs(List<SolrInputDocument> docs) throws IOException,
    SolrServerException {
  
  if (controlClient != null) {
    UpdateRequest req = new UpdateRequest();
    req.add(docs);
    req.setParam("CONTROL", "TRUE");
    req.process(controlClient);
  }
  
  UpdateRequest ureq = new UpdateRequest();
  ureq.add(docs);
  ureq.process(cloudClient);
}
 
Example 13
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateRequestWithParameters() throws Exception {
  SolrClient client = createNewSolrClient();
  
  client.deleteByQuery("*:*");
  client.commit();
  
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "id1");
  
  UpdateRequest req = new UpdateRequest();
  req.setParam("overwrite", "false");
  req.add(doc);
  client.request(req);
  client.request(req);
  client.commit();
  
  SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  QueryResponse rsp = client.query(query);
  
  SolrDocumentList out = rsp.getResults();
  assertEquals(2, out.getNumFound());
  if (!(client instanceof EmbeddedSolrServer)) {
    /* Do not close in case of using EmbeddedSolrServer,
     * as that would close the CoreContainer */
    client.close();
  }
}
 
Example 14
Source File: SendToSolrCloudProcessor.java    From jesterj with Apache License 2.0 5 votes vote down vote up
@Override
protected void batchOperation(ConcurrentBiMap<Document, SolrInputDocument> oldBatch) throws SolrServerException, IOException {
  List<String> deletes = oldBatch.keySet().stream()
      .filter(doc -> doc.getOperation() == Document.Operation.DELETE)
      .map(Document::getId)
      .collect(Collectors.toList());
  if (deletes.size() > 0) {
    getSolrClient().deleteById(deletes);
  }
  List<SolrInputDocument> adds = oldBatch.keySet().stream()
      .filter(doc -> doc.getOperation() != Document.Operation.DELETE)
      .map(oldBatch::get)
      .collect(Collectors.toList());
  if (adds.size() > 0) {
    Map<String, String> params = getParams();
    if (params == null) {
      getSolrClient().add(adds);
    } else {
      UpdateRequest req = new UpdateRequest();
      req.add(adds);
      // always true right now, but pattern for addtional global params...
      for (String s : params.keySet()) {
        req.setParam(s, params.get(s));
      }
      getSolrClient().request(req);
    }
  }
  for (Document document : oldBatch.keySet()) {
    putIdInThreadContext(document);
    if (document.getOperation() == Document.Operation.DELETE) {
      log().info(Status.INDEXED.getMarker(), "{} deleted from solr successfully", document.getId());
    } else {
      log().info(Status.INDEXED.getMarker(), "{} sent to solr successfully", document.getId());
    }
  }
}
 
Example 15
Source File: CloudSolrSinkTask.java    From kafka-connect-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void process(String topic, UpdateRequest updateRequest) throws IOException, SolrServerException {
  final String collection = collection(topic);
  updateRequest.setParam("collection", collection);
  UpdateResponse response = updateRequest.process(client);
  log.trace("process() - qtime = {} elapsedTime = {}", response.getQTime(), response.getElapsedTime());
}
 
Example 16
Source File: CdcrReplicator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private UpdateRequest processUpdate(Object o, UpdateRequest req) {

    // should currently be a List<Oper,Ver,Doc/Id>
    @SuppressWarnings({"rawtypes"})
    List entry = (List) o;

    int operationAndFlags = (Integer) entry.get(0);
    int oper = operationAndFlags & UpdateLog.OPERATION_MASK;
    long version = (Long) entry.get(1);

    // record the operation in the benchmark timer
    state.getBenchmarkTimer().incrementCounter(oper);

    switch (oper) {
      case UpdateLog.ADD: {
        // the version is already attached to the document
        SolrInputDocument sdoc = (SolrInputDocument) entry.get(entry.size() - 1);
        req.add(sdoc);
        return req;
      }
      case UpdateLog.DELETE: {
        byte[] idBytes = (byte[]) entry.get(2);
        req.deleteById(new String(idBytes, Charset.forName("UTF-8")));
        req.setParam(VERSION_FIELD, Long.toString(version));
        return req;
      }

      case UpdateLog.DELETE_BY_QUERY: {
        String query = (String) entry.get(2);
        req.deleteByQuery(query);
        req.setParam(VERSION_FIELD, Long.toString(version));
        return req;
      }

      case UpdateLog.COMMIT: {
        return null;
      }

      default:
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Operation! " + oper);
    }
  }
 
Example 17
Source File: SystemLogListener.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void onEvent(TriggerEvent event, TriggerEventProcessorStage stage, String actionName, ActionContext context,
             Throwable error, String message) throws Exception {
  try {
    ClusterState clusterState = cloudManager.getClusterStateProvider().getClusterState();
    DocCollection coll = clusterState.getCollectionOrNull(collection);
    if (coll == null) {
      log.debug("Collection {} missing, skip sending event {}", collection, event);
      return;
    }
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField(CommonParams.TYPE, DOC_TYPE);
    doc.addField(SOURCE_FIELD, SOURCE);
    doc.addField("id", IdUtils.timeRandomId());
    doc.addField("event.id_s", event.getId());
    doc.addField(EVENT_TYPE_FIELD, event.getEventType().toString());
    doc.addField(EVENT_SOURCE_FIELD, event.getSource());
    doc.addField("event.time_l", event.getEventTime());
    doc.addField("timestamp", new Date());
    addMap("event.property.", doc, event.getProperties());
    doc.addField(STAGE_FIELD, stage.toString());
    if (actionName != null) {
      doc.addField(ACTION_FIELD, actionName);
    }
    if (message != null) {
      doc.addField(MESSAGE_FIELD, message);
    }
    addError(doc, error);
    // add JSON versions of event and context
    String eventJson = Utils.toJSONString(event);
    doc.addField("event_str", eventJson);
    if (context != null) {
      // capture specifics of operations after compute_plan action
      addOperations(doc, (List<SolrRequest>)context.getProperties().get("operations"));
      // capture specifics of responses after execute_plan action
      addResponses(doc, (List<NamedList<Object>>)context.getProperties().get("responses"));
      addActions(BEFORE_ACTIONS_FIELD, doc, (List<String>)context.getProperties().get(TriggerEventProcessorStage.BEFORE_ACTION.toString()));
      addActions(AFTER_ACTIONS_FIELD, doc, (List<String>)context.getProperties().get(TriggerEventProcessorStage.AFTER_ACTION.toString()));
      String contextJson = Utils.toJSONString(context);
      doc.addField("context_str", contextJson);
    }
    UpdateRequest req = new UpdateRequest();
    req.add(doc);
    req.setParam(CollectionAdminParams.COLLECTION, collection);
    cloudManager.request(req);
  } catch (Exception e) {
    if ((e instanceof SolrException) && e.getMessage().contains("Collection not found")) {
      // relatively benign but log this - collection still existed when we started
      log.info("Collection {} missing, skip sending event {}", collection, event);
    } else {
      log.warn("Exception sending event. Collection: {}, event: {}, exception: {}", collection, event, e);
    }
  }
}
 
Example 18
Source File: TestSimExtremeIndexing.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void addDocs(String collection, long start, long count) throws Exception {
  UpdateRequest ureq = new UpdateRequest();
  ureq.setParam("collection", collection);
  ureq.setDocIterator(new FakeDocIterator(start, count));
  solrClient.request(ureq);
}
 
Example 19
Source File: SolrBoltAction.java    From storm-solr with Apache License 2.0 4 votes vote down vote up
public UpdateRequest createUpdateRequest(String collection) {
  UpdateRequest req = new UpdateRequest();
  req.setParam("collection", collection);
  return req;
}
 
Example 20
Source File: BasicHttpSolrClientTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdate() throws Exception {
  DebugServlet.clear();
  try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
    UpdateRequest req = new UpdateRequest();
    req.add(new SolrInputDocument());
    req.setParam("a", "\u1234");
    expectThrows(BaseHttpSolrClient.RemoteSolrException.class, () -> client.request(req));

    //default method
    assertEquals("post", DebugServlet.lastMethod);
    //agent
    assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
    //default wt
    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
    //default version
    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
    assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
    //content type
    assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
    //parameter encoding
    assertEquals(1, DebugServlet.parameters.get("a").length);
    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);

    //XML response and writer
    client.setParser(new XMLResponseParser());
    client.setRequestWriter(new RequestWriter());
    expectThrows(BaseHttpSolrClient.RemoteSolrException.class, () -> client.request(req));

    assertEquals("post", DebugServlet.lastMethod);
    assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
    assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
    assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
    assertEquals("application/xml; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
    assertEquals(1, DebugServlet.parameters.get("a").length);
    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);

    //javabin request
    client.setParser(new BinaryResponseParser());
    client.setRequestWriter(new BinaryRequestWriter());
    DebugServlet.clear();
    expectThrows(BaseHttpSolrClient.RemoteSolrException.class, () -> client.request(req));

    assertEquals("post", DebugServlet.lastMethod);
    assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
    assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
    assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
    assertEquals(1, DebugServlet.parameters.get("a").length);
    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
  }

}