转自-----https://blog.csdn.net/u013378306/article/details/50761771;
首先有几点需要注意
客户端要调用solr服务,首先要把solr服务端工程启动,即前面文章讲的把solr下的slor.war例子放在tomcat下,进行相关配置,并启动。
(1)Exception in thread "main" org.apache.solr.client.solrj.beans.BindingException: class: class solr.PeopleBean does not define any fields.
是因为使用实体bean添加索引时,没有在实体属性上添加 Filed注解,导致solr无法匹配
如
@Field("name")
public void setName(String name) {
this.name = name;
}
public String[] getContent() {
return content;
}
(2)org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Expected mime type application
是因为当solr有多个core时,solrj操作时没有指定是哪个core,以下指定使用core0的数据
private final static String URL = "http://localhost:80/solr/core0";
HttpSolrServer server = new HttpSolrServer(URL);
(3)Exception in thread "main" org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class solr.PeopleBean
是因为当QueryResponse.getBeans(PeopleBean.class);方式查询,并返回实体bean时,必须有一个空的构造方法
public PeopleBean(){//此处应该注意,当QueryResponse.getBeans(PeopleBean.class);方式查询,并返回实体bean时,必须有一个 空的构造方法
}
(4)当明明有数据却查不到时,要注意查询的字段,是否开启了索引。即 在shema.xml中是否设置indexed="true"
<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
<field name="name" type="text_ik" indexed="true" stored="false" multiValued="false" />
<field name="content" type="text_ik" indexed="false" stored="true" multiValued="true" />
其中 indexed="true" 表示开启索引(当字段不需要被检索时,最好不要开启索引,) stored="true"表示存储原来数据(当字段不被检索,而只是需要通过其他字段检索而获得时,要设为true) multiValued="true" 表示返回多值,如一个返回多个content,此时要在java代码中把 content设置 集合或数组类型如
private String[] content;//多值,对应 multiValued="true"
(5)
需要以下jar包
。。。。
文章内容过多,建议根据本文转自网址https://blog.csdn.net/u013378306/article/details/50761771 进行查阅。