使用Spring Data Solr处理字节[]类型的二进制字段
我使用SpringDataSolr来索引数据。当实体包含byte[]类型的字段时,在solr中插入数据时会出现异常 例外:org.springframework.data.solr.UncategorizedSolrException: 错误:[doc=a1-t1]添加字段“contents”=[1,2,3,4,-1,2, 3,8]'msg=字符串长度必须是四的倍数。;嵌套异常 是 org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: 错误:[doc=a1-t1]添加字段“contents”=[1,2,3,4,-1,2, 3,8]'msg=字符串长度必须是四的倍数。([1,2…]是 只是测试数据。) 下面是关于我的代码的信息。 实体: 存储库:使用Spring Data Solr处理字节[]类型的二进制字段,solr,binary,spring-data-solr,Solr,Binary,Spring Data Solr,我使用SpringDataSolr来索引数据。当实体包含byte[]类型的字段时,在solr中插入数据时会出现异常 例外:org.springframework.data.solr.UncategorizedSolrException: 错误:[doc=a1-t1]添加字段“contents”=[1,2,3,4,-1,2, 3,8]'msg=字符串长度必须是四的倍数。;嵌套异常 是 org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSo
public interface AttachedFileSolrRepository extends SolrCrudRepository<AttachedFileSolr, AttachedFileSolrPk> {
//custom interface are omitted
}
byte[] contents = {1, 2, 3, 4, -1, 2, 3, 8};
attachedFileSolr = AttachedFileSolr.builder().id("a1").contents(contents).build();
attachedFileSolrRepository.save(attachedFileSolr);
schema.xml
<fieldType name="binary" class="solr.BinaryField"/>
<field name="contents" type="binary" multiValued="true" indexed="false" required="true" stored="true"/>
框架:spring引导,spring数据解决方案启动器
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
org.springframework.boot
spring启动程序父级
1.3.5.1发布
org.springframework.boot
弹簧启动启动器数据解决方案
solr版本:6.1.0
我的solr可以很好地处理其他类型,如字符串、日期和布尔值,但这是我第一次使用二进制字段。我认为调整schema.xml和java实体很容易,但事实并非如此。我已经尝试了很多,比如将byte[]
更改为byte,将multiValued=“true”
更改为false,或者将index=“false”
更改为false,等等,但总是出现相同的异常。
我感谢任何人的建议。谢谢 使用SolrJ
要添加文档,请执行以下操作:
InputStream inputStream;
byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(inputStream);
byte[] encoded = java.util.Base64.getEncoder().encode(bytes);
SolrInputDocument solrDoc = new SolrInputDocument();
solrDoc.setField("binary", encoded); // binary solr field
// push doc to Solr
从solr检索文档时:
SolrDocument doc;
byte[] bytes = (byte[]) doc.getFieldValue("binary");
byte[] decoded = java.util.Base64.getDecoder().decode(bytes);
请包括您自己的经验/您自己的代码。参考其他网站而自己不提供答案是低质量的。
SolrDocument doc;
byte[] bytes = (byte[]) doc.getFieldValue("binary");
byte[] decoded = java.util.Base64.getDecoder().decode(bytes);