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>"); }
/** * Generates an <add><doc>... XML String with options on the add. * * @param doc the Document to add * @param args 0th and Even numbered args are param names, Odds are param values. * @see #add * @see #doc */ public String add(Doc doc, String... args) { try { StringWriter r = new StringWriter(); // this is anoying if (null == args || 0 == args.length) { r.write("<add>"); r.write(doc.xml); r.write("</add>"); } else { XML.writeUnescapedXML(r, "add", doc.xml, (Object[]) args); } return r.getBuffer().toString(); } catch (IOException e) { throw new RuntimeException("this should never happen with a StringWriter", e); } }