org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class 处理

solr进行范围超找时候抛异常:

1、问题描述:

solrJ中进行范围搜索时候报错:
org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class com.baidu.Item
Caused by: org.apache.solr.client.solrj.beans.BindingException: Exception while setting value
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.baidu.Item.title to java.util.ArrayList

在这里插入图片描述

2、解决办法
  • solr\core1\conf\schema.xml 文件中设置filed 为title字段的multiValued=“true”
  • 更改Javabean 的title 为ArrayList 类型;

这里采用第二种方式,更改Javabean

》在路径 :G:\tools\solr\core1\conf\schema.xml找到配置文件
可以看到 field 属性配置如下:
sdf

可以看到 filed 为title字段的multiValued=“true” ;
所以需要更改 multiValued=“true” 为multiValued=“false”

2.1、Javabean 以及测试 testQueryToJavabean如下:
  • Item 类
  • testQueryToJavabean单元测试

public class Item implements Serializable {
    private static final long serialVersionUID=1L;
    @Field("id")
    private String id;
    @Field("title")
    private ArrayList title;
    @Field("price")
    private long  price;
//  提供  setter 、getter方法  重写toString
}
在这里插入代码片
    @Test
    public void testQueryToJavabean() throws IOException, SolrServerException {
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
        /**
         * 普通查询
         */
//        SolrQuery query = new SolrQuery("title:iphone");
        /**
         * boolean  查询
         */
//        SolrQuery query = new SolrQuery("title:iphone OR title:小米");
        /**
         *  相似度查询 ,类似于Lucene的编辑距离0-2
         */
//        SolrQuery query = new SolrQuery("title:ipHOne~2");
        /**
         * 范围查询  xx  TO  xx
         * 闭区间
         */
        SolrQuery query = new SolrQuery("price:[10 TO 10000]");

        //
        QueryResponse response = server.query(query);
        List<Item> itemList = response.getBeans(Item.class);
        for (Item item : itemList) {
            System.out.println(item);
        }
        System.out.println("搜多到的条数:" + itemList.size());
    }
2.1再测,

尼玛,有提示float 类型非法,打开schema.xml 中查找price 的相关配置:
时代光华
》发现price 配置是float ,行吧,更改Javabean以及插入core1索引库的price属性为float
如下
1、javabean更改

public class Item implements Serializable {
    private static final long serialVersionUID=1L;
    @Field("id")
    private String id;
    @Field("title")
    private String title;
    @Field("price")
    private Float  price;
    // 提供  setter 、getter方法  重写toString
}

2、更新core1索引库中price的属性为float 类型

    /**
     * pojo中属性没有注解@Field,导致solrJ并不知道哪个属性要对应到索引库中
     * @throws IOException
     * @throws SolrServerException
     */
    @Test
    public void testCreateIndexBean() throws IOException, SolrServerException {
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core1");
        //
        Item item = new Item();
        item.setId("9");
        item.setPrice(3000F);
        item.setTitle("小米手机");
        // server中添加bean
        server.addBean(item);
        server.commit();
    }

3、单元测试走一走,完美解决:
sdf
ok

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值