2012-03-06 75 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。