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

The following examples show how to use org.apache.solr.common.SolrInputField#addValue() . 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: MappingSolrConverter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private Collection<SolrInputField> writeRegularPropertyToTarget(final Map<? super Object, ? super Object> target,
		SolrPersistentProperty persistentProperty, Object fieldValue) {

	SolrInputField field = new SolrInputField(persistentProperty.getFieldName());

	if (persistentProperty.isCollectionLike()) {
		Collection<?> collection = asCollection(fieldValue);
		for (Object o : collection) {
			if (o != null) {
				field.addValue(convertToSolrType(persistentProperty.getType(), o), 1f);
			}
		}
	} else {
		field.setValue(convertToSolrType(persistentProperty.getType(), fieldValue), 1f);
	}

	target.put(persistentProperty.getFieldName(), field);

	return Collections.singleton(field);

}
 
Example 2
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 3
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 4
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 5
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 6
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 7
Source File: UpdateProcessorTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for building up SolrInputFields
 */
final SolrInputField field(String name, Object... values) {
  SolrInputField f = new SolrInputField(name);
  for (Object v : values) {
    f.addValue(v);
  }
  return f;
}
 
Example 8
Source File: UUIDUpdateProcessorFallbackTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for building up SolrInputFields
 */
SolrInputField field(String name, float boost, Object... values) {
  SolrInputField f = new SolrInputField(name);
  for (Object v : values) {
    f.addValue(v);
  }
  return f;
}
 
Example 9
Source File: DefaultValueUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** 
 * Convenience method for building up SolrInputFields
 */
SolrInputField field(String name, Object... values) {
  SolrInputField f = new SolrInputField(name);
  for (Object v : values) {
    f.addValue(v);
  }
  return f;
}
 
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;
}