org.apache.solr.update.AddUpdateCommand Java Examples

The following examples show how to use org.apache.solr.update.AddUpdateCommand. 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: TolerantUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  BytesRef id = null;
  
  try {
    // force AddUpdateCommand to validate+cache the id before proceeding
    id = cmd.getIndexedId();
    
    super.processAdd(cmd);

  } catch (Throwable t) { 
    firstErrTracker.caught(t);
    knownErrors.add(new ToleratedUpdateError
                    (CmdType.ADD,
                     getPrintableId(id),
                     t.getMessage()));
    
    if (knownErrors.size() > maxErrors) {
      firstErrTracker.throwFirst();
    }
  }
}
 
Example #2
Source File: DimensionalRoutedAlias.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public CandidateCollection findCandidateGivenValue(AddUpdateCommand cmd) {
  contextualizeDimensions(formattedRouteValues(cmd.solrDoc));
  List<CandidateCollection> subPartCandidates = new ArrayList<>();
  for (RoutedAlias dimension : dimensions) {
    subPartCandidates.add(dimension.findCandidateGivenValue(cmd));
  }

  StringBuilder col2Create = new StringBuilder(getAliasName());
  StringBuilder destCol = new StringBuilder(getAliasName());
  CreationType max = CreationType.NONE;
  for (CandidateCollection subCol : subPartCandidates) {
    col2Create.append(subCol.getCreationCollection());
    destCol.append(subCol.getDestinationCollection());
    if (subCol.getCreationType().ordinal() > max.ordinal()) {
      max = subCol.getCreationType();
    }
  }
  return new CandidateCollection(max,destCol.toString(),col2Create.toString());
}
 
Example #3
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void putAclTransactionState(UpdateRequestProcessor processor, SolrQueryRequest request, AclChangeSet changeSet) throws IOException
{
    SolrDocument aclState = getState(core, request, "TRACKER!STATE!ACLTX");
    String version = version(aclState, FIELD_S_ACLTXCOMMITTIME, FIELD_S_ACLTXID, changeSet.getCommitTimeMs(), changeSet.getId());

    if (version != null)
    {

        SolrInputDocument input = new SolrInputDocument();
        input.addField(FIELD_SOLR4_ID, "TRACKER!STATE!ACLTX");
        input.addField(FIELD_VERSION, version);
        input.addField(FIELD_S_ACLTXID, changeSet.getId());
        input.addField(FIELD_S_INACLTXID, changeSet.getId());
        input.addField(FIELD_S_ACLTXCOMMITTIME, changeSet.getCommitTimeMs());
        input.addField(FIELD_DOC_TYPE, DOC_TYPE_STATE);

        AddUpdateCommand cmd = new AddUpdateCommand(request);
        cmd.overwrite = true;
        cmd.solrDoc = input;
        processor.processAdd(cmd);
    }
}
 
Example #4
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void knnMultiClass_maxOutputClasses2_shouldAssignMax2Classes() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMultiClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word1 word1 word1",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.KNN);
  params.setMaxPredictedClasses(2);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());
  updateProcessorToTest.processAdd(update);

  @SuppressWarnings({"unchecked"})
  ArrayList<Object> assignedClasses = (ArrayList)unseenDocument1.getFieldValues(TRAINING_CLASS);
  assertThat(assignedClasses.size(),is(2));
  assertThat(assignedClasses.get(0),is("class2"));
  assertThat(assignedClasses.get(1),is("class1"));
}
 
Example #5
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testAtomicUpdateFieldValue() throws Exception {
  String str = "[{'id':'1', 'val_s':{'add':'foo'}}]".replace('\'', '"');
  SolrQueryRequest req = req();
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);
  JsonLoader loader = new JsonLoader();
  loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);

  assertEquals( 1, p.addCommands.size() );

  AddUpdateCommand add = p.addCommands.get(0);
  assertEquals(add.commitWithin, -1);
  assertEquals(add.overwrite, true);
  assertEquals("SolrInputDocument(fields: [id=1, val_s={add=foo}])", add.solrDoc.toString());

  req.close();
}
 
Example #6
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void bayesMultiClass_maxOutputClasses2_shouldAssignMax2Classes() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMultiClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word1 word1 word1",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.BAYES);
  params.setMaxPredictedClasses(2);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());
  updateProcessorToTest.processAdd(update);

  @SuppressWarnings({"unchecked"})
  ArrayList<Object> assignedClasses = (ArrayList)unseenDocument1.getFieldValues(TRAINING_CLASS);
  assertThat(assignedClasses.size(),is(2));
  assertThat(assignedClasses.get(0),is("class2"));
  assertThat(assignedClasses.get(1),is("class1"));
}
 
Example #7
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void knnMultiClass_boostFieldsMaxOutputClasses2_shouldAssignMax2Classes() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMultiClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word4 word4 word4",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.KNN);
  params.setInputFieldNames(new String[]{TITLE+"^1.5",CONTENT+"^0.5",AUTHOR+"^2.5"});
  params.setMaxPredictedClasses(2);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());

  updateProcessorToTest.processAdd(update);

  @SuppressWarnings({"unchecked"})
  ArrayList<Object> assignedClasses = (ArrayList)unseenDocument1.getFieldValues(TRAINING_CLASS);
  assertThat(assignedClasses.size(),is(2));
  assertThat(assignedClasses.get(0),is("class4"));
  assertThat(assignedClasses.get(1),is("class6"));
}
 
Example #8
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void knnMonoClass_sampleParams_shouldAssignCorrectClass() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMonoClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word4 word4 word4",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params = initParams(ClassificationUpdateProcessorFactory.Algorithm.KNN);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());
  updateProcessorToTest.processAdd(update);

  assertThat(unseenDocument1.getFieldValue(TRAINING_CLASS),is("class2"));
}
 
Example #9
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void knnMonoClass_contextQueryFiltered_shouldAssignCorrectClass() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMonoClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word4 word4 word4",
      CONTENT, "word2 word2 ",
      AUTHOR, "a");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.KNN);
  Query class3DocsChunk=new TermQuery(new Term(TITLE,"word6"));
  params.setTrainingFilterQuery(class3DocsChunk);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());
  updateProcessorToTest.processAdd(update);

  assertThat(unseenDocument1.getFieldValue(TRAINING_CLASS),is("class3"));
}
 
Example #10
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void capIndex(long dbid) throws IOException
{
    UpdateRequestProcessor processor = null;
    try (SolrQueryRequest request = newSolrQueryRequest())
    {
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, newSolrQueryResponse());

        SolrInputDocument input = new SolrInputDocument();
        input.addField(FIELD_SOLR4_ID, INDEX_CAP_ID);
        input.addField(FIELD_VERSION, 0);
        input.addField(FIELD_DBID, -dbid); //Making this negative to ensure it is never confused with node DBID
        input.addField(FIELD_DOC_TYPE, DOC_TYPE_STATE);

        AddUpdateCommand cmd = new AddUpdateCommand(request);
        cmd.overwrite = true;
        cmd.solrDoc = input;
        processor.processAdd(cmd);
    }
    finally
    {
        if (processor != null) processor.finish();
    }
}
 
Example #11
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimalValuesInAdd() throws Exception {
  String str = "{'add':[{'id':'1','d1':256.78,'d2':-5123456789.0,'d3':0.0,'d3':1.0,'d4':1.7E-10}]}".replace('\'', '"');
  SolrQueryRequest req = req();
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);
  JsonLoader loader = new JsonLoader();
  loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);

  assertEquals(1, p.addCommands.size());

  AddUpdateCommand add = p.addCommands.get(0);
  SolrInputDocument d = add.solrDoc;
  SolrInputField f = d.getField("d1");
  assertEquals(256.78, f.getValue());
  f = d.getField("d2");
  assertEquals(-5123456789.0, f.getValue());
  f = d.getField("d3");
  assertEquals(2, ((List)f.getValue()).size());
  assertTrue(((List)f.getValue()).contains(0.0));
  assertTrue(((List) f.getValue()).contains(1.0));
  f = d.getField("d4");
  assertEquals(1.7E-10, f.getValue());

  req.close();
}
 
Example #12
Source File: XmlUpdateRequestHandlerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testExternalEntities() throws Exception
{
  String file = getFile("mailing_lists.pdf").toURI().toASCIIString();
  String xml = 
    "<?xml version=\"1.0\"?>" +
    // check that external entities are not resolved!
    "<!DOCTYPE foo [<!ENTITY bar SYSTEM \""+file+"\">]>" +
    "<add>" +
    "  &bar;" +
    "  <doc>" +
    "    <field name=\"id\">12345</field>" +
    "    <field name=\"name\">kitten</field>" +
    "  </doc>" +
    "</add>";
  SolrQueryRequest req = req();
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);
  XMLLoader loader = new XMLLoader().init(null);
  loader.load(req, rsp, new ContentStreamBase.StringStream(xml), p);

  AddUpdateCommand add = p.addCommands.get(0);
  assertEquals("12345", add.solrDoc.getField("id").getFirstValue());
  req.close();
}
 
Example #13
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void putTransactionState(UpdateRequestProcessor processor, SolrQueryRequest request, Transaction tx) throws IOException
{
    SolrDocument txState = getState(core, request, "TRACKER!STATE!TX");
    String version = version(txState, FIELD_S_TXCOMMITTIME, FIELD_S_TXID, tx.getCommitTimeMs(), tx.getId());

    if (version != null)
    {
        SolrInputDocument input = new SolrInputDocument();
        input.addField(FIELD_SOLR4_ID, "TRACKER!STATE!TX");
        input.addField(FIELD_VERSION, version);
        input.addField(FIELD_S_TXID, tx.getId());
        input.addField(FIELD_S_INTXID, tx.getId());
        input.addField(FIELD_S_TXCOMMITTIME, tx.getCommitTimeMs());
        input.addField(FIELD_DOC_TYPE, DOC_TYPE_STATE);

        AddUpdateCommand cmd = new AddUpdateCommand(request);
        cmd.overwrite = true;
        cmd.solrDoc = input;
        processor.processAdd(cmd);
    }
}
 
Example #14
Source File: ExtractingRequestHandlerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommitWithin() throws Exception {
  ExtractingRequestHandler handler = (ExtractingRequestHandler) h.getCore().getRequestHandler("/update/extract");
  assertTrue("handler is null and it shouldn't be", handler != null);
  
  SolrQueryRequest req = req("literal.id", "one",
                             ExtractingParams.RESOURCE_NAME, "extraction/version_control.txt",
                             "commitWithin", "200"
                             );
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);

  ExtractingDocumentLoader loader = (ExtractingDocumentLoader) handler.newLoader(req, p);
  loader.load(req, rsp, new ContentStreamBase.FileStream(getFile("extraction/version_control.txt")),p);

  AddUpdateCommand add = p.addCommands.get(0);
  assertEquals(200, add.commitWithin);

  req.close();
}
 
Example #15
Source File: ExtractingDocumentLoader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public ExtractingDocumentLoader(SolrQueryRequest req, UpdateRequestProcessor processor,
                         TikaConfig config, ParseContextConfig parseContextConfig,
                                SolrContentHandlerFactory factory) {
  this.params = req.getParams();
  this.core = req.getCore();
  this.config = config;
  this.parseContextConfig = parseContextConfig;
  this.processor = processor;

  templateAdd = new AddUpdateCommand(req);
  templateAdd.overwrite = params.getBool(UpdateParams.OVERWRITE, true);
  templateAdd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);

  //this is lightweight
  autoDetectParser = new AutoDetectParser(config);
  this.factory = factory;
  
  ignoreTikaException = params.getBool(ExtractingParams.IGNORE_TIKA_EXCEPTION, false);
}
 
Example #16
Source File: DistributedUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testVersionAdd() throws IOException {
  SolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams());
  int threads = 5;
  Function<DistributedUpdateProcessor,Boolean> versionAddFunc = (DistributedUpdateProcessor process) -> {
    try {
      AddUpdateCommand cmd = new AddUpdateCommand(req);
      cmd.solrDoc = new SolrInputDocument();
      cmd.solrDoc.setField("notid", "10");
      return process.versionAdd(cmd);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  };
  int succeeded = runCommands(threads, 1000, req, versionAddFunc);
  // only one should succeed
  assertThat(succeeded, is(1));

  succeeded = runCommands(threads, -1, req, versionAddFunc);
  // all should succeed
  assertThat(succeeded, is(threads));
}
 
Example #17
Source File: ClassificationUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * @param cmd the update command in input containing the Document to classify
 * @throws IOException If there is a low-level I/O error
 */
@Override
public void processAdd(AddUpdateCommand cmd)
    throws IOException {
  SolrInputDocument doc = cmd.getSolrInputDocument();
  Document luceneDocument = cmd.getLuceneDocument();
  String assignedClass;
  Object documentClass = doc.getFieldValue(trainingClassField);
  if (documentClass == null) {
    List<ClassificationResult<BytesRef>> assignedClassifications = classifier.getClasses(luceneDocument, maxOutputClasses);
    if (assignedClassifications != null) {
      for (ClassificationResult<BytesRef> singleClassification : assignedClassifications) {
        assignedClass = singleClassification.getAssignedClass().utf8ToString();
        doc.addField(predictedClassField, assignedClass);
      }
    }
  }
  super.processAdd(cmd);
}
 
Example #18
Source File: TemplateUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected void process(AddUpdateCommand cmd, SolrQueryRequest req, SolrQueryResponse rsp) {
  String[] vals = getParams("field");
  SolrInputDocument doc = cmd.getSolrInputDocument();
  if (vals != null && vals.length > 0) {
    for (String val : vals) {
      if (val == null || val.isEmpty()) continue;
      int idx = val.indexOf(':');
      if (idx == -1)
        throw new RuntimeException("'field' must be of the format <field-name>:<the-template-string>");

      String fName = val.substring(0, idx);
      String template = val.substring(idx + 1);
      doc.addField(fName, replaceTokens(template, templateCache, s -> {
        Object v = doc.getFieldValue(s);
        return v == null ? "" : v;
      }, BRACES_PLACEHOLDER_PATTERN));
    }
  }

}
 
Example #19
Source File: UUIDUpdateProcessorFallbackTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testProcessorPrefixReqParam() throws Exception {
  List<UpdateRequestProcessorFactory> processors = UpdateRequestProcessorChain.getReqProcessors("uuid", h.getCore());
  UpdateRequestProcessorFactory processorFactory = processors.get(0);
  assertTrue(processorFactory instanceof UUIDUpdateProcessorFactory);

  SolrQueryResponse rsp = new SolrQueryResponse();
  SolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams());
  AddUpdateCommand cmd = new AddUpdateCommand(req);
  cmd.solrDoc = new SolrInputDocument();
  cmd.solrDoc.addField("random_s", "random_val");

  processorFactory.getInstance(req, rsp, null).processAdd(cmd);
  assertNotNull(cmd.solrDoc);
  assertNotNull(cmd.solrDoc.get("id"));
  assertNotNull(cmd.solrDoc.get("id").getValue());
}
 
Example #20
Source File: RegexpBoostProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void processBoost(AddUpdateCommand command) {
  SolrInputDocument document = command.getSolrInputDocument();
  if (document.containsKey(inputFieldname)) {
    String value = (String) document.getFieldValue(inputFieldname);
    double boost = 1.0f;
    for (BoostEntry boostEntry : boostEntries) {
      if (boostEntry.getPattern().matcher(value).matches()) {
        if (log.isDebugEnabled()) {
          log.debug("Pattern match {} for {}", boostEntry.getPattern().pattern(), value);
        }
        boost = (boostEntry.getBoost() * 1000) * (boost * 1000) / 1000000;
      }
    }
    document.setField(boostFieldname, boost);

    if (log.isDebugEnabled()) {
      log.debug("Value {}, applied to field {}", boost, boostFieldname);
    }
  }
}
 
Example #21
Source File: RoutedAliasUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  routedAlias.validateRouteValue(cmd);

  // to avoid potential for race conditions, this next method should not get called again unless
  // we have created a collection synchronously
  routedAlias.updateParsedCollectionAliases(this.zkController.zkStateReader, false);

  String targetCollection = routedAlias.createCollectionsIfRequired(cmd);

  if (thisCollection.equals(targetCollection)) {
    // pass on through; we've reached the right collection
    super.processAdd(cmd);
  } else {
    // send to the right collection
    SolrCmdDistributor.Node targetLeaderNode = routeDocToSlice(targetCollection, cmd.getSolrInputDocument());
    cmdDistrib.distribAdd(cmd, Collections.singletonList(targetLeaderNode), new ModifiableSolrParams(outParamsToLeader));
  }
}
 
Example #22
Source File: CdcrUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean versionAdd(AddUpdateCommand cmd) throws IOException {
  /*
  temporarily set the PEER_SYNC flag so that DistributedUpdateProcessor.versionAdd doesn't execute leader logic
  but the else part of that if. That way version remains preserved.

  we cannot set the flag for the whole processAdd method because DistributedUpdateProcessor.setupRequest() would set
  isLeader to false which wouldn't work
   */
  if (cmd.getReq().getParams().get(CDCR_UPDATE) != null) {
    cmd.setFlags(cmd.getFlags() | UpdateCommand.PEER_SYNC); // we need super.versionAdd() to set leaderLogic to false
  }

  boolean result = super.versionAdd(cmd);

  // unset the flag to avoid unintended consequences down the chain
  if (cmd.getReq().getParams().get(CDCR_UPDATE) != null) {
    cmd.setFlags(cmd.getFlags() & ~UpdateCommand.PEER_SYNC);
  }

  return result;
}
 
Example #23
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testBooleanValuesInAdd() throws Exception {
  String str = "{'add':[{'id':'1','b1':true,'b2':false,'b3':[false,true]}]}".replace('\'', '"');
  SolrQueryRequest req = req();
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);
  JsonLoader loader = new JsonLoader();
  loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);

  assertEquals(1, p.addCommands.size());

  AddUpdateCommand add = p.addCommands.get(0);
  SolrInputDocument d = add.solrDoc;
  SolrInputField f = d.getField("b1");
  assertEquals(Boolean.TRUE, f.getValue());
  f = d.getField("b2");
  assertEquals(Boolean.FALSE, f.getValue());
  f = d.getField("b3");
  assertEquals(2, ((List)f.getValue()).size());
  assertEquals(Boolean.FALSE, ((List)f.getValue()).get(0));
  assertEquals(Boolean.TRUE, ((List)f.getValue()).get(1));

  req.close();
}
 
Example #24
Source File: RemoveTrailingUnderscoreProcessor.java    From apache-solr-essentials with Apache License 2.0 6 votes vote down vote up
/**
 * Intercept the add document operation.
 * Here this process gets a chance to change the incoming {@link SolrInputDocument}.
 * 
 * @param command the update command.
 * @throws IOException in case of I/O failure.
 */
@Override
public void processAdd(final AddUpdateCommand command) throws IOException {
	// 1. Retrieve the SolrInputDocument that contains data to be indexed.
	final SolrInputDocument document = command.getSolrInputDocument();
	
	// 2. Loop through the target fields
	for (final String fieldName : fields) {
		
		// 3. Get the field values (for simplicity we assume fields are monovalued and are strings)
		final String fieldValue = (String) document.getFieldValue(fieldName);
		
		// 4. Check and eventually change the value of that field.
		if (fieldValue != null && fieldValue.endsWith("_")) {
			document.setField(fieldName, fieldValue.substring(0, fieldValue.length() -1));
		}
	}
	
	// 5. IMPORTANT: forward the control to the next processor in the chain.
	super.processAdd(command);
}
 
Example #25
Source File: LocalGraph.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a new {@link LocalGraph} with the given data.
 * 
 * @param graphNode the graph name.
 * @param request the Solr query request.
 * @param response the Solr query response.
 * @param qparser the query parser.
 * @param fetchSize the fetch size that will be used in reads.
 * @param consumer the Graph event consumer that will be notified on relevant events.
 */
private LocalGraph(
	final Node graphNode, 
	final SolrQueryRequest request, 
	final SolrQueryResponse response, 
	final QParser qparser, 
	final int fetchSize, 
	final GraphEventConsumer consumer) {
	super(graphNode, consumer, fetchSize);
	this.graphTermQuery = new TermQuery(new Term(Field.C, graphNodeStringified));
	this.request = request;
	this.updateCommand = new AddUpdateCommand(request);
	this.updateProcessor = request.getCore().getUpdateProcessingChain(null).createProcessor(request, response);
	this.searcher = request.getSearcher();
	this.qParser = qparser;
}
 
Example #26
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void bayesMonoClass_sampleParams_shouldAssignCorrectClass() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMonoClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word4 word4 word4",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.BAYES);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());
  updateProcessorToTest.processAdd(update);

  assertThat(unseenDocument1.getFieldValue(TRAINING_CLASS),is("class1"));
}
 
Example #27
Source File: AbstractDefaultValueUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  final SolrInputDocument doc = cmd.getSolrInputDocument();

  if (! doc.containsKey(fieldName)) {
    doc.addField(fieldName, getDefaultValue());
  }

  super.processAdd(cmd);
}
 
Example #28
Source File: SkipExistingDocumentsProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkippableUpdateIsNotSkippedIfNotLeader() throws IOException {
  UpdateRequestProcessor next = Mockito.mock(DistributedUpdateProcessor.class);
  SkipExistingDocumentsUpdateProcessor processor
          = Mockito.spy(new SkipExistingDocumentsUpdateProcessor(defaultRequest, next, true, true));

  AddUpdateCommand cmd = createAtomicUpdateCmd(defaultRequest);
  doReturn(false).when(processor).isLeader(cmd);
  doReturn(false).when(processor).doesDocumentExist(docId);

  processor.processAdd(cmd);
  verify(next).processAdd(cmd);
}
 
Example #29
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testBigIntegerValuesInAdd() throws Exception {
  String str = ("{'add':[{'id':'1','bi1':123456789012345678901,'bi2':1098765432109876543210,"
               + "'bi3':[1234567890123456789012,10987654321098765432109]}]}").replace('\'', '"');
  SolrQueryRequest req = req();
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);
  JsonLoader loader = new JsonLoader();
  loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);

  assertEquals(1, p.addCommands.size());

  AddUpdateCommand add = p.addCommands.get(0);
  SolrInputDocument d = add.solrDoc;
  SolrInputField f = d.getField("bi1");
  assertTrue(f.getValue() instanceof String);
  assertEquals("123456789012345678901", f.getValue());
  f = d.getField("bi2");
  assertTrue(f.getValue() instanceof String);
  assertEquals("1098765432109876543210", f.getValue());
  f = d.getField("bi3");
  assertEquals(2, ((List)f.getValue()).size());
  assertTrue(((List)f.getValue()).contains("1234567890123456789012"));
  assertTrue(((List)f.getValue()).contains("10987654321098765432109"));

  req.close();
}
 
Example #30
Source File: SolrUpdateService.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the given document to the solr index without commit.
 *
 * @param docs the documents to add
 * @throws IOException iff something goes wrong
 */
public void add(Collection<SolrInputDocument> docs) throws IOException {
    for (SolrInputDocument doc : docs) {
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = doc;
        cmd.commitWithin = COMMIT_WITHIN;
        updateProcessor.processAdd(cmd);
    }
}