我正在尝试将编码字符串发送到 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 都遇到了同样的问题。
最佳答案
请参阅 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 编码字符串。
关于java - Solr 编码/解码数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578687/