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

The following examples show how to use org.apache.solr.common.SolrInputDocument#values() . These examples are extracted from open source projects. 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 Project: lucene-solr   File: AddUpdateCommand.java    License: Apache License 2.0 6 votes vote down vote up
/** Extract all child documents from parent that are saved in fields */
private void flattenLabelled(List<SolrInputDocument> unwrappedDocs, SolrInputDocument currentDoc, boolean isRoot) {
  for (SolrInputField field: currentDoc.values()) {
    Object value = field.getFirstValue();
    // check if value is a childDocument
    if (value instanceof SolrInputDocument) {
      Object val = field.getValue();
      if (!(val instanceof Collection)) {
        flattenLabelled(unwrappedDocs, ((SolrInputDocument) val));
        continue;
      }
      @SuppressWarnings({"unchecked"})
      Collection<SolrInputDocument> childrenList = ((Collection) val);
      for (SolrInputDocument child : childrenList) {
        flattenLabelled(unwrappedDocs, child);
      }
    }
  }

  if (!isRoot) unwrappedDocs.add(currentDoc);
}
 
Example 2
Source Project: examples   File: MorphlineMapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void load(SolrInputDocument doc) throws IOException, SolrServerException {
  String uniqueKeyFieldName = getSchema().getUniqueKeyField().getName();
  Object id = doc.getFieldValue(uniqueKeyFieldName);
  if (id == null) {
    throw new IllegalArgumentException("Missing value for (required) unique document key: " + uniqueKeyFieldName
        + " (see Solr schema.xml)");
  }
  try {
    context.write(new Text(id.toString()), new SolrInputDocumentWritable(doc));
  } catch (InterruptedException e) {
    throw new IOException("Interrupted while writing " + doc, e);
  }

  if (LOG.isDebugEnabled()) {
    long numParserOutputBytes = 0;
    for (SolrInputField field : doc.values()) {
      numParserOutputBytes += sizeOf(field.getValue());
    }
    context.getCounter(MorphlineCounters.class.getName(), MorphlineCounters.PARSER_OUTPUT_BYTES.toString()).increment(numParserOutputBytes);
  }
  context.getCounter(MorphlineCounters.class.getName(), MorphlineCounters.DOCS_READ.toString()).increment(1);
}
 
Example 3
/**
 * Utility method that examines the SolrInputDocument in an AddUpdateCommand
 * and returns true if the documents contains atomic update instructions.
 */
public static boolean isAtomicUpdate(final AddUpdateCommand cmd) {
  SolrInputDocument sdoc = cmd.getSolrInputDocument();
  for (SolrInputField sif : sdoc.values()) {
    Object val = sif.getValue();
    if (val instanceof Map && !(val instanceof SolrDocumentBase)) {
      return true;
    }
  }
  
  return false;
}
 
Example 4
private boolean processDocChildren(SolrInputDocument doc, String fullPath) {
  boolean isNested = false;
  for(SolrInputField field: doc.values()) {
    int childNum = 0;
    boolean isSingleVal = !(field.getValue() instanceof Collection);
    for(Object val: field) {
      if (!(val instanceof SolrInputDocument)) {
        // either all collection items are child docs or none are.
        break;
      }
      final String fieldName = field.getName();

      if (fieldName.contains(PATH_SEP_CHAR)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Field name: '" + fieldName
            + "' contains: '" + PATH_SEP_CHAR + "' , which is reserved for the nested URP");
      }
      final String sChildNum = isSingleVal ? SINGULAR_VALUE_CHAR : String.valueOf(childNum);
      SolrInputDocument cDoc = (SolrInputDocument) val;
      if (!cDoc.containsKey(uniqueKeyFieldName)) {
        String parentDocId = doc.getField(uniqueKeyFieldName).getFirstValue().toString();
        cDoc.setField(uniqueKeyFieldName, generateChildUniqueId(parentDocId, fieldName, sChildNum));
      }
      if (!isNested) {
        isNested = true;
      }
      final String lastKeyPath = PATH_SEP_CHAR + fieldName + NUM_SEP_CHAR + sChildNum;
      // concat of all paths children.grandChild => /children#1/grandChild#
      final String childDocPath = fullPath == null ? lastKeyPath : fullPath + lastKeyPath;
      processChildDoc(cDoc, doc, childDocPath);
      ++childNum;
    }
  }
  return isNested;
}
 
Example 5
static long estimate(SolrInputDocument doc) {
  if (doc == null) return 0L;
  long size = 0;
  for (SolrInputField inputField : doc.values()) {
    size += primitiveEstimate(inputField.getName(), 0L);
    size += estimate(inputField.getValue());
  }

  if (doc.hasChildDocuments()) {
    for (SolrInputDocument childDoc : doc.getChildDocuments()) {
      size += estimate(childDoc);
    }
  }
  return size;
}
 
Example 6
/**
 * Merges the fromDoc into the toDoc using the atomic update syntax.
 * 
 * @param fromDoc SolrInputDocument which will merged into the toDoc
 * @param toDoc the final SolrInputDocument that will be mutated with the values from the fromDoc atomic commands
 * @return toDoc with mutated values
 */
@SuppressWarnings({"unchecked"})
public SolrInputDocument merge(final SolrInputDocument fromDoc, SolrInputDocument toDoc) {
  for (SolrInputField sif : fromDoc.values()) {
   Object val = sif.getValue();
    if (val instanceof Map) {
      for (Entry<String,Object> entry : ((Map<String,Object>) val).entrySet()) {
        String key = entry.getKey();
        Object fieldVal = entry.getValue();
        switch (key) {
          case "add":
            doAdd(toDoc, sif, fieldVal);
            break;
          case "set":
            doSet(toDoc, sif, fieldVal);
            break;
          case "remove":
            doRemove(toDoc, sif, fieldVal);
            break;
          case "removeregex":
            doRemoveRegex(toDoc, sif, fieldVal);
            break;
          case "inc":
            doInc(toDoc, sif, fieldVal);
            break;
          case "add-distinct":
            doAddDistinct(toDoc, sif, fieldVal);
            break;
          default:
            Object id = toDoc.containsKey(idField.getName())? toDoc.getFieldValue(idField.getName()):
                fromDoc.getFieldValue(idField.getName());
            String err = "Unknown operation for the an atomic update, operation ignored: " + key;
            if (id != null) {
              err = err + " for id:" + id;
            }
            throw new SolrException(ErrorCode.BAD_REQUEST, err);
        }
        // validate that the field being modified is not the id field.
        if (idField.getName().equals(sif.getName())) {
          throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid update of id field: " + sif);
        }

      }
    } else {
      // normal fields are treated as a "set"
      toDoc.put(sif.getName(), sif);
    }
  }
  
  return toDoc;
}