public boolean assertSolrInputFieldEquals(Object expected, Object actual) { if (!(expected instanceof SolrInputField) || !(actual instanceof SolrInputField)) { return false; } if (expected == actual) { return true; } SolrInputField sif1 = (SolrInputField) expected; SolrInputField sif2 = (SolrInputField) actual; if (!sif1.getName().equals(sif2.getName())) { return false; } if (!sif1.getValue().equals(sif2.getValue())) { return false; } return true; }
/** * Generates a simple <add><doc>... XML String with no options */ public String adoc(SolrInputDocument sdoc) { List<String> fields = new ArrayList<String>(); for (SolrInputField sf : sdoc) { for (Object o : sf.getValues()) { fields.add(sf.getName()); fields.add(o.toString()); } } return adoc(fields.toArray(new String[fields.size()])); }
private void addCopyAsIsFields(SolrInputDocument doc, SolrInputField[] copyAsIsFieldsValues) { if (copyAsIsFieldsValues != null) { for (SolrInputField f : copyAsIsFieldsValues) { if (f != null) { Collection<Object> values = f.getValues(); if (values != null && values.size() > 0) { addField(doc, f.getName(), values); } } } } } }
/** * Convenience method for building up SolrInputDocuments */ final SolrInputDocument doc(SolrInputField... fields) { SolrInputDocument d = new SolrInputDocument(); for (SolrInputField f : fields) { d.put(f.getName(), f); } return d; }
/** * Converts a SolrInputDocument into a Map * * @param doc the SolrInputDocument to convert * @return the input document as a map */ private Map<String, Object> convertToMap(SolrInputDocument doc) { // create the Map we will put the fields in Map<String, Object> newDoc = new HashMap<String, Object>(); // loop though all the fields and insert them into the map Collection<SolrInputField> fields = doc.values(); if (fields != null) { for (SolrInputField field : fields) { newDoc.put(field.getName(), field.getValue()); } } return newDoc; }
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()); } // Append other fields else { doc.addField(field.getName(), field.getValue(), field.getBoost()); } } }
/**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 { doc.addField(field.getName(), value, 1.0f); } catch (Exception e) { e.printStackTrace(); } } return doc; }
/** * 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}. * * @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; }
/** * @param d SolrInputDocument to convert * @return a SolrDocument with the same fields and values as the SolrInputDocument */ public static SolrDocument toSolrDocument( SolrInputDocument d ) { SolrDocument doc = new SolrDocument(); for( SolrInputField field : d ) { doc.setField( field.getName(), field.getValue() ); } return doc; }
private SolrInputField addToDocument(SolrInputDocument doc, String fieldName, String value, boolean raw) { if (!StringUtils.isBlank(fieldName) && !StringUtils.isBlank(value)) { SolrInputField f; if (!raw) { f = new SolrInputField(fieldName); f.setValue(value); } else { f = new SolrInputField(fieldName + "_raw"); f.setValue(value); } doc.addField(f.getName(), f.getValue()); if (raw) { addFieldNameToSchema(f.getName(), RAW_FIELD_TYPE_NAME, false, true); } else { addFieldNameToSchema(f.getName(), ANALYZED_FIELD_TYPE_NAME, false, true); } return f; } return null; }
private SolrInputField addToDocument(SolrInputDocument doc, String fieldName, String value, boolean raw) { if (!StringUtils.isBlank(fieldName) && !StringUtils.isBlank(value)) { SolrInputField f; if (!raw) { f = new SolrInputField(fieldName); f.setValue(value); } else { f = new SolrInputField(fieldName + "_raw"); f.setValue(value); } doc.addField(f.getName(), f.getValue()); if (raw) { addFieldNameToSchema(f.getName(), RAW_FIELD_TYPE_NAME, false, true); } else { addFieldNameToSchema(f.getName(), ANALYZED_FIELD_TYPE_NAME, false, true); } return f; } return null; }
public static void writeXML( SolrInputDocument doc, Writer writer ) throws IOException { writer.write("<doc boost=\""+doc.getDocumentBoost()+"\">"); for( SolrInputField field : doc ) { float boost = field.getBoost(); String name = field.getName(); for( Object v : field ) { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); }else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0,bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost ); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name ); } // only write the boost for the first multi-valued field // otherwise, the used boost is the product of all the boost values boost = 1.0f; } } writer.write("</doc>"); }
String name = field.getName();
public static void indexDescriptiveMetadataFields(ModelService model, String aipId, String representationId, List<DescriptiveMetadata> metadataList, SolrInputDocument doc) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { // guarding against repeated fields Set<String> usedNonRepeatableFields = new HashSet<>(); for (DescriptiveMetadata metadata : metadataList) { StoragePath storagePath = ModelUtils.getDescriptiveMetadataStoragePath(aipId, representationId, metadata.getId()); Binary binary = model.getStorage().getBinary(storagePath); try { SolrInputDocument fields = getDescriptiveMetadataFields(binary, metadata.getType(), metadata.getVersion()); for (SolrInputField field : fields) { if (NON_REPEATABLE_FIELDS.contains(field.getName())) { boolean added = usedNonRepeatableFields.add(field.getName()); if (added) { doc.addField(field.getName(), field.getValue()); } } else { doc.addField(field.getName(), field.getValue()); } } } catch (GenericException e) { LOGGER.warn("Problem processing descriptive metadata: {}", e.getMessage(), e); } catch (Exception e) { LOGGER.error("Error processing descriptive metadata: {}", metadata, e); } } }
String name = field.getName();
private List<NamedList> solrInputDocumentToList(SolrInputDocument doc) { List<NamedList> l = new ArrayList<NamedList>(); NamedList nl = new NamedList(); nl.add("boost", doc.getDocumentBoost() == 1.0f ? null : doc.getDocumentBoost()); l.add(nl); Iterator<SolrInputField> it = doc.iterator(); while (it.hasNext()) { nl = new NamedList(); SolrInputField field = it.next(); nl.add("name", field.getName()); nl.add("val", field.getValue()); nl.add("boost", field.getBoost() == 1.0f ? null : field.getBoost()); l.add(nl); } return l; }
/** * Convenience method for building up SolrInputDocuments */ protected final SolrInputDocument doc(SolrInputField... fields) { SolrInputDocument d = new SolrInputDocument(); for (SolrInputField f : fields) { d.put(f.getName(), f); } return d; }
public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException { List<SolrInputDocument> children = sdoc.getChildDocuments(); int sz = sdoc.size() + (children==null ? 0 : children.size()); writeTag(SOLRINPUTDOC, sz); writeFloat(1f); // document boost for (SolrInputField inputField : sdoc.values()) { writeExternString(inputField.getName()); writeVal(inputField.getValue()); } if (children != null) { for (SolrInputDocument child : children) { writeSolrInputDocument(child); } } }
/** * @param d SolrInputDocument to convert * @return a SolrDocument with the same fields and values as the SolrInputDocument */ public static SolrDocument toSolrDocument(SolrInputDocument d) { SolrDocument doc = new SolrDocument(); for (SolrInputField field : d) { doc.setField(field.getName(), field.getValue()); } if (d.getChildDocuments() != null) { for (SolrInputDocument in : d.getChildDocuments()) { doc.addChildDocument(toSolrDocument(in)); } } return doc; }
public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException { List<SolrInputDocument> children = sdoc.getChildDocuments(); int sz = sdoc.size() + (children==null ? 0 : children.size()); writeTag(SOLRINPUTDOC, sz); writeFloat(sdoc.getDocumentBoost()); for (SolrInputField inputField : sdoc.values()) { if (inputField.getBoost() != 1.0f) { writeFloat(inputField.getBoost()); } writeExternString(inputField.getName()); writeVal(inputField.getValue()); } if (children != null) { for (SolrInputDocument child : children) { writeSolrInputDocument(child); } } }