Java Code Examples for org.apache.solr.common.SolrInputField#getValue()

The following examples show how to use org.apache.solr.common.SolrInputField#getValue() . 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: SolrInputFieldEquivalence.java    From kafka-connect-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean doEquivalent(SolrInputField o1, SolrInputField o2) {
  if (o1.getValue() instanceof SolrInputDocument) {
    if (!(o2.getValue() instanceof SolrInputDocument)) {
      return false;
    }
    final MapDifference<String, SolrInputField> difference = Maps.difference(
        (SolrInputDocument) o1.getValue(),
        (SolrInputDocument) o2.getValue(),
        this
    );
    if (!difference.areEqual()) {
      return false;
    }
  } else {
    if (o1.getValue() != o2.getValue()) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: AddUpdateCommand.java    From lucene-solr with 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 3
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param updateSif the SolrInputField to update its values
 * @param cmdDocWChildren the doc to insert/set inside updateSif
 * @param updateDoc the document that was sent as part of the Add Update Command
 * @return updated SolrInputDocument
 */
@SuppressWarnings({"unchecked"})
public SolrInputDocument updateDocInSif(SolrInputField updateSif, SolrInputDocument cmdDocWChildren, SolrInputDocument updateDoc) {
  @SuppressWarnings({"rawtypes"})
  List sifToReplaceValues = (List) updateSif.getValues();
  final boolean wasList = updateSif.getValue() instanceof Collection;
  int index = getDocIndexFromCollection(cmdDocWChildren, sifToReplaceValues);
  SolrInputDocument updatedDoc = merge(updateDoc, cmdDocWChildren);
  if(index == -1) {
    sifToReplaceValues.add(updatedDoc);
  } else {
    sifToReplaceValues.set(index, updatedDoc);
  }
  // in the case where value was a List prior to the update and post update there is no more then one value
  // it should be kept as a List.
  final boolean singleVal = !wasList && sifToReplaceValues.size() <= 1;
  updateSif.setValue(singleVal? sifToReplaceValues.get(0): sifToReplaceValues);
  return cmdDocWChildren;
}
 
Example 4
Source File: TestDocumentObjectBinder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testSimple() throws Exception {
  DocumentObjectBinder binder = new DocumentObjectBinder();
  XMLResponseParser parser = new XMLResponseParser();
  NamedList<Object> nl = parser.processResponse(new StringReader(xml));
  QueryResponse res = new QueryResponse(nl, null);

  SolrDocumentList solDocList = res.getResults();
  List<Item> l = binder.getBeans(Item.class,res.getResults());
  assertEquals(solDocList.size(), l.size());
  assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);

  Item item = new Item();
  item.id = "aaa";
  item.categories = new String[] {"aaa", "bbb", "ccc"};
  SolrInputDocument out = binder.toSolrInputDocument(item);

  assertEquals(item.id, out.getFieldValue("id"));
  SolrInputField catfield = out.getField("cat");
  assertEquals(3, catfield.getValueCount());

  @SuppressWarnings({"unchecked"})
  List<String> catValues = (List<String>) catfield.getValue();
  assertEquals("aaa", catValues.get(0));
  assertEquals("bbb", catValues.get(1));
  assertEquals("ccc", catValues.get(2));
}
 
Example 5
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * 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 6
Source File: NestedUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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 7
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * @return whether or not to drop this cmd
 * @throws IOException If there is a low-level I/O error.
 */
protected boolean versionAdd(AddUpdateCommand cmd) throws IOException {
  BytesRef idBytes = cmd.getIndexedId();

  if (idBytes == null) {
    super.processAdd(cmd);
    return false;
  }

  if (vinfo == null) {
    if (AtomicUpdateDocumentMerger.isAtomicUpdate(cmd)) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Atomic document updates are not supported unless <updateLog/> is configured");
    } else {
      super.processAdd(cmd);
      return false;
    }
  }

  // This is only the hash for the bucket, and must be based only on the uniqueKey (i.e. do not use a pluggable hash
  // here)
  int bucketHash = bucketHash(idBytes);

  // at this point, there is an update we need to try and apply.
  // we may or may not be the leader.

  // Find any existing version in the document
  // TODO: don't reuse update commands any more!
  long versionOnUpdate = cmd.getVersion();

  if (versionOnUpdate == 0) {
    SolrInputField versionField = cmd.getSolrInputDocument().getField(CommonParams.VERSION_FIELD);
    if (versionField != null) {
      Object o = versionField.getValue();
      versionOnUpdate = o instanceof Number ? ((Number) o).longValue() : Long.parseLong(o.toString());
    } else {
      // Find the version
      String versionOnUpdateS = req.getParams().get(CommonParams.VERSION_FIELD);
      versionOnUpdate = versionOnUpdateS == null ? 0 : Long.parseLong(versionOnUpdateS);
    }
  }

  boolean isReplayOrPeersync = (cmd.getFlags() & (UpdateCommand.REPLAY | UpdateCommand.PEER_SYNC)) != 0;
  boolean leaderLogic = isLeader && !isReplayOrPeersync;
  boolean forwardedFromCollection = cmd.getReq().getParams().get(DISTRIB_FROM_COLLECTION) != null;

  VersionBucket bucket = vinfo.bucket(bucketHash);

  long dependentVersionFound = -1;
  // if this is an in-place update, check and wait if we should be waiting for a previous update (on which
  // this update depends), before entering the synchronized block
  if (!leaderLogic && cmd.isInPlaceUpdate()) {
    dependentVersionFound = waitForDependentUpdates(cmd, versionOnUpdate, isReplayOrPeersync, bucket);
    if (dependentVersionFound == -1) {
      // it means the document has been deleted by now at the leader. drop this update
      return true;
    }
  }

  vinfo.lockForUpdate();
  try {
    long finalVersionOnUpdate = versionOnUpdate;
    return bucket.runWithLock(vinfo.getVersionBucketLockTimeoutMs(), () -> doVersionAdd(cmd, finalVersionOnUpdate, isReplayOrPeersync, leaderLogic, forwardedFromCollection, bucket));
  } finally {
    vinfo.unlockForUpdate();
  }
}
 
Example 9
Source File: SignatureUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  if (enabled) {
    SolrInputDocument doc = cmd.getSolrInputDocument();
    List<String> currDocSigFields = null;
    boolean isPartialUpdate = AtomicUpdateDocumentMerger.isAtomicUpdate(cmd);
    if (sigFields == null || sigFields.size() == 0) {
      if (isPartialUpdate)  {
        throw new SolrException
            (ErrorCode.SERVER_ERROR,
                "Can't use SignatureUpdateProcessor with partial updates on signature fields");
      }
      Collection<String> docFields = doc.getFieldNames();
      currDocSigFields = new ArrayList<>(docFields.size());
      currDocSigFields.addAll(docFields);
      Collections.sort(currDocSigFields);
    } else {
      currDocSigFields = sigFields;
    }

    Signature sig = req.getCore().getResourceLoader().newInstance(signatureClass, Signature.class);
    sig.init(params);

    for (String field : currDocSigFields) {
      SolrInputField f = doc.getField(field);
      if (f != null) {
        if (isPartialUpdate)  {
          throw new SolrException
              (ErrorCode.SERVER_ERROR,
                  "Can't use SignatureUpdateProcessor with partial update request " +
                      "containing signature field: " + field);
        }
        sig.add(field);
        Object o = f.getValue();
        if (o instanceof Collection) {
          for (Object oo : (Collection)o) {
            sig.add(String.valueOf(oo));
          }
        } else {
          sig.add(String.valueOf(o));
        }
      }
    }

    byte[] signature = sig.getSignature();
    char[] arr = new char[signature.length<<1];
    for (int i=0; i<signature.length; i++) {
      int b = signature[i];
      int idx = i<<1;
      arr[idx]= StrUtils.HEX_DIGITS[(b >> 4) & 0xf];
      arr[idx+1]= StrUtils.HEX_DIGITS[b & 0xf];
    }
    String sigString = new String(arr);
    doc.addField(signatureField, sigString);

    if (overwriteDupes) {
      cmd.updateTerm = new Term(signatureField, sigString);
    }

  }

  if (next != null)
    next.processAdd(cmd);
}
 
Example 10
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a SolrInputDocument to SolrDocument, using an IndexSchema instance.
 *
 * @param sdoc The input document.
 * @param schema The index schema.
 * @param forInPlaceUpdate Whether the document is being used for an in place update,
 *                         see {@link DocumentBuilder#toDocument(SolrInputDocument, IndexSchema, boolean, boolean)}
 */
public static SolrDocument toSolrDoc(SolrInputDocument sdoc, IndexSchema schema, boolean forInPlaceUpdate) {
  // TODO what about child / nested docs?
  // TODO: do something more performant than this double conversion
  Document doc = DocumentBuilder.toDocument(sdoc, schema, forInPlaceUpdate, true);

  // copy the stored fields only
  Document out = new Document();
  for (IndexableField f : doc.getFields()) {
    if (f.fieldType().stored()) {
      out.add(f);
    } else if (f.fieldType().docValuesType() != DocValuesType.NONE) {
      SchemaField schemaField = schema.getFieldOrNull(f.name());
      if (schemaField != null && !schemaField.stored() && schemaField.useDocValuesAsStored()) {
        out.add(f);
      }
    } else {
      log.debug("Don't know how to handle field {}", f);
    }
  }

  SolrDocument solrDoc = toSolrDoc(out, schema);

  // add child docs
  for(SolrInputField solrInputField: sdoc) {
    if(solrInputField.getFirstValue() instanceof SolrInputDocument) {
      // is child doc
      Object val = solrInputField.getValue();
      Iterator<SolrDocument> childDocs = solrInputField.getValues().stream()
          .map(x -> toSolrDoc((SolrInputDocument) x, schema)).iterator();
      if(val instanceof Collection) {
        // add as collection even if single element collection
        solrDoc.setField(solrInputField.getName(), Lists.newArrayList(childDocs));
      } else {
        // single child doc
        solrDoc.setField(solrInputField.getName(), childDocs.next());
      }
    }
  }

  return solrDoc;
}