Java Code Examples for org.apache.solr.common.SolrInputDocument#remove()

The following examples show how to use org.apache.solr.common.SolrInputDocument#remove() . 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: DocumentBuilderTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptions() {
  SolrCore core = h.getCore();
  
  // make sure a null value is not indexed
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField( "id", "123" );
  doc.addField( "unknown", "something" );
  Exception ex = expectThrows(Exception.class, () -> DocumentBuilder.toDocument( doc, core.getLatestSchema() ));
  assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
  doc.remove( "unknown" );
  

  doc.addField( "weight", "not a number" );
  ex = expectThrows(Exception.class, () -> DocumentBuilder.toDocument( doc, core.getLatestSchema()));
  assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
  assertTrue( "cause is number format", ex.getCause() instanceof NumberFormatException );

  // now make sure it is OK
  doc.setField( "weight", "1.34" );
  DocumentBuilder.toDocument( doc, core.getLatestSchema() );
}
 
Example 2
Source File: FieldMutatingUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Calls <code>mutate</code> on any fields identified by the selector 
 * before forwarding the command down the chain.  Any SolrExceptions 
 * thrown by <code>mutate</code> will be logged with the Field name, 
 * wrapped and re-thrown.
 */
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  final SolrInputDocument doc = cmd.getSolrInputDocument();

  // make a copy we can iterate over while mutating the doc
  final Collection<String> fieldNames 
    = new ArrayList<>(doc.getFieldNames());

  for (final String fname : fieldNames) {

    if (! selector.shouldMutate(fname)) continue;
    
    final SolrInputField src = doc.get(fname);

    SolrInputField dest = null;
    try { 
      dest = mutate(src);
    } catch (SolrException e) {
      String msg = "Unable to mutate field '"+fname+"': "+e.getMessage();
      SolrException.log(log, msg, e);
      throw new SolrException(BAD_REQUEST, msg, e);
    }
    if (null == dest) {
      doc.remove(fname);
    } else {
      // semantics of what happens if dest has diff name are hard
      // we could treat it as a copy, or a rename
      // for now, don't allow it.
      if (! fname.equals(dest.getName()) ) {
        throw new SolrException(SERVER_ERROR,
                                "mutate returned field with different name: " 
                                + fname + " => " + dest.getName());
      }
      doc.put(dest.getName(), dest);
    }
  }
  super.processAdd(cmd);
}
 
Example 3
Source File: TestInPlaceUpdateWithRouteField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdatingDocValuesWithRouteField() throws Exception {

   new UpdateRequest()
    .deleteByQuery("*:*").commit(cluster.getSolrClient(), COLLECTION);
  
   new UpdateRequest().add(createDocs(NUMBER_OF_DOCS)).commit(cluster.getSolrClient(), COLLECTION);

  int id = TestUtil.nextInt(random(), 1, NUMBER_OF_DOCS - 1);
  SolrDocument solrDocument = queryDoc(id);
  Long initialVersion = (Long) solrDocument.get("_version_");
  Integer luceneDocId = (Integer) solrDocument.get("[docid]");
  String shardName = (String) solrDocument.get("shardName");
  Assert.assertThat(solrDocument.get("inplace_updatable_int"), is(id));

  int newDocValue = TestUtil.nextInt(random(), 1, 2 * NUMBER_OF_DOCS - 1);
  SolrInputDocument sdoc = sdoc("id", ""+id,
      // use route field in update command
      "shardName", shardName,
      "inplace_updatable_int", map("set", newDocValue));
  
  UpdateRequest updateRequest = new UpdateRequest()
      .add(sdoc);
  updateRequest.commit(cluster.getSolrClient(), COLLECTION);
  solrDocument = queryDoc(id);
  Long newVersion = (Long) solrDocument.get("_version_");
  Assert.assertTrue("Version of updated document must be greater than original one",
      newVersion > initialVersion);
  Assert.assertThat( "Doc value must be updated", solrDocument.get("inplace_updatable_int"), is(newDocValue));
  Assert.assertThat("Lucene doc id should not be changed for In-Place Updates.", solrDocument.get("[docid]"), is(luceneDocId));

  sdoc.remove("shardName");
  checkWrongCommandFailure(sdoc);

  sdoc.addField("shardName",  map("set", "newShardName"));
  checkWrongCommandFailure(sdoc);
}
 
Example 4
Source File: SplitCompoundFieldProcessor.java    From apache-solr-essentials with Apache License 2.0 5 votes vote down vote up
@Override
public void processAdd(final AddUpdateCommand command) throws IOException {
	// 1. Get the Solr (Input) document
	final SolrInputDocument document = command.getSolrInputDocument();
	
	// 2. Get the value of the compound field 
	final String compoundValue = (String) document.getFieldValue(COMPOUND_FIELD_NAME);

	// 3. Split the value and create the other fields
	if (compoundValue != null) {
		
		// 4. Create and populate the "year" field.
		if (compoundValue.length() >=4) {
			final String year = compoundValue.substring(0, 4);
			document.setField("year", year);
		}
		
		// 5. Create and populate the "language" field.
		if (compoundValue.length() >=39) {
			final String language = compoundValue.substring(36, 39);
			document.setField("language", language);				
		}
		
		// 6. Remove the compound field.
		document.remove(COMPOUND_FIELD_NAME);
	}
	
	// 7. IMPORTANT: forward the control to the next processor in the chain.
	super.processAdd(command);
}
 
Example 5
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
boolean getUpdatedDocument(AddUpdateCommand cmd, long versionOnUpdate) throws IOException {
  if (!AtomicUpdateDocumentMerger.isAtomicUpdate(cmd)) return false;

  Set<String> inPlaceUpdatedFields = AtomicUpdateDocumentMerger.computeInPlaceUpdatableFields(cmd);
  if (inPlaceUpdatedFields.size() > 0) { // non-empty means this is suitable for in-place updates
    if (docMerger.doInPlaceUpdateMerge(cmd, inPlaceUpdatedFields)) {
      return true;
    } else {
      // in-place update failed, so fall through and re-try the same with a full atomic update
    }
  }
  
  // full (non-inplace) atomic update
  SolrInputDocument sdoc = cmd.getSolrInputDocument();
  BytesRef idBytes = cmd.getIndexedId();
  String idString = cmd.getPrintableId();
  SolrInputDocument oldRootDocWithChildren = RealTimeGetComponent.getInputDocument(cmd.getReq().getCore(), idBytes, RealTimeGetComponent.Resolution.ROOT_WITH_CHILDREN);

  if (oldRootDocWithChildren == null) {
    if (versionOnUpdate > 0) {
      // could just let the optimistic locking throw the error
      throw new SolrException(ErrorCode.CONFLICT, "Document not found for update.  id=" + idString);
    } else if (req.getParams().get(ShardParams._ROUTE_) != null) {
      // the specified document could not be found in this shard
      // and was explicitly routed using _route_
      throw new SolrException(ErrorCode.BAD_REQUEST,
          "Could not find document id=" + idString +
              ", perhaps the wrong \"_route_\" param was supplied");
    }
  } else {
    oldRootDocWithChildren.remove(CommonParams.VERSION_FIELD);
  }


  SolrInputDocument mergedDoc;
  if(idField == null || oldRootDocWithChildren == null) {
    // create a new doc by default if an old one wasn't found
    mergedDoc = docMerger.merge(sdoc, new SolrInputDocument());
  } else {
    // Safety check: don't allow an update to an existing doc that has children, unless we actually support this.
    if (req.getSchema().isUsableForChildDocs() // however, next line we see it doesn't support child docs
        && req.getSchema().supportsPartialUpdatesOfChildDocs() == false
        && req.getSearcher().count(new TermQuery(new Term(IndexSchema.ROOT_FIELD_NAME, idBytes))) > 1) {
      throw new SolrException(ErrorCode.BAD_REQUEST, "This schema does not support partial updates to nested docs. See ref guide.");
    }

    String oldRootDocRootFieldVal = (String) oldRootDocWithChildren.getFieldValue(IndexSchema.ROOT_FIELD_NAME);
    if(req.getSchema().savesChildDocRelations() && oldRootDocRootFieldVal != null &&
        !idString.equals(oldRootDocRootFieldVal)) {
      // this is an update where the updated doc is not the root document
      SolrInputDocument sdocWithChildren = RealTimeGetComponent.getInputDocument(cmd.getReq().getCore(),
          idBytes, RealTimeGetComponent.Resolution.DOC_WITH_CHILDREN);
      mergedDoc = docMerger.mergeChildDoc(sdoc, oldRootDocWithChildren, sdocWithChildren);
    } else {
      mergedDoc = docMerger.merge(sdoc, oldRootDocWithChildren);
    }
  }
  cmd.solrDoc = mergedDoc;
  return true;
}
 
Example 6
Source File: MetricsCollectorHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  SolrInputDocument doc = cmd.solrDoc;
  if (doc == null) {
    return;
  }
  String metricName = (String)doc.getFieldValue(MetricUtils.METRIC_NAME);
  if (metricName == null) {
    log.warn("Missing {} field in document, skipping: {}", MetricUtils.METRIC_NAME, doc);
    return;
  }
  doc.remove(MetricUtils.METRIC_NAME);
  // XXX we could modify keys by using this original registry name
  doc.remove(SolrReporter.REGISTRY_ID);
  String groupId = (String)doc.getFieldValue(SolrReporter.GROUP_ID);
  if (groupId == null) {
    log.warn("Missing {}  field in document, skipping: {}", SolrReporter.GROUP_ID, doc);
    return;
  }
  doc.remove(SolrReporter.GROUP_ID);
  String reporterId = (String)doc.getFieldValue(SolrReporter.REPORTER_ID);
  if (reporterId == null) {
    log.warn("Missing {} field in document, skipping: {}", SolrReporter.REPORTER_ID, doc);
    return;
  }
  doc.remove(SolrReporter.REPORTER_ID);
  String labelId = (String)doc.getFieldValue(SolrReporter.LABEL_ID);
  doc.remove(SolrReporter.LABEL_ID);
  doc.forEach(f -> {
    String key;
    if (doc.size() == 1 && f.getName().equals(MetricUtils.VALUE)) {
      // only one "value" field - skip the unnecessary field name
      key = MetricRegistry.name(labelId, metricName);
    } else {
      key = MetricRegistry.name(labelId, metricName, f.getName());
    }
    MetricRegistry registry = metricManager.registry(groupId);
    AggregateMetric metric = getOrCreate(registry, key);
    Object o = f.getFirstValue();
    if (o != null) {
      metric.set(reporterId, o);
    } else {
      // remove missing values
      metric.clear(reporterId);
    }
  });
}