Search in sources :

Example 41 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 42 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 43 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 44 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 45 with SolrInputField

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;
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField) IndexValue(org.apache.stanbol.entityhub.yard.solr.model.IndexValue) NoConverterException(org.apache.stanbol.entityhub.yard.solr.model.NoConverterException) SolrInputDocument(org.apache.solr.common.SolrInputDocument)

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