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

The following examples show how to use org.apache.solr.common.SolrInputField#getName() . 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: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void doRemove(SolrInputDocument toDoc, SolrInputField sif, Object fieldVal) {
  final String name = sif.getName();
  SolrInputField existingField = toDoc.get(name);
  if (existingField == null) return;
  @SuppressWarnings({"rawtypes"})
  final Collection original = existingField.getValues();
  if (fieldVal instanceof Collection) {
    for (Object object : (Collection) fieldVal) {
      removeObj(original, object, name);
    }
  } else {
    removeObj(original, fieldVal, name);
  }

  toDoc.setField(name, original);
}
 
Example 2
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void doRemoveRegex(SolrInputDocument toDoc, SolrInputField sif, Object valuePatterns) {
  final String name = sif.getName();
  final SolrInputField existingField = toDoc.get(name);
  if (existingField != null) {
    final Collection<Object> valueToRemove = new HashSet<>();
    final Collection<Object> original = existingField.getValues();
    final Collection<Pattern> patterns = preparePatterns(valuePatterns);
    for (Object value : original) {
      for(Pattern pattern : patterns) {
        final Matcher m = pattern.matcher(value.toString());
        if (m.matches()) {
          valueToRemove.add(value);
        }
      }
    }
    original.removeAll(valueToRemove);
    toDoc.setField(name, original);
  }
}
 
Example 3
Source File: FieldValueMutatingUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected final SolrInputField mutate(final SolrInputField src) {
  Collection<Object> values = src.getValues();
  if(values == null) return src;//don't mutate
  SolrInputField result = new SolrInputField(src.getName());
  for (final Object srcVal : values) {
    final Object destVal = mutateValue(srcVal);
    if (DELETE_VALUE_SINGLETON == destVal) { 
      /* NOOP */
      if (log.isDebugEnabled()) {
        log.debug("removing value from field '{}': {}",
            src.getName(), srcVal);
      }
    } else {
      if (destVal != srcVal) {
        if (log.isDebugEnabled()) {
          log.debug("replace value from field '{}': {} with {}",
              new Object[]{src.getName(), srcVal, destVal});
        }
      }
      result.addValue(destVal);
    }
  }
  return 0 == result.getValueCount() ? null : result;
}
 
Example 4
Source File: PreAnalyzedUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected SolrInputField mutate(SolrInputField src) {
  SchemaField sf = schema.getFieldOrNull(src.getName());
  if (sf == null) { // remove this field
    return null;
  }
  FieldType type = PreAnalyzedField.createFieldType(sf);
  if (type == null) { // neither indexed nor stored - skip
    return null;
  }
  SolrInputField res = new SolrInputField(src.getName());
  for (Object o : src) {
    if (o == null) {
      continue;
    }
    Field pre = (Field)parser.createField(sf, o);
    if (pre != null) {
      res.addValue(pre);
    } else { // restore the original value
      log.warn("Could not parse field {} - using original value as is: {}", src.getName(), o);
      res.addValue(o);
    }
  }
  return res;
}
 
Example 5
Source File: MCRSolrFileIndexHandler.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private ModifiableSolrParams getSolrParams(Path file, BasicFileAttributes attrs) throws IOException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    SolrInputDocument doc = MCRSolrPathDocumentFactory.getInstance().getDocument(file, attrs);
    for (SolrInputField field : doc) {
        String name = "literal." + field.getName();
        if (field.getValueCount() > 1) {
            String[] values = getValues(field.getValues());
            params.set(name, values);
        } else {
            params.set(name, field.getValue().toString());
        }
    }
    return params;
}
 
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: 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 8
Source File: NestedAtomicUpdateTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
private static void assertDocContainsSubset(SolrInputDocument subsetDoc, SolrInputDocument fullDoc) {
  for(SolrInputField field: subsetDoc) {
    String fieldName = field.getName();
    assertTrue("doc should contain field: " + fieldName, fullDoc.containsKey(fieldName));
    Object fullValue = fullDoc.getField(fieldName).getValue();
    if(fullValue instanceof Collection) {
      ((Collection) fullValue).containsAll(field.getValues());
    } else {
      assertEquals("docs should have the same value for field: " + fieldName, field.getValue(), fullValue);
    }
  }
}
 
Example 9
Source File: ClientUtils.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
public static void writeXML( SolrInputDocument doc, Writer writer ) throws IOException
{
  writer.write("<doc>");

  for( SolrInputField field : doc ) {
    String name = field.getName();

    for( Object v : field ) {
      String update = null;

      if(v instanceof SolrInputDocument) {
        writeVal(writer, name, v , null);
      } else if (v instanceof Map) {
        // currently only supports a single value
        for (Entry<Object,Object> entry : ((Map<Object,Object>)v).entrySet()) {
          update = entry.getKey().toString();
          v = entry.getValue();
          if (v instanceof Collection) {
            @SuppressWarnings({"rawtypes"})
            Collection values = (Collection) v;
            for (Object value : values) {
              writeVal(writer, name, value, update);
            }
          } else  {
            writeVal(writer, name, v, update);
          }
        }
      } else  {
        writeVal(writer, name, v, update);
      }
    }
  }

  if (doc.hasChildDocuments()) {
    for (SolrInputDocument childDocument : doc.getChildDocuments()) {
      writeXML(childDocument, writer);
    }
  }
  
  writer.write("</doc>");
}
 
Example 10
Source File: AllValuesOrNoneFieldMutatingUpdateProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected final SolrInputField mutate(final SolrInputField srcField) {
  Collection<Object> vals = srcField.getValues();
  if(vals== null || vals.isEmpty()) return srcField;
  List<String> messages = null;
  SolrInputField result = new SolrInputField(srcField.getName());
  for (final Object srcVal : vals) {
    final Object destVal = mutateValue(srcVal);
    if (SKIP_FIELD_VALUE_LIST_SINGLETON == destVal) {
      if (log.isDebugEnabled()) {
        log.debug("field '{}' {} value '{}' is not mutable, so no values will be mutated",
            new Object[]{srcField.getName(), srcVal.getClass().getSimpleName(), srcVal});
      }
      return srcField;
    }
    if (DELETE_VALUE_SINGLETON == destVal) {
      if (log.isDebugEnabled()) {
        if (null == messages) {
          messages = new ArrayList<>();
        }
        messages.add(String.format(Locale.ROOT, "removing value from field '%s': %s '%s'", 
                                   srcField.getName(), srcVal.getClass().getSimpleName(), srcVal));
      }
    } else {
      if (log.isDebugEnabled()) {
        if (null == messages) {
          messages = new ArrayList<>();
        }
        messages.add(String.format(Locale.ROOT, "replace value from field '%s': %s '%s' with %s '%s'", 
                                   srcField.getName(), srcVal.getClass().getSimpleName(), srcVal, 
                                   destVal.getClass().getSimpleName(), destVal));
      }
      result.addValue(destVal);
    }
  }
  
  if (null != messages && log.isDebugEnabled()) {
    for (String message : messages) {
      log.debug(message);
    }
  }
  return 0 == result.getValueCount() ? null : result;
}
 
Example 11
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected void doSet(SolrInputDocument toDoc, SolrInputField sif, Object fieldVal) {
  String name = sif.getName();
  toDoc.setField(name, getNativeFieldValue(name, fieldVal));
}
 
Example 12
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected void doAdd(SolrInputDocument toDoc, SolrInputField sif, Object fieldVal) {
  String name = sif.getName();
  toDoc.addField(name, getNativeFieldValue(name, fieldVal));
}