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

The following examples show how to use org.apache.solr.common.SolrInputField#getValues() . 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: AutocompleteUpdateRequestProcessor.java    From solr-autocomplete with Apache License 2.0 6 votes vote down vote up
private void addField(SolrInputDocument doc, String name, String value) {
  // find if such field already exists
  if (doc.get(name) == null) {
    doc.addField(name, value);
  } else {
    // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check
    SolrInputField f = doc.get(name);
    
    boolean valueExists = false;
    
    for (Object existingValue : f.getValues()) {
      if (existingValue == null && value == null) {
        valueExists = true;
        break;
      }
      if (existingValue != null && value != null && existingValue.equals(value)) {
        valueExists = true;
        break;
      }
    }
      
    if (!valueExists) {
      f.addValue(value);
    }
  }
}
 
Example 2
Source File: CustomIndexLoader.java    From solr-autocomplete with Apache License 2.0 6 votes vote down vote up
private static void addField(SolrInputDocument doc, String name, String value) {
  // find if such field already exists
  if (doc.get(name) == null) {
    // System.out.println("Adding field " + name + " without previous values");
    doc.addField(name, value);
  } else {
    // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check
    SolrInputField f = doc.get(name);
    for (Object val : f.getValues()) {
      // fix for boolean values
      if ((value.equalsIgnoreCase("t") && val.toString().equalsIgnoreCase("true")) ||
          (value.equalsIgnoreCase("f") && val.toString().equalsIgnoreCase("false"))) {
            return;
      }
      if (value.equals(val.toString())) {
        // if we find such value in the doc, we will not add it again
        // System.out.println("Field " + name + " already contains value " + value);
        return;
      }
    }
    // System.out.println("Adding field " + name + " without new value " + value);
    f.addValue(value);
  }
}
 
Example 3
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param fullDoc the full doc to  be compared against
 * @param partialDoc the sub document to be tested
 * @return whether partialDoc is derived from fullDoc
 */
public static boolean isDerivedFromDoc(SolrInputDocument fullDoc, SolrInputDocument partialDoc) {
  for(SolrInputField subSif: partialDoc) {
    Collection<Object> fieldValues = fullDoc.getFieldValues(subSif.getName());
    if (fieldValues == null) return false;
    if (fieldValues.size() < subSif.getValueCount()) return false;
    Collection<Object> partialFieldValues = subSif.getValues();
    // filter all derived child docs from partial field values since they fail List#containsAll check (uses SolrInputDocument#equals which fails).
    // If a child doc exists in partialDoc but not in full doc, it will not be filtered, and therefore List#containsAll will return false
    Stream<Object> nonChildDocElements = partialFieldValues.stream().filter(x -> !(isChildDoc(x) &&
        (fieldValues.stream().anyMatch(y ->
            (isChildDoc(x) &&
                isDerivedFromDoc((SolrInputDocument) y, (SolrInputDocument) x)
            )
        )
        )));
    if (!nonChildDocElements.allMatch(fieldValues::contains)) return false;
  }
  return true;
}
 
Example 4
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 5
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 6
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 7
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 8
Source File: RowMutationHelper.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
private static RecordMutation createRecordMutation(SolrInputDocument doc, String id) {
  RecordMutation recordMutation = new RecordMutation();
  // TODO: what's solr default behavior?
  recordMutation.setRecordMutationType(RecordMutationType.REPLACE_ENTIRE_RECORD);
  Record record = new Record();
  record.setFamily(findFamily(doc));
  record.setRecordId(id);

  for (String fieldName : doc.getFieldNames()) {
    if (!fieldName.contains(".")) {
      continue;
    }
    SolrInputField field = doc.getField(fieldName);
    String rawColumnName = fieldName.substring(fieldName.indexOf(".") + 1, fieldName.length());

    if (field.getValueCount() > 1) {
      for (Object fieldVal : field.getValues()) {
        record.addToColumns(new Column(rawColumnName, fieldVal.toString()));
      }
    } else {
      record.addToColumns(new Column(rawColumnName, field.getFirstValue().toString()));
    }
  }
  recordMutation.setRecord(record);
  return recordMutation;
}
 
Example 9
Source File: AutocompleteUpdateRequestProcessor.java    From solr-autocomplete with Apache License 2.0 5 votes vote down vote up
private void addCopyAsIsFields(SolrInputDocument doc, SolrInputField[] copyAsIsFieldsValues) {
  if (copyAsIsFieldsValues != null) {
    for (SolrInputField f : copyAsIsFieldsValues) {
      if (f != null) {
        Collection<Object> values = f.getValues();
        
        if (values != null && values.size() > 0) {
          addField(doc, f.getName(), values);
        }
      }
    }
  }
}
 
Example 10
Source File: CustomIndexLoader.java    From solr-autocomplete with Apache License 2.0 5 votes vote down vote up
private static void addField(SolrInputDocument doc, String name, String [] values) {
  // find if such field already exists
  if (doc.get(name) == null) {
    doc.addField(name, values);
  } else {
    // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check
    SolrInputField f = doc.get(name);
    for (String v : values) {
      boolean valueAlreadyIn = false;
      for (Object val : f.getValues()) {
        if (v.equals(val.toString())) {
          // if we find such value in the doc, we will not add it again
          valueAlreadyIn = true;
          break;
        }
        // fix for boolean values
        if ((v.equalsIgnoreCase("t") && val.toString().equalsIgnoreCase("true")) ||
            (v.equalsIgnoreCase("f") && val.toString().equalsIgnoreCase("false"))) {
          valueAlreadyIn = true;
          break;
        }
      }
      
      if (!valueAlreadyIn) {
        f.addValue(v);
      }
    }
  }
}
 
Example 11
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;
}