use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class AddUpdateCommand method getPrintableId.
public String getPrintableId() {
if (req != null) {
IndexSchema schema = req.getSchema();
SchemaField sf = schema.getUniqueKeyField();
if (solrDoc != null && sf != null) {
SolrInputField field = solrDoc.getField(sf.getName());
if (field != null) {
return field.getFirstValue().toString();
}
}
}
return "(null)";
}
use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class FieldMutatingUpdateProcessor method processAdd.
/**
* 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 {
// 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);
}
use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class FieldNameMutatingUpdateProcessorFactory method getInstance.
@Override
public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
return new UpdateRequestProcessor(next) {
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
final SolrInputDocument doc = cmd.getSolrInputDocument();
final Collection<String> fieldNames = new ArrayList<>(doc.getFieldNames());
for (final String fname : fieldNames) {
Matcher matcher = pattern.matcher(fname);
if (matcher.find()) {
String newFieldName = matcher.replaceAll(replacement);
if (!newFieldName.equals(fname)) {
SolrInputField old = doc.remove(fname);
old.setName(newFieldName);
doc.put(newFieldName, old);
}
}
}
super.processAdd(cmd);
}
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
super.processDelete(cmd);
}
};
}
use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class FieldValueMutatingUpdateProcessor method mutate.
@Override
protected final SolrInputField mutate(final SolrInputField src) {
Collection<Object> values = src.getValues();
//don't mutate
if (values == null)
return src;
SolrInputField result = new SolrInputField(src.getName());
for (final Object srcVal : values) {
final Object destVal = mutateValue(srcVal);
if (DELETE_VALUE_SINGLETON == destVal) {
/* NOOP */
log.debug("removing value from field '{}': {}", src.getName(), srcVal);
} else {
if (destVal != srcVal) {
log.debug("replace value from field '{}': {} with {}", new Object[] { src.getName(), srcVal, destVal });
}
result.addValue(destVal);
}
}
return 0 == result.getValueCount() ? null : result;
}
use of org.apache.solr.common.SolrInputField in project stanbol by apache.
the class SolrYard method createSolrInputDocument.
/**
* Internally used to create Solr input documents for parsed representations.
* <p>
* This method supports boosting of fields. The boost is calculated by combining
* <ol>
* <li>the boot for the whole representation - by calling {@link #getDocumentBoost(Representation)}
* <li>the boost of each field - by using the configured {@link #fieldBoostMap}
* </ol>
*
* @param representation
* the representation
* @return the Solr document for indexing
*/
protected final SolrInputDocument createSolrInputDocument(Representation representation) {
SolrYardConfig config = (SolrYardConfig) getConfig();
SolrInputDocument inputDocument = new SolrInputDocument();
// domain for all added documents!
if (config.isMultiYardIndexLayout()) {
inputDocument.addField(fieldMapper.getDocumentDomainField(), config.getId());
}
// else we need to do nothing
inputDocument.addField(fieldMapper.getDocumentIdField(), representation.getId());
// first process the document boost
Float documentBoost = getDocumentBoost(representation);
// document boosts and are not multiplied with with document boosts
if (documentBoost != null) {
inputDocument.setDocumentBoost(documentBoost);
}
for (Iterator<String> fields = representation.getFieldNames(); fields.hasNext(); ) {
// TODO: maybe add some functionality to prevent indexing of the
// field configured as documentBoostFieldName!
// But this would also prevent the possibility to intentionally
// override the boost.
String field = fields.next();
/*
* With STANBOL-1027 the calculation of the boost has changed to
* consider multiple values for Representation#get(field).
*/
//the boost without considering the number of values per solr field
float baseBoost;
Float fieldBoost = fieldBoostMap == null ? null : fieldBoostMap.get(field);
//used to keep track of field we need boost
final Map<String, int[]> fieldsToBoost;
if (fieldBoost != null) {
baseBoost = documentBoost != null ? fieldBoost * documentBoost : fieldBoost;
fieldsToBoost = new HashMap<String, int[]>();
} else {
baseBoost = -1;
fieldsToBoost = null;
}
// does already exactly that (in an more efficient way)
for (Iterator<Object> values = representation.get(field); values.hasNext(); ) {
// now we need to get the indexField for the value
Object next = values.next();
IndexValue value;
try {
value = indexValueFactory.createIndexValue(next);
for (String fieldName : fieldMapper.getFieldNames(Arrays.asList(field), value)) {
//In step (1) of boosting just keep track of the field
if (fieldBoost != null) {
//wee need to boost in (2)
int[] numValues = fieldsToBoost.get(fieldName);
if (numValues == null) {
numValues = new int[] { 1 };
fieldsToBoost.put(fieldName, numValues);
//the first time add the document with the baseBoost
//as this will be the correct boost for single value fields
inputDocument.addField(fieldName, value.getValue(), baseBoost);
} else {
numValues[0]++;
//for multi valued fields the correct boost is set in (2)
//so we can add here without an boost
inputDocument.addField(fieldName, value.getValue());
}
} else {
//add add the values without boost
inputDocument.addField(fieldName, value.getValue());
}
}
} catch (NoConverterException e) {
log.warn(String.format("Unable to convert value %s (type:%s) for field %s!", next, next.getClass(), field), e);
} catch (IllegalArgumentException e) {
//usually because the Object is NULL or empty
if (log.isDebugEnabled()) {
log.debug(String.format("Illegal Value %s (type:%s) for field %s!", next, next.getClass(), field), e);
}
} catch (RuntimeException e) {
log.warn(String.format("Unable to process value %s (type:%s) for field %s!", next, next.getClass(), field), e);
}
}
if (fieldBoost != null) {
//we need still to do part (2) of setting the correct boost
for (Entry<String, int[]> entry : fieldsToBoost.entrySet()) {
if (entry.getValue()[0] > 1) {
//adapt the boost only for multi valued fields
SolrInputField solrField = inputDocument.getField(entry.getKey());
//the correct bosst is baseBoost (representing entity boost with field
//boost) multiplied with the sqrt(fieldValues). The 2nd part aims to
//compensate the Solr lengthNorm (1/sqrt(fieldTokens))
//see STANBOL-1027 for details
solrField.setBoost(baseBoost * (float) Math.sqrt(entry.getValue()[0]));
}
}
}
}
return inputDocument;
}
Aggregations