1. 程式人生 > >SSM+solr 通過商品搜尋學習solr的簡單使用

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