Scenario
During develop and debug one Solr feature, I constantly need check the value of date field.
In response, the date field is string, but in eclipse debug mode, the value is long(milliseconds from epoch time) - as Solr actually stores the milliseconds in index, so why not write one transformer that add the milliseconds into response.
Here it is.
DateToLongTransfomerFactory
During develop and debug one Solr feature, I constantly need check the value of date field.
In response, the date field is string, but in eclipse debug mode, the value is long(milliseconds from epoch time) - as Solr actually stores the milliseconds in index, so why not write one transformer that add the milliseconds into response.
Here it is.
DateToLongTransfomerFactory
public class DateToLongTransfomerFactory extends TransformerFactory { @Override public DocTransformer create(String field, SolrParams params, SolrQueryRequest req) { return new DateToLongTransfomer(field, params); } /** * org.apache.solr.search.SolrReturnFields.parseFieldList(String[], * SolrQueryRequest) DocTransformers augmenters = new DocTransformers(); * * DocTransformer is thread safe. */ private static class DateToLongTransfomer extends DocTransformer { private String fl; private String field; public DateToLongTransfomer(String field, SolrParams params) { // field is the name of transformer [dateToLong] this.field = field; fl = Preconditions.checkNotNull(params.get("fl"), "fl can't be null in transfromer"); } @Override public void transform(SolrDocument doc, int docid) throws IOException { String fieldValue = getFieldValue(doc, fl); if (fieldValue != null) { doc.addField(field, fieldValue); } } public static String getFieldValue(SolrDocument doc, String field) { List<String> rst = new ArrayList<String>(); Object obj = doc.get(field); getFieldvalues(doc, rst, obj); if (rst.isEmpty()) { return null; } return rst.get(0); } public static void getFieldvalues(SolrDocument doc, List<String> rst, Object obj) { if (obj == null) return; if (obj instanceof org.apache.lucene.document.Field) { org.apache.lucene.document.Field field = (Field) obj; String oldValue = field.stringValue(); if (oldValue != null) { rst.add(oldValue); } } else if (obj instanceof IndexableField) { IndexableField field = (IndexableField) obj; String oldValue = field.stringValue(); if (oldValue != null) { rst.add(oldValue); } } else if (obj instanceof Collection) { Collection colls = (Collection) obj; for (Object newObj : colls) { getFieldvalues(doc, rst, newObj); } } else { rst.add(obj.toString()); // throw new RuntimeException("When this is called? obj.type:" // + obj.getClass()); } } } }