Example usage for org.apache.solr.common SolrInputDocument putAll

List of usage examples for org.apache.solr.common SolrInputDocument putAll

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputDocument putAll.

Prototype

@Override
    public void putAll(Map<? extends String, ? extends SolrInputField> t) 

Source Link

Usage

From source file:nl.minbzk.dwr.zoeken.enricher.generator.SolrGenerator.java

License:Open Source License

/**
 * {@inheritDoc}/*w w  w.  ja v  a  2  s. com*/
 */
@Override
public void process(final ImportEnvelope envelope, final ProcessorResult processorResult,
        final GeneratorResult generatorResult, final EnricherSettings settings, final EnricherJob job)
        throws Exception {
    Map<String, SolrInputField> metaFields = processMetaFields(envelope, processorResult, generatorResult,
            settings, job);

    String effectiveLanguage = determineEffectiveLanguage(envelope, processorResult, job);

    boolean isLanguageDetectionEnabled = job.getLanguageDetectionParameter() != null;

    // Now add all separate sections as documents

    for (int i = 0; i < processorResult.getContent().size(); i++) {
        ProcessorContent content = processorResult.getContent().get(i);

        SolrInputDocument document = new SolrInputDocument();

        // XXX: IDs can consist of the reference ('id'), document section number ('section') or any piece of content metadata

        if (!StringUtils.isEmpty(settings.getSolrUniqueKeyComposition())) {
            String idValue = "";

            for (String field : settings.getSolrUniqueKeyComposition().split(",")) {
                String fieldValue = null;

                if (field.trim().equalsIgnoreCase("id"))
                    fieldValue = envelope.getReference();
                else if (field.trim().equalsIgnoreCase("section"))
                    fieldValue = Integer.toString(i);
                else if (metaFields.containsKey(field.trim())) {
                    if (metaFields.get(field.trim()).getValueCount() == 1)
                        fieldValue = metaFields.get(field.trim()).getFirstValue().toString();
                    else
                        logger.error("The content metadata field '" + field.trim()
                                + "' does not contain exactly one value - not adding to the composite key");
                } else if (logger.isDebugEnabled())
                    logger.debug("Not adding missing content metadata field '" + field.trim()
                            + "' (not necessarily an error)");

                if (fieldValue != null)
                    idValue += (idValue.length() > 0 ? " - " : "") + fieldValue;
            }

            document.addField(FIELD_ID, idValue);
        } else {
            // XXX: We typically implement document sections by having each processor content piece contain its own ID

            document.addField(FIELD_ID, i + " - " + envelope.getReference());
        }

        // And the content-specific metadata

        for (Entry<String, List<String>> contentMetadata : content.getMetadata().entrySet()) {
            if (logger.isDebugEnabled())
                logger.debug("Adding field '" + contentMetadata.getKey() + "' with "
                        + contentMetadata.getValue().size() + " value(s)");

            SolrInputField solrInputField = new SolrInputField(contentMetadata.getKey());

            for (String contentMetadataValue : contentMetadata.getValue()) {
                if (logger.isDebugEnabled())
                    logger.debug("Adding field value '" + contentMetadata.getKey() + "' = '"
                            + contentMetadataValue + "'");

                solrInputField.addValue(contentMetadataValue, 1.0f);
            }

            metaFields.put(contentMetadata.getKey(), solrInputField);
        }

        // Add the regular metadata fields

        document.putAll(metaFields);

        // And finally the content

        document.addField(FIELD_CONTENT
                + (isLanguageDetectionEnabled && effectiveLanguage != null ? "-" + effectiveLanguage : ""),
                content.getContent());

        if (logger.isDebugEnabled())
            logger.debug("Content block " + i + " contains " + content.getContent().length() + " characters"
                    + (isLanguageDetectionEnabled && effectiveLanguage != null
                            ? " in language " + effectiveLanguage
                            : ""));

        ((MultiGeneratorResult) generatorResult).addDocument(job, document);
    }
}

From source file:org.roda.core.index.IndexModelObserver.java

private ReturnWithExceptions<Long> indexFile(AIP aip, File file, List<String> ancestors, boolean recursive) {
    ReturnWithExceptions<Long> exceptions = new ReturnWithExceptions<>();
    Long sizeInBytes = 0L;//from   w  w  w .j a  v  a  2s.  c  o m
    SolrInputDocument fileDocument = SolrUtils.fileToSolrDocument(aip, file, ancestors);

    // Add information from PREMIS
    Binary premisFile = getFilePremisFile(file);
    if (premisFile != null) {
        try {
            SolrInputDocument premisSolrDoc = PremisV3Utils.getSolrDocument(premisFile);
            fileDocument.putAll(premisSolrDoc);
            sizeInBytes = SolrUtils.objectToLong(premisSolrDoc.get(RodaConstants.FILE_SIZE).getValue(), 0L);
        } catch (GenericException e) {
            LOGGER.warn("Could not index file PREMIS information", e);
            exceptions.addException(e);
        }
    }

    // Add full text
    String fulltext = getFileFulltext(file);
    if (fulltext != null) {
        fileDocument.addField(RodaConstants.FILE_FULLTEXT, fulltext);

    }

    try {
        index.add(RodaConstants.INDEX_FILE, fileDocument);
    } catch (SolrServerException | SolrException | IOException e) {
        LOGGER.error("Cannot index file: {}", file, e);
        exceptions.addException(e);
    }

    if (recursive && file.isDirectory()) {
        try {
            CloseableIterable<OptionalWithCause<File>> allFiles = model.listFilesUnder(file, true);
            for (OptionalWithCause<File> subfile : allFiles) {
                if (subfile.isPresent()) {
                    ReturnWithExceptions<Long> ret = indexFile(aip, subfile.get(), ancestors, false);
                    sizeInBytes += ret.getRet();
                    exceptions.addExceptions(ret.getExceptions());
                } else {
                    LOGGER.error("Cannot index file", subfile.getCause());
                    exceptions.addException(subfile.getCause());
                }
            }
            IOUtils.closeQuietly(allFiles);

        } catch (NotFoundException | GenericException | RequestNotValidException
                | AuthorizationDeniedException e) {
            LOGGER.error("Cannot index file sub-resources: {}", file, e);
            exceptions.addException(e);
        }
    }

    exceptions.setRet(sizeInBytes);
    return exceptions;
}