Advertisement
Guest User

Untitled

a guest
Aug 12th, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package net.semanticmetadata.lire.solr;
  2.  
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5.  
  6. import org.apache.lucene.document.Field;
  7. import org.apache.lucene.index.IndexableField;
  8. import org.apache.lucene.search.SortField;
  9. import org.apache.lucene.util.BytesRef;
  10. import org.apache.solr.common.util.Base64;
  11. import org.apache.solr.response.TextResponseWriter;
  12. import org.apache.solr.schema.FieldType;
  13. import org.apache.solr.schema.SchemaField;
  14.  
  15.  
  16. public class BinaryDocValuesField extends FieldType {
  17.  
  18.     private String toBase64String(ByteBuffer buf) {
  19.         return Base64.byteArrayToBase64(buf.array(), buf.position(), buf.limit() - buf.position());
  20.     }
  21.  
  22.     @Override
  23.     public void write(TextResponseWriter writer, String name, IndexableField f) throws IOException {
  24.         writer.writeStr(name, toBase64String(toObject(f)), false);
  25.     }
  26.  
  27.     @Override
  28.     public SortField getSortField(SchemaField field, boolean top) {
  29.         throw new RuntimeException("Cannot sort on a Binary field");
  30.     }
  31.  
  32.  
  33.     @Override
  34.     public String toExternal(IndexableField f) {
  35.         return ("Not possible ...");
  36.     }
  37.  
  38.     @Override
  39.     public ByteBuffer toObject(IndexableField f) {
  40.         BytesRef bytes = f.binaryValue();
  41.         return  ByteBuffer.wrap(bytes.bytes, bytes.offset, bytes.length);
  42.     }
  43.  
  44.     @Override
  45.     public IndexableField createField(SchemaField field, Object val, float boost) {
  46.         if (val == null) return null;
  47.         if (!field.stored()) {
  48.             return null;
  49.         }
  50.         byte[] buf = null;
  51.         int offset = 0, len = 0;
  52.         if (val instanceof byte[]) {
  53.             buf = (byte[]) val;
  54.             len = buf.length;
  55.         } else if (val instanceof ByteBuffer && ((ByteBuffer)val).hasArray()) {
  56.             ByteBuffer byteBuf = (ByteBuffer) val;
  57.             buf = byteBuf.array();
  58.             offset = byteBuf.position();
  59.             len = byteBuf.limit() - byteBuf.position();
  60.         } else {
  61.             String strVal = val.toString();
  62.             //the string has to be a base64 encoded string
  63.             buf = Base64.base64ToByteArray(strVal);
  64.             offset = 0;
  65.             len = buf.length;
  66.         }
  67.  
  68.         Field f = new org.apache.lucene.document.BinaryDocValuesField(field.getName(), new BytesRef(buf, offset, len));
  69.         f.setBoost(boost);
  70.         return f;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement