SSM+solr 經過商品搜索學習solr的簡單使用

 

學習了一下https://github.com/TyCoding/ssm-redis-solr這個github上的solr搜索功能,如今來記錄一下。html

個人理解就是solr有點相似於數據庫,但它是有索引的數據庫,按不少字段創建索引,多是b+樹或者散列索引,而後就可以實現海量數據的查找。solr經過導入jar包就能夠對這個庫就行增刪改查了,後端逃不掉的增刪改查。。。java

 1.配置tomcat

具體我就不說了,由於我是直接用了github上配置好的,畢竟站在巨人的肩膀上學習嘛git

地址:https://github.com/TyCoding/solr-tomcatgithub

2.訪問solr並使用

訪問端口:localhost:8080/solr/index.htmlweb

這裏的new_core就是項目中配置的路徑,就將商品的索引放在這裏。redis

而後用Test測試它的使用,測試的時候要引入配置文件,否則會致使空指針錯誤,我竟然如今才知道。怪不得之前只要用Autowired的時候就會空指針錯誤。。,並且還要@Runwith註解,引入包import org.springframework.test.context.junit4.*;eclipse點擊不會有import提示,須要本身加上去。spring

 

 這裏新建了一個實體對象,而後把這個實體對象加入到索引庫裏,在solr索引庫裏面就能夠找到這個字段數據庫

在new_core的schema裏面就以Id建好了索引後端

以及不少的信息tomcat

@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.實現效果

相關文章
相關標籤/搜索