2

In the quite simple piece of code

SolrClient solrServer;
solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/test1")
        .build();
List<MyBean> myBeans = new LinkedList<>(Arrays.asList(new MyBean("a","1",1),new MyBean("b", "2", 2), new MyBean("c","3",3)));
String searchTerm = "a";
try {
    solrServer.addBeans(myBeans);
    solrServer.commit();
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.set("q", searchTerm);
    QueryResponse queryResponse = solrServer.query(solrQuery);
    List<MyBean> foundDocuments = queryResponse.getBeans(MyBean.class);
    System.out.println(foundDocuments);
} catch (SolrServerException | IOException ex) {
    throw new RuntimeException(ex);
}

with a fresh Solr core test1 (created with bin/solr create_core -c test1 in a terminal) in a Java SE main method with

public class MyBean {

    @Field
    private String property1;
    @Field
    private String property2;
    private int property3;

    public MyBean() {
    }

    public MyBean(String property1, String property2, int property3) {
        this.property1 = property1;
        this.property2 = property2;
        this.property3 = property3;
    }

    [public getter and setter for property1, property2 and property3]
}

I'm constantly getting

Exception in thread "main" org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class richtercloud.solr.bean.indexing.MyBean
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:71)
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBeans(DocumentObjectBinder.java:50)
    at org.apache.solr.client.solrj.response.QueryResponse.getBeans(QueryResponse.java:618)
    at richtercloud.solr.bean.indexing.NewMain.main(NewMain.java:42)
Caused by: org.apache.solr.client.solrj.beans.BindingException: Exception while setting value : [a] on private java.lang.String richtercloud.solr.bean.indexing.MyBean.property1
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:455)
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.inject(DocumentObjectBinder.java:438)
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:67)
    ... 3 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field richtercloud.solr.bean.indexing.MyBean.property1 to java.util.ArrayList
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:449)
    ... 5 more

and I can't wrap my head around why Solr would want to set a list of string on a string field. A bug?

A MCVE is at https://github.com/krichter722/solr-bean-indexing. I found Retrieve Object from Solr, but it doesn't explain why I'm getting the exception and I feel like I'm doing the same thing as suggested there.

I'm using Solr 6.5.1.

Community
  • 1
  • 1
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177

1 Answers1

3

Very likely in your core http://localhost:8983/solr/test1 the field property1 has multivalued attribute set to true.

This means that field property1 has to be an ArrayList.

If you want a simple String you should change the field configuration (set multiValued="false") in the schema.

Community
  • 1
  • 1
freedev
  • 25,946
  • 8
  • 108
  • 125