List of usage examples for org.apache.solr.schema IndexSchema getCopyFieldsList
public List<CopyField> getCopyFieldsList(final String sourceField)
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.jav a2 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: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 w ww . j ava 2 s. c om 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; }