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

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

Introduction

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

Prototype

@Override
    public Collection<SolrInputField> values() 

Source Link

Usage

From source file:com.ngdata.hbaseindexer.indexer.FusionDocumentWriter.java

License:Apache License

/**
 * shs: New method added for handling Atomic Updates.
 * @param inputDocuments This is the list of documents that are atomic update documents.
 * @return inputDocuments (modified: the atomic update documents have been removed from the incoming parameter and
 *         only non-atomic update documents remain in that collection). The null value will be returned if all
 *         documents from the list of SolrInputDocuments have been removed.
 * @throws SolrServerException/* w  w w. j  a  v  a2s. co  m*/
 * @throws IOException
 */
protected List<SolrInputDocument> addAtomicUpdateDocuments(Collection<SolrInputDocument> inputDocuments)
        throws SolrServerException, IOException {
    // Visit each document in the collection and determine if it is a document that is an atomic update request. If it
    // is then add it to the atomicUpdateDocs and remove it from inputDocuments.
    Collection<SolrInputDocument> atomicUpdateDocuments = new ArrayList<SolrInputDocument>();

    List<SolrInputDocument> retain = null; // docs that aren't atomic updates

    Iterator<SolrInputDocument> docIterator = inputDocuments.iterator();
    while (docIterator.hasNext()) {
        boolean documentIsAtomic = false;
        SolrInputDocument doc = docIterator.next();
        for (SolrInputField solrInputField : doc.values()) {
            Object val = solrInputField.getValue();
            // If the type of the field just retrieved from the document is a Map object, then this could be an atomic
            // update document.
            if (val instanceof Map) {
                int entryCount = 0; // Used only for log messages. If the log message below is removed, this may also be deleted.
                for (Map.Entry<String, Object> entry : ((Map<String, Object>) val).entrySet()) {
                    String key = entry.getKey();
                    if (key.equals("add") || key.equals("set") || key.equals("remove")
                            || key.equals("removeregex") || key.equals("inc")) {
                        // keep track of the time we saw this doc on the hbase side
                        Map<String, String> atomicUpdateMap = new HashMap<String, String>();
                        atomicUpdateMap.put("set", DateUtil.getThreadLocalDateFormat().format(new Date()));
                        doc.addField("_hbasets_tdt", atomicUpdateMap);

                        // The atomic update documents should be added to the atomicUpdateDocs...
                        atomicUpdateDocuments.add(doc);
                        documentIsAtomic = true;
                        break; // from the entry for loop
                    }
                    entryCount++;
                }
            }
            // The document was determined to be an atomic update document, no need to visit the rest of the fields, so
            // break from the solrInputField loop.
            if (documentIsAtomic)
                break;

        } // end for

        if (!documentIsAtomic) {
            if (retain == null)
                retain = new ArrayList<SolrInputDocument>();
            retain.add(doc);
        }

    } // end while more docs

    // The following processing is necessary IFF there are documents in the atomicUpdateDocuments collection, which
    // means that we have documents needing an atomic update.
    if (atomicUpdateDocuments != null && !atomicUpdateDocuments.isEmpty()) {
        // For Fusion document submission, the SolrInputDocument must be converted to JSON; however, since these
        // documents are going directly to Solr and are NOT being submitted to a Fusion indexing pipeline, they do not
        // need to be converted to JSON.
        atomicUpdatesReceivedMeter.mark(atomicUpdateDocuments.size());
        try {
            // Submit atomic update documents to Solr at this point.
            solrProxy.add(atomicUpdateDocuments, 500);
            solrAtomicUpdatesMeter.mark(atomicUpdateDocuments.size());
        } catch (Exception e) {
            log.warn("Solr failed to process batch of " + atomicUpdateDocuments.size()
                    + " atomic updates due to: " + e + "; will re-try each doc individually");
            retrySolrAtomicUpdatesIndividually(atomicUpdateDocuments);
        }
    }
    // Finally, return any documents remaining in the inputDocuments collection for processing through the Fusion
    // indexing pipeline.
    return retain;
}