1. 程式人生 > >linux下安裝solr及使用

linux下安裝solr及使用

solr下載地址https://download.csdn.net/download/kxj19980524/10873569

tomcat下載地址https://download.csdn.net/download/kxj19980524/10853555

拖拽上傳功能https://blog.csdn.net/kxj19980524/article/details/85246778

下載好後上傳到linux下,然後解壓縮

然後在/usr/local下建立solr,並且複製一份tomcat到solr下

然後進入到dist下面找到solr的war包

把這個war包部署到tomcat的webapps下面

 然後啟動tomcat來解壓縮這個war包,進入到tomcat的bin目錄下,執行startup.sh

檢視日誌,說明啟動成功了,就解壓完畢了,然後關閉它.

然後關閉tomcat,並且刪除solr.war,關閉tomcat執行bin下面的shutdown.sh

然後進入到這個目錄下,把這個目錄下所有jar包複製到tomcat的solr的lib下面去

然後退到example目錄下.複製solr到那個目錄下,起個名為solrhome

 

然後編輯這個目錄下的web.xml檔案

先把原來的註釋去掉,然後把value改為solrhome的路徑 
 然後儲存,啟動solr下的tomcat,關閉防火牆,訪問路徑就可以了

然後配置IK分詞器,下載地址https://download.csdn.net/download/kxj19980524/10867301,這個東西的壓縮包不是linux版的,在window裡面解壓縮成資料夾,然後使用我上面拖拽功能的第二種上傳到linux上

上傳上去後進入到目錄中,把jar包複製到這個目錄下

然後在這個目錄下建立classes資料夾

然後把這三個配置檔案複製到classes資料夾下

然後進入到核1的conf裡面配置IK分詞器

進入到schema.xml裡在最下面配置IK,和業務域,業務域的話,根據自己專案中的表做適當修改,不能直接抄,配置好後退出,重啟就好了.

<fieldType name="text_ik" class="solr.TextField">

  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>

</fieldType>

<!--業務域-->

<field name="item_title" type="text_ik" indexed="true" stored="true"/>

<field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>

<field name="item_price"  type="long" indexed="true" stored="true"/>

<field name="item_image" type="string" indexed="false" stored="true" />

<field name="item_category_name" type="string" indexed="true" stored="true" />

<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>

<copyField source="item_title" dest="item_keywords"/>

<copyField source="item_sell_point" dest="item_keywords"/>

<copyField source="item_category_name" dest="item_keywords"/>

這下面就是使用程式碼來建立索引庫,從資料庫表中獲取資訊,把相應的資訊建立成索引,自己稍加修改就可以了 

package cn.e3mall.search.service.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.e3mall.common.pojo.SearchItem;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.search.mapper.ItemMapper;
import cn.e3mall.search.service.SearchItemService;

/**
 * 索引庫維護Service
 * <p>Title: SearchItemServiceImpl</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Service
public class SearchItemServiceImpl implements SearchItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private SolrServer solrServer;
    
    @Override
    public E3Result importAllItems() {
        try {
            //查詢商品列表
            List<SearchItem> itemList = itemMapper.getItemList();
            //遍歷商品列表
            for (SearchItem searchItem : itemList) {
                //建立文件物件
                SolrInputDocument document = new SolrInputDocument();
                //向文件物件中新增域
                document.addField("id", searchItem.getId());
                document.addField("item_title", searchItem.getTitle());
                document.addField("item_sell_point", searchItem.getSell_point());
                document.addField("item_price", searchItem.getPrice());
                document.addField("item_image", searchItem.getImage());
                document.addField("item_category_name", searchItem.getCategory_name());
                //把文件物件寫入索引庫
                solrServer.add(document);
            }
            //提交
            solrServer.commit();
            //返回匯入成功
            return E3Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return E3Result.build(500, "資料匯入時發生異常");
                    
        }
    }

}

然後在spring配置檔案中注入一個solrserver物件,因為最好是讓spring管理,並且是單例的節省效能,所以注入一下 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg index="0" value="http://192.168.25.163:8080/solr/collection1"/>
    </bean>
    
</beans>

不明白的話看window版的安裝,都一樣https://blog.csdn.net/kxj19980524/article/details/85202324