3

I'm using spring data solr to index data. When the entity contains field of type byte[], here comes the exception when inserting data into solr.

Exception: org.springframework.data.solr.UncategorizedSolrException:

ERROR: [doc=a1-t1] Error adding field 'contents'='[1, 2, 3, 4, -1, 2, 3, 8]' msg=String length must be a multiple of four.; nested exception is org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException:

ERROR: [doc=a1-t1] Error adding field 'contents'='[1, 2, 3, 4, -1, 2, 3, 8]' msg=String length must be a multiple of four. ([1, 2 ...] is just the test data.)

Below is the infomation about my code. Entity:

@IdClass(AttachedFileSolrPk.class)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@SolrDocument(solrCoreName = "attached_file")
public class AttachedFileSolr implements Serializable {

    /**
     * ID
     */
    @Id
    @Field
    private String id;

//  @ElementCollection(targetClass=byte.class)
    @Field
    @NotNull
    private byte[] contents;

    //other fields are omitted
}

Repository:

public interface AttachedFileSolrRepository extends SolrCrudRepository<AttachedFileSolr, AttachedFileSolrPk> {
    //custom interface are omitted
}

Call Repository:

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"/>

framework: spring boot, spring data solr starter

<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>

solr version: 6.1.0

My solr is working well with other type such as string, date and boolean, but it's my first time using binary field. I think it's easy just adjust the schema.xml and java entity, but it turns out not to be so. I have tried a lot, such as changing byte[] to be byte and multiValued="true" to be false, or indexed="false" to be false, and so on, but always comes the same exception. I appreciate any advice by anybody. Thanks!

Community
  • 1
  • 1
Yorick
  • 31
  • 3

1 Answers1

-1

Using SolrJ.

To add doc:

    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

When retrieving doc from solr:

    SolrDocument doc;
    byte[] bytes = (byte[]) doc.getFieldValue("binary");
    byte[] decoded = java.util.Base64.getDecoder().decode(bytes);
GrosMelon
  • 1
  • 2
  • Please include your own experience/your own code. Referring to other sites and not providing an answer by yourself is low quality. – Dominik Mar 09 '21 at 14:11