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

The following examples show how to use org.apache.solr.common.SolrInputDocument#containsKey() . 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: LanguageIdentifierUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Decides the fallback language, either from content of fallback field or fallback value
 * @param doc the Solr document
 * @param fallbackFields an array of strings with field names containing fallback language codes
 * @param fallbackValue a language code to use in case no fallbackFields are found
 */
private String getFallbackLang(SolrInputDocument doc, String[] fallbackFields, String fallbackValue) {
  String lang = null;
  for(String field : fallbackFields) {
    if(doc.containsKey(field)) {
      lang = (String) doc.getFieldValue(field);
      log.debug("Language fallback to field {}", field);
      break;
    }
  }
  if(lang == null) {
    log.debug("Language fallback to value {}", fallbackValue);
    lang = fallbackValue;
  }
  return lang;
}
 
Example 2
Source File: DocExpirationUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  final SolrInputDocument doc = cmd.getSolrInputDocument();

  final String math = doc.containsKey(ttlField) 
    ? doc.getFieldValue(ttlField).toString() : defaultTtl;

  if (null != math) {
    try {
      final DateMathParser dmp = new DateMathParser();
      // TODO: should we try to accept things like "1DAY" as well as "+1DAY" ?
      // How? 
      // 'startsWith("+")' is a bad idea because it would cause problems with
      // things like "/DAY+1YEAR"
      // Maybe catch ParseException and retry with "+" prepended?
      doc.addField(expireField, dmp.parseMath(math));
    } catch (ParseException pe) {
      throw new SolrException(BAD_REQUEST, "Can't parse ttl as date math: " + math, pe);
    }
  }

  super.processAdd(cmd);
}
 
Example 3
Source File: URLClassifyProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand command) throws IOException {
  if (isEnabled()) {
    SolrInputDocument document = command.getSolrInputDocument();
    if (document.containsKey(urlFieldname)) {
      String url = (String) document.getFieldValue(urlFieldname);
      try {
        URL normalizedURL = getNormalizedURL(url);
        document.setField(lengthFieldname, length(normalizedURL));
        document.setField(levelsFieldname, levels(normalizedURL));
        document.setField(toplevelpageFieldname, isTopLevelPage(normalizedURL) ? 1 : 0);
        document.setField(landingpageFieldname, isLandingPage(normalizedURL) ? 1 : 0);
        if (domainFieldname != null) {
          document.setField(domainFieldname, normalizedURL.getHost());
        }
        if (canonicalUrlFieldname != null) {
          document.setField(canonicalUrlFieldname, getCanonicalUrl(normalizedURL));
        }
        log.debug("{}", document);
      } catch (MalformedURLException | URISyntaxException e) {
        log.warn("cannot get the normalized url for '{}' due to {}", url, e.getMessage());
      }
    }
  }
  super.processAdd(command);
}
 
Example 4
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 5
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private SolrInputDocument buildDoc(Map<String, Object> m) {
  SolrInputDocument result = new SolrInputDocument();
  for (Map.Entry<String, Object> e : m.entrySet()) {
    if (mapEntryIsChildDoc(e.getValue())) { // parse child documents
      if (e.getValue() instanceof List) {
        List value = (List) e.getValue();
        for (Object o : value) {
          if (o instanceof Map) {
            // retain the value as a list, even if the list contains a single value.
            if(!result.containsKey(e.getKey())) {
              result.setField(e.getKey(), new ArrayList<>(1));
            }
            result.addField(e.getKey(), buildDoc((Map) o));
          }
        }
      } else if (e.getValue() instanceof Map) {
        result.addField(e.getKey(), buildDoc((Map) e.getValue()));
      }
    } else {
      result.setField(e.getKey(), e.getValue());
    }
  }
  return result;
}
 
Example 6
Source File: SolrWriter.java    From metron with Apache License 2.0 6 votes vote down vote up
public Collection<SolrInputDocument> toDocs(Iterable<BulkMessage<JSONObject>> messages) {
  Collection<SolrInputDocument> ret = new ArrayList<>();
  for(BulkMessage<JSONObject> bulkWriterMessage: messages) {
    SolrInputDocument document = new SolrInputDocument();
    JSONObject message = bulkWriterMessage.getMessage();
    for (Object key : message.keySet()) {
      Object value = message.get(key);
      if (value instanceof Iterable) {
        for (Object v : (Iterable) value) {
          document.addField("" + key, v);
        }
      } else {
        document.addField("" + key, value);
      }
    }
    if (!document.containsKey(Constants.GUID)) {
      document.addField(Constants.GUID, UUID.randomUUID().toString());
    }
    ret.add(document);
  }
  return ret;
}
 
Example 7
Source File: DeleteStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method that can handle String values when dealing with odd 
 * {@link Tuple} -&gt; {@link SolrInputDocument} conversions 
 * (ie: <code>tuple(..)</code> in tests)
 */
private static Long getVersion(final SolrInputDocument doc) throws NumberFormatException {
  if (! doc.containsKey(VERSION_FIELD)) {
    return null;
  }
  final Object v = doc.getFieldValue(VERSION_FIELD);
  if (null == v) {
    return null;
  }
  if (v instanceof Long) {
    return (Long)v;
  }
  return Long.parseLong(v.toString());
}
 
Example 8
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 9
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 10
Source File: AbstractSolrSentryTestBase.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
 * Function to validate the content of Solr response with that of input document.
 * @param solrInputDoc - Solr doc inserted into Solr
 * @param solrRespDocs - List of Solr doc obtained as response
 * (NOTE: This function ignores "_version_" field in validating Solr doc content)
 */
public void validateSolrDocContent(SolrInputDocument solrInputDoc,
                                   SolrDocumentList solrRespDocs) {
  for (SolrDocument solrRespDoc : solrRespDocs) {
    String expFieldValue = (String) solrInputDoc.getFieldValue("id");
    String resFieldValue = (String) solrRespDoc.getFieldValue("id");
    if (expFieldValue.equals(resFieldValue)) {
      int expectedRespFieldCount = solrRespDoc.size();
      if (solrRespDoc.containsKey("_version_")) {
        expectedRespFieldCount = expectedRespFieldCount - 1;
      }
      int expectedOrigFieldCount = solrInputDoc.size();
      if (solrInputDoc.containsKey("_version_")) {
        expectedOrigFieldCount = expectedOrigFieldCount - 1;
      }
      assertEquals("Expected " + expectedOrigFieldCount + " fields. But, found "
            + expectedRespFieldCount + " fields", expectedOrigFieldCount , expectedRespFieldCount);
      for (String field : solrInputDoc.getFieldNames()) {
        if (field.equals("_version_") == true) {
          continue;
        }

        expFieldValue = (String) solrInputDoc.getFieldValue(field);
        resFieldValue = (String) solrRespDoc.getFieldValue(field);
        assertEquals("Expected value for field: " + field + " is " + expFieldValue
            + "; But, found " + resFieldValue, expFieldValue, resFieldValue);
      }

      return;
    }
  }

  fail("Solr doc not found in Solr collection");
}
 
Example 11
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 12
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private boolean isChildDoc(SolrInputDocument extendedFieldValue) {
  return extendedFieldValue.containsKey(req.getSchema().getUniqueKeyField().getName());
}
 
Example 13
Source File: SolrLogPostTool.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void setFieldIfUnset(SolrInputDocument doc, String fieldName, String fieldValue) {
  if (doc.containsKey(fieldName)) return;

  doc.setField(fieldName, fieldValue);
}