2012-03-06 72 views
0

我想将编码字符串发送到Solr,然后在检索时对其进行解码。我的编码是这样的:Solr编码/解码数据

public static String compress(String inputString) { 
    try { 
     if (inputString == null || inputString.length() == 0) { 
      return null; 
     } 
     return new String(compress(inputString.getBytes("UTF-8"))); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 


private static byte[] compress(byte[] input) { 
    try { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     GZIPOutputStream gzip = new GZIPOutputStream(out); 
     gzip.write(input); 
     gzip.close(); 
     return out.toByteArray(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

然后我发到SOLR,当我试图把它找回来(忽略现在的解码,因为它没有在这里)

SolrDocument resultDoc = iter.next(); 
String content = (String) resultDoc.getFieldValue("source"); 
System.out.println(content); 

如果我发送一个字符串,因为“你好我的名字是克里斯”编码将看起来像(忽略什么堆栈溢出改变);

ã�������ÛHÕ……W»≠T»KÃMU»,VpŒ( ,�ìùùG��� 

但我回来从SOLR是

#31;ã#8;#0;#0;#0;#0;#0;#0;#0;ÛHÕ……W»≠T»KÃMU»,VpŒ( ,#6;#0;ìùùG#22;#0;#0;#0; 

这显然会令解码失败。我已经尝试使用Jetty安装和Tomcat都有相同的问题。

回答

1

请参阅Solr发行版随附的示例schema.xml文件中的此条目。

<!--Binary data type. The data should be sent/retrieved in as Base64 encoded Strings --> 
<fieldtype name="binary" class="solr.BinaryField"/> 

确保您使用存储在索引中的编码值的字段使用binary字段类型和所使用编码字符串的base64。