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

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

Introduction

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

Prototype

public String getName() 

Source Link

Usage

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 {//from   w  ww .  j  av  a  2  s.co  m
            merged.removeField(fieldName);
        }
    }

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

    return merged;
}

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  w  ww .  j  a va  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());
        }/* w w  w .j av  a 2s.c o  m*/
        // Append other fields
        else {
            doc.addField(field.getName(), field.getValue(), field.getBoost());
        }
    }
}

From source file:com.sindicetech.siren.solr.UpdateProcessorTestBase.java

License:Open Source License

/**
 * Convenience method for building up SolrInputDocuments
 *///  ww  w  .  j  av  a  2 s.c  o  m
protected final SolrInputDocument doc(SolrInputField... fields) {
    SolrInputDocument d = new SolrInputDocument();
    for (SolrInputField f : fields) {
        d.put(f.getName(), f);
    }
    return d;
}

From source file:edu.cmu.lti.oaqa.annographix.solr.SolrUtils.java

License:Apache License

/**
 * Take key-value pairs (field name, field text) and create an indexable document,
 * which is an object of the type {@link org.apache.solr.common.SolrInputDocument}.
 * //w ww . jav  a 2  s .c  o  m
 * @param    keyValueMap  key-value map (field name, field text).
 * 
 * @return an object of the type {@link org.apache.solr.common.SolrInputDocument},
 *         which can be indexed.
 * @throws Exception
 */
public static SolrInputDocument buildSolrDocument(HashMap<String, Object> keyValueMap) throws Exception {

    SolrInputDocument doc = new SolrInputDocument();

    Iterator<String> keys = keyValueMap.keySet().iterator();
    while (keys.hasNext()) {
        String key = keys.next();
        Object value = keyValueMap.get(key);

        SolrInputField field = new SolrInputField(key);
        try {

            doc.addField(field.getName(), value, 1.0f);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return doc;

}

From source file:edu.cmu.lti.oaqa.core.provider.solr.SolrWrapper.java

License:Apache License

/**Building solr document for indexing from key-value pairs**/
public SolrInputDocument buildSolrDocument(HashMap<String, Object> hshMap) throws Exception {

    SolrInputDocument doc = new SolrInputDocument();

    Iterator<String> keys = hshMap.keySet().iterator();
    while (keys.hasNext()) {
        String key = keys.next();
        Object value = hshMap.get(key);

        SolrInputField field = new SolrInputField(key);
        try {//from  w  ww.  j av  a 2s.c  o m

            doc.addField(field.getName(), value, 1.0f);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return doc;

}

From source file:net.yacy.cora.federate.solr.SchemaConfiguration.java

License:Open Source License

public SolrDocument toSolrDocument(final SolrInputDocument doc, Set<String> omitFields) {
    SolrDocument sd = new SolrDocument();
    for (SolrInputField field : doc) {
        if (this.contains(field.getName()) && (omitFields == null || !omitFields.contains(field.getName()))) { // check each field if enabled in local Solr schema
            sd.setField(field.getName(), field.getValue());
        }/*from w w  w.  j  a  v  a2  s  .c  o  m*/
    }
    return sd;
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

public static Document toDocument(SolrInputDocument doc, IndexSchema schema, AlfrescoSolrDataModel model) {
    Document out = new Document();
    out.setBoost(doc.getDocumentBoost());

    // Load fields from SolrDocument to Document
    for (SolrInputField field : doc) {
        String name = field.getName();
        SchemaField sfield = schema.getFieldOrNull(name);
        boolean used = false;
        float boost = field.getBoost();

        // Make sure it has the correct number
        if (sfield != null && !sfield.multiValued() && field.getValueCount() > 1) {
            String id = "";
            SchemaField sf = schema.getUniqueKeyField();
            if (sf != null) {
                id = "[" + doc.getFieldValue(sf.getName()) + "] ";
            }/*from ww  w . j a v  a2 s.co  m*/
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "ERROR: " + id + "multiple values encountered for non multiValued field " + sfield.getName()
                            + ": " + field.getValue());
        }

        // load each field value
        boolean hasField = false;
        for (Object v : field) {
            // TODO: Sort out null
            if (v == null) {
                continue;
            }
            String val = null;
            hasField = true;
            boolean isBinaryField = false;
            if (sfield != null && sfield.getType() instanceof BinaryField) {
                isBinaryField = true;
                BinaryField binaryField = (BinaryField) sfield.getType();
                Field f = binaryField.createField(sfield, v, boost);
                if (f != null)
                    out.add(f);
                used = true;
            } else {
                // TODO!!! HACK -- date conversion
                if (sfield != null && v instanceof Date && sfield.getType() instanceof DateField) {
                    DateField df = (DateField) sfield.getType();
                    val = df.toInternal((Date) v) + 'Z';
                } else if (v != null) {
                    val = v.toString();
                }

                if (sfield != null) {
                    if (v instanceof Reader) {
                        used = true;
                        Field f = new Field(field.getName(), (Reader) v, model.getFieldTermVec(sfield));
                        f.setOmitNorms(model.getOmitNorms(sfield));
                        f.setOmitTermFreqAndPositions(sfield.omitTf());

                        if (f != null) { // null fields are not added
                            out.add(f);
                        }
                    } else {
                        used = true;
                        Field f = sfield.createField(val, boost);
                        if (f != null) { // null fields are not added
                            out.add(f);
                        }
                    }
                }
            }

            // Check if we should copy this field to any other fields.
            // This could happen whether it is explicit or not.
            List<CopyField> copyFields = schema.getCopyFieldsList(name);
            for (CopyField cf : copyFields) {
                SchemaField destinationField = cf.getDestination();
                // check if the copy field is a multivalued or not
                if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "ERROR: multiple values encountered for non multiValued copy field "
                                    + destinationField.getName() + ": " + val);
                }

                used = true;
                Field f = null;
                if (isBinaryField) {
                    if (destinationField.getType() instanceof BinaryField) {
                        BinaryField binaryField = (BinaryField) destinationField.getType();
                        f = binaryField.createField(destinationField, v, boost);
                    }
                } else {
                    f = destinationField.createField(cf.getLimitedValue(val), boost);
                }
                if (f != null) { // null fields are not added
                    out.add(f);
                }
            }

            // In lucene, the boost for a given field is the product of the
            // document boost and *all* boosts on values of that field.
            // For multi-valued fields, we only want to set the boost on the
            // first field.
            boost = 1.0f;
        }

        // make sure the field was used somehow...
        if (!used && hasField) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR:unknown field '" + name + "'");
        }
    }

    // Now validate required fields or add default values
    // fields with default values are defacto 'required'
    for (SchemaField field : schema.getRequiredFields()) {
        if (out.getField(field.getName()) == null) {
            if (field.getDefaultValue() != null) {
                out.add(field.createField(field.getDefaultValue(), 1.0f));
            } else {
                String id = schema.printableUniqueKey(out);
                String msg = "Document [" + id + "] missing required field: " + field.getName();
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
            }
        }
    }
    return out;
}

From source file:org.apache.sentry.tests.e2e.solr.AbstractSolrSentryTestCase.java

License:Apache License

protected SolrDocumentList expectedDocs(SolrInputDocument... docs) {
    SolrDocumentList result = new SolrDocumentList();

    for (SolrInputDocument doc : docs) {
        SolrDocument r = new SolrDocument();
        for (SolrInputField field : doc) {
            r.setField(field.getName(), field.getValue());
        }/*from w  w w . java  2  s.  c o m*/
        result.add(r);
    }
    return result;
}

From source file:org.craftercms.search.service.impl.DenormalizingPostProcessor.java

License:Open Source License

protected Collection<SolrInputField> getParentFields(SolrInputDocument parentDoc) {
    List<SolrInputField> fields = new ArrayList<>();

    for (SolrInputField field : parentDoc) {
        if (!ArrayUtils.contains(fieldsToIgnore, field.getName())) {
            fields.add(field);/*from   ww w .j a  va2s.c o m*/
        }
    }

    return fields;
}