Solr编码/解码数据

时间:2012-03-06 06:02:49

标签: java encoding solr decode

我正在尝试将编码的字符串发送到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);

如果我发送一个字符串,例如“Hello my name is Chris”,编码将会显示(忽略堆栈溢出已更改);

 ã�������Û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 个答案:

答案 0 :(得分: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 fieldType,并且您使用的是base64编码字符串。