Example usage for org.apache.solr.common SolrInputField getValue

List of usage examples for org.apache.solr.common SolrInputField getValue

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputField getValue.

Prototype

public Object getValue() 

Source Link

Usage

From source file:com.grantingersoll.intell.index.BayesUpdateRequestProcessor.java

License:Apache License

public ClassifierResult classifyDocument(SolrInputDocument doc) throws IOException {
    SolrInputField field = doc.getField(inputField);
    if (field == null)
        return null;

    if (!(field.getValue() instanceof String))
        return null;

    String[] tokens = tokenizeField((String) field.getValue());

    try {/*from   ww  w .j  av a  2  s . co m*/
        return ctx.classifyDocument(tokens, defaultCategory);
    } catch (InvalidDatastoreException e) {
        throw new IOException("Invalid Classifier Datastore", e);
    }
}

From source file:com.gu.solr.MergeUtils.java

License:Apache License

public static SolrInputDocument merge(SolrInputDocument solrInputDocument, SolrDocument existing,
        IndexSchema schema, boolean overwriteMultivalues) {
    SolrInputDocument merged = copy(existing);

    for (SolrInputField field : solrInputDocument) {
        String fieldName = field.getName();
        if (!overwriteMultivalues && schema.getField(fieldName).multiValued()) {
            // leave for additions
        } else {/*w w  w .j  ava2s  . com*/
            merged.removeField(fieldName);
        }
    }

    for (SolrInputField field : solrInputDocument) {
        merged.addField(field.getName(), field.getValue());
    }

    return merged;
}

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/* ww  w.  j  a  va2s  .  c  om*/
 * @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;
}

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

License:Apache License

/**
 * Add a SolrInputDocument to this writer.
 * <p>/*from  ww  w . ja  v a  2 s.c  o m*/
 * Adding multiple documents without ids will result in an IllegalStateException being thrown.
 */
@Override
public void add(SolrInputDocument solrDocument) {
    String docId = documentId;
    SolrInputField uniqueKeySolrField = solrDocument.getField(uniqueKeyField);
    if (uniqueKeySolrField == null) {
        if (idUsed) {
            throw new IllegalStateException(
                    "Document id '" + documentId + "' has already been used by this record");
        }
        solrDocument.addField(uniqueKeyField, documentId);
        idUsed = true;
    } else {
        docId = uniqueKeySolrField.getValue().toString();
    }

    if (tableNameField != null) {
        solrDocument.addField(tableNameField, tableName);
    }

    updateCollector.add(docId, solrDocument);
}

From source file:com.nominanuda.solr.SolrHelper.java

License:Apache License

public DataObject sid2DataObject(SolrInputDocument sid) {
    Map<String, Object> m = new LinkedHashMap<String, Object>();
    Iterator<SolrInputField> itr = sid.iterator();
    while (itr.hasNext()) {
        SolrInputField f = itr.next();
        m.put(f.getName(), f.getValue());
    }/*from ww w  . j  a v  a  2  s. c  o m*/
    return solrDoc2DataObject(m);
}

From source file:com.sindicetech.siren.solr.handler.DocumentBuilder.java

License:Open Source License

public void add(SolrInputField field) {
    if (field != null) {
        // Overwrite the 'id' field
        if (field.getName().equals(IdFieldMapper.INPUT_FIELD)) {
            doc.setField(field.getName(), field.getValue(), field.getBoost());
        }/*from  ww w.  j  ava2s .  c  o  m*/
        // Append other fields
        else {
            doc.addField(field.getName(), field.getValue(), field.getBoost());
        }
    }
}

From source file:com.tamingtext.classifier.bayes.BayesUpdateRequestProcessor.java

License:Apache License

public String[] tokenizeField(String fieldName, SolrInputField field) throws IOException {
    if (field == null)
        return new String[0];
    if (!(field.getValue() instanceof String))
        return new String[0];
    //<start id="mahout.bayes.tokenize"/>
    String input = (String) field.getValue();

    ArrayList<String> tokenList = new ArrayList<String>();
    TokenStream ts = analyzer.tokenStream(inputField, new StringReader(input));
    while (ts.incrementToken()) {
        tokenList.add(ts.getAttribute(CharTermAttribute.class).toString());
    }//  w w w .j  a  v  a2  s  .co  m
    String[] tokens = tokenList.toArray(new String[tokenList.size()]);
    //<end id="mahout.bayes.tokenize"/>
    return tokens;
}

From source file:edu.unc.lib.dl.data.ingest.solr.indexing.SolrUpdateDriver.java

License:Apache License

/**
 * Perform a partial document update from a IndexDocumentBean. Null fields are considered to be unspecified and will
 * not be changed, except for the update timestamp field which is always set.
 * //from  w  ww.  j  av  a  2s. c om
 * @param operation
 * @param idb
 * @throws IndexingException
 */
public void updateDocument(String operation, IndexDocumentBean idb) throws IndexingException {
    try {
        SolrInputDocument sid = solrServer.getBinder().toSolrInputDocument(idb);
        for (String fieldName : sid.getFieldNames()) {
            if (!ID_FIELD.equals(fieldName)) {
                SolrInputField inputField = sid.getField(fieldName);
                // Adding in each non-null field value, except the timestamp field which gets cleared if not specified so
                // that it always gets updated as part of a partial update
                // TODO enable timestamp updating when fix for SOLR-4133 is released, which enables setting null fields
                if (inputField != null
                        && (inputField.getValue() != null || UPDATE_TIMESTAMP.equals(fieldName))) {
                    Map<String, Object> partialUpdate = new HashMap<String, Object>();
                    partialUpdate.put(operation, inputField.getValue());
                    sid.setField(fieldName, partialUpdate);
                }
            }
        }
        log.debug("Performing partial update:\n" + ClientUtils.toXML(sid));
        solrServer.add(sid);
    } catch (IOException e) {
        throw new IndexingException("Failed to add document to solr", e);
    } catch (SolrServerException e) {
        throw new IndexingException("Failed to add document to solr", e);
    }
}

From source file:edu.unc.lib.dl.data.ingest.solr.test.AtomicUpdateTest.java

License:Apache License

@Test
public void atomicUpdate() throws IOException {
    IndexDocumentBean idb = new IndexDocumentBean();
    idb.setId("id");
    idb.setStatus(Arrays.asList("Unpublished", "Parent Unpublished"));

    DocumentObjectBinder binder = new DocumentObjectBinder();
    SolrInputDocument sid = binder.toSolrInputDocument(idb);

    String operation = "set";

    for (String fieldName : sid.getFieldNames()) {
        if (!ID_FIELD.equals(fieldName)) {
            SolrInputField inputField = sid.getField(fieldName);
            // Adding in each non-null field value, except the timestamp field which gets cleared if not specified so
            // that it always gets updated as part of a partial update
            // TODO enable timestamp updating when fix for SOLR-4133 is released, which enables setting null fields
            if (inputField != null && (inputField.getValue() != null || UPDATE_TIMESTAMP.equals(fieldName))) {
                Map<String, Object> partialUpdate = new HashMap<String, Object>();
                partialUpdate.put(operation, inputField.getValue());
                sid.setField(fieldName, partialUpdate);
            }/*w  w  w .j a v  a  2s.  c o m*/
        }
    }

    /*   Map<String,String> mapField = new HashMap<String,String>();
       mapField.put("", arg1)
       Map<String, Object> partialUpdate = new HashMap<String, Object>();
       partialUpdate.put("set", inputField.getFirstValue());
       sid.setField(fieldName, partialUpdate);*/

    StringWriter writer = new StringWriter();
    ClientUtils.writeXML(sid, writer);

    System.out.println(writer.toString());

    System.out.println(sid.toString());
}

From source file:io.confluent.connect.solr.sink.DefaultSolrInputDocumentConverterTest.java

License:Apache License

private void convertField(Schema fieldSchema, final Object inputValue, final Object expectedValue) {
    final String FIELD_NAME = "asdf";
    final String TOPIC = "test";
    final Schema valueSchema = SchemaBuilder.struct().field(FIELD_NAME, fieldSchema).build();
    final Field field = valueSchema.field(FIELD_NAME);
    final Struct value = new Struct(valueSchema).put(FIELD_NAME, inputValue);
    SolrInputDocument solrInputDocument = new SolrInputDocument();
    this.solrInputDocumentConverter.convertField(value, field, solrInputDocument);

    SolrInputField solrInputField = solrInputDocument.getField(FIELD_NAME);

    if (null == expectedValue) {
        Assert.assertNull(String.format("SolrInputDocument should not have '%s' field.", FIELD_NAME),
                solrInputField);//from  ww  w . ja  va  2  s .  c  o m
    } else {
        Assert.assertNotNull(String.format("SolrInputDocument should have '%s' field.", FIELD_NAME),
                solrInputField);

        final Object actualValue = solrInputField.getValue();
        Assert.assertEquals(
                String.format("'%s' field of SolrInputDocument does not have the expected value.", FIELD_NAME),
                expectedValue, actualValue);
    }
}