当前位置: 移动技术网 > IT编程>开发语言>Java > SSM+solr 通过商品搜索学习solr的简单使用

SSM+solr 通过商品搜索学习solr的简单使用

2018年12月12日  | 移动技术网IT编程  | 我要评论

 

学习了一下https://github.com/tycoding/ssm-redis-solr这个github上的solr搜索功能,现在来记录一下。

我的理解就是solr有点类似于数据库,但它是有索引的数据库,按很多字段建立索引,可能是b+树或者散列索引,然后就能够实现海量数据的查找。solr通过导入jar包就可以对这个库就行增删改查了,后端逃不掉的增删改查。。。

 1.配置tomcat

具体我就不说了,因为我是直接用了github上配置好的,毕竟站在巨人的肩膀上学习嘛

地址:https://github.com/tycoding/solr-tomcat

2.访问solr并使用

访问端口:localhost:8080/solr/

这里的new_core就是项目中配置的路径,就将商品的索引放在这里。

然后用test测试它的使用,测试的时候要引入配置文件,不然会导致空指针错误,我居然现在才知道。怪不得以前只要用autowired的时候就会空指针错误。。,而且还要@runwith注解,引入包import org.springframework.test.context.junit4.*;eclipse点击不会有import提示,需要自己加上去。

 

 这里新建了一个实体对象,然后把这个实体对象加入到索引库里,在solr索引库里面就可以找到这个字段

在new_core的schema里面就以id建好了索引

以及很多的信息

@test
    public void testfindbyid() {
        goods goods = solrtemplate.getbyid(1, goods.class);
        system.out.println("--------" + goods.gettitle());
    }

通过id查找,控制台会输出你刚刚插入的数据,也就是通过solrtemplate找到了你的数据。

@test
    public void testaddlist() {
        list<goods> list = new arraylist<goods>();
        //循环插入100条数据
        for (int i = 0; i < 100; i++) {
            bigdecimal price=new bigdecimal (2.3);
            goods goods = new goods(i + 1l, "华为mate" + i,price, "手机", "手机", "华为专卖店");
            list.add(goods);
        }
        solrtemplate.savebeans(list); //添加集合对象,调用savebeans();添加普通对象类型数据,使用savebean();
        solrtemplate.commit(); //提交
    }

还可以批量插入数据,或者分页查询

@test
    public void testpagequery() {
        query query = new simplequery("*:*");
        query.setoffset(20); //开始索引(默认0)
        query.setrows(20); //每页记录数(默认10)
        scoredpage<goods> page = solrtemplate.queryforpage(query, goods.class);
        system.out.println("总记录数:" + page.gettotalelements());
        list<goods> list = page.getcontent();
    }

3.学习一下项目中怎么配置

注意要在web.xml加一个过滤,不然注入不了solrtemplate这个bean

 

spring-solr.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
       xmlns:solr="http://www.springframework.org/schema/data/solr"
       xsi:schemalocation="http://www.springframework.org/schema/data/solr
          http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- solr服务器地址 -->
    <solr:solr-server id="solrserver" url="http://127.0.0.1:8080/solr/new_core"/>

    <!-- solr模板,使用solr模板可对索引库进行crud的操作 -->
    <bean id="solrtemplate" class="org.springframework.data.solr.core.solrtemplate">
        <constructor-arg ref="solrserver"/>
    </bean>

</beans>

就是加载一个solr的模板

 

solrutil.java

把数据库的数据库批量加入

@component
public class solrutil {

    @autowired
    private goodsmapper goodsmapper;

    @autowired
    private solrtemplate solrtemplate;

    /**
     * 实现将数据库中的数据批量导入到solr索引库中
     */
    public void importgoodsdata() {

        list<goods> list = goodsmapper.findall();
        system.out.println("====商品列表====");
        for (goods goods : list) {
            system.out.println(goods.gettitle());
        }

        solrtemplate.savebeans(list);
        solrtemplate.commit(); //提交
        system.out.println("====结束====");
    }

    public static void main(string[] args) {
        applicationcontext context = new classpathxmlapplicationcontext("classpath:spring/spring*.xml");
        solrutil solrutil = (solrutil) context.getbean("solrutil");
        solrutil.importgoodsdata();
    }
}

 

 这样就把数据加入索引库中。

实体类有一个field标识这个实体字段在索引库里的名称

 @field
    private long id; //商品id
    @field("item_title")
    private string title; //商品标题
    @field("item_price")
    private bigdecimal price; //商品价格
    @field("item_image")
    private string image; //商品图片
    @field("item_category")
    private string category; //商品类别
    @field("item_brand")
    private string brand; //商品品牌
    @field("item_seller")
    private string seller; //商品卖家

最后,搜索功能的实现

按价格查找

 //按价格区间查询
     if (searchmap.get("price") != null) {
         if (!searchmap.get("price").equals("")) {
             string[] price = ((string) searchmap.get("price")).split("-");
             if (!price[0].equals("0")) {
                 //如果起点区间不等于0
                 criteria filtercriteria = new criteria("item_price").greaterthanequal(price[0]);
                 filterquery filterquery = new simplefilterquery(filtercriteria);
                 query.addfilterquery(filterquery);
             }

             if (!price[1].equals("*")) {
                 //如果区间重点不等于*
                 criteria filtercriteria = new criteria("item_price").lessthanequal(price[1]);
                 filterquery filterquery = new simplefilterquery(filtercriteria);
                 query.addfilterquery(filterquery);
             }
         }
     }

 4.实现效果

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网