Example usage for org.apache.solr.schema CopyField getDestination

List of usage examples for org.apache.solr.schema CopyField getDestination

Introduction

In this page you can find the example usage for org.apache.solr.schema CopyField getDestination.

Prototype

public SchemaField getDestination() 

Source Link

Usage

From source file:com.searchbox.engine.solr.EmbeddedSolr.java

License:Apache License

private boolean addCopyFields(Collection collection, Field field, Set<String> copyFields) {
    SolrCore core = coreContainer.getCore(collection.getName());
    IndexSchema schema = core.getLatestSchema();

    for (CopyField copyField : schema.getCopyFieldsList(field.getKey())) {
        copyFields.remove(copyField.getDestination().getName());
    }//  ww  w .ja v  a 2  s .c o  m

    Map<String, java.util.Collection<String>> copyFieldsMap = new HashMap<String, java.util.Collection<String>>();
    copyFieldsMap.put(field.getKey(), copyFields);
    schema = schema.addCopyFields(copyFieldsMap);

    core.setLatestSchema(schema);

    return true;
}

From source file:lux.solr.SolrIndexConfig.java

License:Mozilla Public License

private void registerXmlTextFields() {
    String xmlFieldName = indexConfig.getFieldName(FieldRole.XML_TEXT);
    SchemaField schemaField = schema.getFieldOrNull(xmlFieldName);
    Analyzer xmlAnalyzer = null;//  www  .  ja  va 2s . co  m
    Analyzer xmlQueryAnalyzer = null;
    if (schemaField != null) {
        xmlAnalyzer = schemaField.getType().getAnalyzer();
        xmlQueryAnalyzer = schemaField.getType().getQueryAnalyzer();
        if (xmlAnalyzer != null) {
            for (FieldRole role : new FieldRole[] { FieldRole.XML_TEXT, FieldRole.ELEMENT_TEXT,
                    FieldRole.ATTRIBUTE_TEXT }) {
                FieldDefinition field = indexConfig.getField(role);
                field.setAnalyzer(xmlAnalyzer); // this analyzer is used when indexing
                field.setQueryAnalyzer(xmlQueryAnalyzer); // this analyzer is used when indexing
                indexConfig.getFieldAnalyzers().put(field.getName(), xmlQueryAnalyzer); // this analyzer is used when parsing queries
            }
        }
    }
    for (CopyField copyField : schema.getCopyFieldsList(xmlFieldName)) {
        // register fields copied from lux_text with the indexer so that we feed them an XdmNode
        SchemaField destination = copyField.getDestination();
        Analyzer analyzer = destination.getType().getAnalyzer();
        if (analyzer == null) {
            // can this happen?? what about the query analyzer? can that be null? 
            if (xmlAnalyzer != null) {
                analyzer = xmlAnalyzer; // why would you copy it then?
            } else {
                analyzer = new DefaultAnalyzer();
            }
        }
        // TODO: should there be additional element and attribute text fields as well?
        XmlTextField xmlCopyField = new XmlTextField(destination.getName(), analyzer);
        xmlCopyField.setQueryAnalyzer(analyzer);
        indexConfig.addField(xmlCopyField);
    }
}

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()) + "] ";
            }// ww  w.  j a  v  a2 s . c o 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;
}