gpt4 book ai didi

java - 如何避免在 Apache Solr 6 中的 @Field 注释 bean 字段上设置 ArrayList?

转载 作者:行者123 更新时间:2023-11-30 07:54:41 25 4
gpt4 key购买 nike

在非常简单的一段代码中

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 SE main 方法中使用新的 Solr 核心 test1(在终端中使用 bin/solr create_core -c test1 创建)

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

我不断得到

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 想要在字符串字段上设置字符串列表。错误?

MCVE 位于 https://github.com/krichter722/solr-bean-indexing .我找到了 Retrieve Object from Solr ,但这并不能解释为什么我会遇到异常,而且我觉得我正在按照那里的建议做同样的事情。

我正在使用 Solr 6.5.1。

最佳答案

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

这意味着字段 property1 必须是 ArrayList

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

关于java - 如何避免在 Apache Solr 6 中的 @Field 注释 bean 字段上设置 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com