首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Apache Solr6中,如何避免在@Field注解的bean字段上设置ArrayList?

在Apache Solr6中,如何避免在@Field注解的bean字段上设置ArrayList?
EN

Stack Overflow用户
提问于 2017-05-10 04:52:52
回答 1查看 835关注 0票数 2

在这段非常简单的代码中

代码语言:javascript
复制
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);
}

在Java方法中使用新的Solr核心test1 (使用终端中的bin/solr create_core -c test1创建)

代码语言:javascript
复制
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]
}

我不断地得到

代码语言:javascript
复制
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

我不明白为什么Solr要在字符串字段上设置一个字符串列表。虫子?

https://github.com/krichter722/solr-bean-indexing上有一个MCVE。我找到了Retrieve Object from Solr,但它不能解释为什么我会得到异常,我感觉我在做与那里建议的相同的事情。

我使用的是Solr 6.5.1。

EN

回答 1

Stack Overflow用户

发布于 2017-05-11 08:05:33

在您的核心http://localhost:8983/solr/test1中,字段property1multivalued属性很可能设置为true

这意味着字段property1必须是一个ArrayList

如果您想要一个简单的String,那么您应该更改模式中的字段配置(设置multiValued="false")。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43879657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档