zoukankan      html  css  js  c++  java
  • SSM+solr 通过商品搜索学习solr的简单使用

    学习了一下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/index.html

    这里的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.实现效果

  • 相关阅读:
    python学习(四)流程控制语句
    python学习(三)运算符
    python学习(二)数据类型转换
    python学习(一)基本数据类型
    mysql学习-mysql分页查询
    装饰器
    迭代器与生成器
    深入解析类对象与类实例的创建过程
    深入理解Python中的元类---metaclass
    flask源码解析之DispatcherMiddleware
  • 原文地址:https://www.cnblogs.com/HannahLihui/p/10104416.html
Copyright © 2011-2022 走看看