Search in sources :

Example 36 with SolrInputField

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)";
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrInputField(org.apache.solr.common.SolrInputField) IndexSchema(org.apache.solr.schema.IndexSchema)

Example 37 with SolrInputField

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);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputField(org.apache.solr.common.SolrInputField) ArrayList(java.util.ArrayList) SolrException(org.apache.solr.common.SolrException)

Example 38 with SolrInputField

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);
        }
    };
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) Matcher(java.util.regex.Matcher) SolrInputField(org.apache.solr.common.SolrInputField) ArrayList(java.util.ArrayList) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Example 39 with SolrInputField

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;
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField)

Example 40 with SolrInputField

use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.

the class ConcatFieldUpdateProcessorFactory method getInstance.

@Override
public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
    return mutator(getSelector(), next, src -> {
        if (src.getValueCount() <= 1)
            return src;
        SolrInputField result = new SolrInputField(src.getName());
        result.setValue(StringUtils.join(src.getValues(), delimiter));
        return result;
    });
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField)

Aggregations

SolrInputField (org.apache.solr.common.SolrInputField)45 SolrInputDocument (org.apache.solr.common.SolrInputDocument)25 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)14 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)12 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)11 JsonLoader (org.apache.solr.handler.loader.JsonLoader)11 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)11 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)11 Test (org.junit.Test)8 SolrException (org.apache.solr.common.SolrException)7 SchemaField (org.apache.solr.schema.SchemaField)7 Collection (java.util.Collection)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Matcher (java.util.regex.Matcher)3 IndexSchema (org.apache.solr.schema.IndexSchema)3 DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)3 HashMap (java.util.HashMap)2 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)2