1. 程式人生 > >電商專案day10(Solr入門&spring Data Solr整合到專案)

電商專案day10(Solr入門&spring Data Solr整合到專案)

今日目標:

1.solr的安裝與配置

2.solr的核心配置檔案

3.配置業務域

4.spring Date Solr整合到專案

5.批量資料匯入

一、Solr的介紹

1.solr是什麼?

       大多數搜尋引擎應用都必須具有某種搜尋功能,問題是搜尋功能往往是巨大的資源消耗並且它們由於沉重的資料庫載入而拖垮你的應用的效能。
       這就是為什麼轉移負載到一個外部的搜尋伺服器是一個不錯的主意,Apache Solr 是一個流行的開源搜尋伺服器,它通過使用類似REST 的 HTTP API,這就確保你能從幾乎任何程式語言來使用 solr。
       Solr 是一個開源搜尋平臺,用於構建搜尋應用程式。 它建立在 Lucene(全文搜尋引擎)之上。 Solr 是企業級的,快速的和高度可擴充套件的。 使用 Solr 構建的應用程式非常複雜,可提供高效能。
        為了在CNET網路的公司網站上新增搜尋功能,Yonik Seely 於 2004 年建立了 Solr。並在2006 年 1 月,它成為 Apache軟體基金會下的一個開源專案。並於 2016 年釋出最新版本 Solr6.0,支援並行 SQL查詢的執行。Solr 可以和Hadoop 一起使用。由於 Hadoop 處理大量資料,Solr 幫助我們從這麼大的源中找到所需的資訊。不僅限於搜尋,Solr 也可以用於儲存目的。像其他 NoSQL 資料庫一樣,
它是一種非關係資料儲存和處理技術。
         總之,Solr 是一個可擴充套件的,可部署,搜尋/儲存引擎,優化搜尋大量以文字為中心的
資料。

2.安裝和部署

      1.首先解壓solr的檔案

2.solr是war工程所以直接找到war工程解壓到tomcat中即可

我們檢視tomcat的日誌檔案可以看到如下錯誤資訊

十二月 16, 2018 3:52:48 下午 org.apache.catalina.core.StandardContext filterStart
嚴重: Exception starting filter SolrRequestFilter
java.lang.NoClassDefFoundError: Failed to initialize Apache Solr: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j

logging jars need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more information, see: http://wiki.apache.org/solr/SolrLogging
    at org.apache.solr.servlet.CheckLoggingConfiguration.check(CheckLoggingConfiguration.java:28)

錯誤原因是:缺少相關的jar包,我們根據提示拷貝相關的jar到solr專案的lib包下

3.建立本地索引庫

我們修改名字為solrhome   見名之意  

 

4.接下來,我們還要配置,讓solr知道我們建立的solrhome的位置,在這另一個方法是,修改tomcat的配置檔案,不建議用。

重新啟動tomcat如圖

檢視日誌,有警告

5.我們處理一下上面的警告,雖然不影響使用,有點強迫症,理解萬歲

6.修改配置檔案

該目錄下

警告已經解決

8.核心schema.xml的解析

中文分詞:Lucene 預設逐字分詞。
        solr預設也是逐字分詞,需要整合中文分詞器 ik分詞器。
        
    solr域欄位配置:
    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
    field:配置域欄位的標籤
    name:欄位名稱
    type:欄位型別
    indexed:是否索引。含義:是否可以根據該欄位進行搜尋操作。true可以根據該欄位搜尋,false,不能基於該欄位進去全文檢索。
    stored:是否儲存。含義:該欄位內容是否儲存在索引庫中。  
            業務:是否儲存,基於頁面是否需要展示決定的。需要展示時,儲存。
            
    solr域欄位型別配置:        
    <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
    fieldType:配置域欄位型別的標籤
    class:域欄位型別對應的solr類
    sortMissingLast:是否可以根據該欄位型別排序
    
    
    
     <dynamicField name="*_i"  type="int"    indexed="true"  stored="true"/>
     動態域:
        作用:簡化配置域欄位。
        
         <field name="item_i"  type="int"    indexed="true"  stored="true"/>
         <field name="item1_i"  type="int"    indexed="true"  stored="true"/>
         <field name="item2_i"  type="int"    indexed="true"  stored="true"/>
         <field name="item3_i"  type="int"    indexed="true"  stored="true"/>
         <field name="item4_i"  type="int"    indexed="true"  stored="true"/>
         ...
         
    <copyField source="cat" dest="text"/>
    複製域解決多欄位搜尋功能。例如:商品關鍵字搜尋
    <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
    
        <copyField source="item_title" dest="item_keywords"/>
        <copyField source="item_category" dest="item_keywords"/>
        <copyField source="item_seller" dest="item_keywords"/>
        <copyField source="item_brand" dest="item_keywords"/>
        
    source:源域
    dest:目標域

二、Spring Data  Solr入門

詳情請見day10-1    springData solr入門

三、批量匯入資料

solr整合IK分詞器  第三方分詞器
        中文分詞:
            優勢:動態新增擴充套件詞:高富帥、白富美
                動態停用停用詞:的、得、地
        
        1、匯入ik的jar包到solr工程中
        2、匯入ik配置檔案到solr工程的classes類路徑下
        3、 <!--  配置業務域欄位型別,使用ik分詞器 -->
            <fieldType name="text_ik" class="solr.TextField">
                <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
            </fieldType>
    1.業務相關的域欄位

2.首先編寫一個工具類獲取資料的資料

@Component
public class SolrUtil {

    @Autowired
    private TbItemMapper itemMapper;

    @Autowired
    private SolrTemplate solrTemplate;


    public void dataImport(){
        //從資料庫中查詢滿足條件的的商品列表資料 注意查詢的是符合條件的:  上架的   狀態為 1的
      List<TbItem> itemList = itemMapper.findAllGrounding();
        for (TbItem item : itemList) {
            String spec = item.getSpec();
             //組裝動態域屬性
            Map<String,String> map = JSON.parseObject(spec, Map.class);
            item.setSpecMap(map);
        }
        //2.將查詢的結果匯入到索引庫
        solrTemplate.saveBeans(itemList);
        solrTemplate.commit();
        System.out.println("itemList import finished.....");
    }

}

3.在mapper.xml的對映檔案中新增

 <select id="findAllGrounding" resultMap="BaseResultMap">
  	SELECT item.* FROM tb_item item,tb_goods goods WHERE item.goods_id=goods.id AND item.status='1' AND goods.is_marketable='1'
  </select>

4.在Tbitem中配置動態域

滿足條件的商品資料匯入到索引庫
    
    業務:什麼商品匯入索引庫?
        1、上架商品匯入索引庫  tb_goods  is_marketable='1'
        2、商品狀態為1,正常狀態 tb_item   status='1'

/*
配置規格動態域
 */
@Dynamic
@Field("item_spec_*")
private Map<String,String> specMap;

5.編寫測試方法匯入資料

/**
 * 匯入商品資料
 */
@Test
public void dataImport(){
    solrUtil.dataImport();
}

四、今日總結

域和複製域 和 動態域