手把手教你如何玩轉Solr(包含項目實戰)

大家可以關注我的微信公衆號:Java菜鳥進階之路

一:Solr簡介
       Solr是一個獨立的企業級搜索應用服務器,它對外提供類似於Web-service的API接口。用戶可以通過http請求,向搜索引擎服務器提交一定格式的XML文件,生成索引;也可以通過Http Get操作提出查找請求,並得到XML格式的返回結果。

     Solr是一個高性能,採用Java5開發,

Solr

基於Lucene的全文搜索服務器。同時對其進行了擴展,提供了比Lucene更爲豐富的查詢語言,同時實現了可配置、可擴展並對查詢性能進行了優化,並且提供了一個完善的功能管理界面,是一款非常優秀的全文搜索引擎

二:Solr服務器的搭建
步驟:

(1)在磁盤中創建一個文件目錄,即認爲該文件爲Solr服務器的相關屬性。

(2)在上述的文件目錄中,複製一個Tomcat服務器(最好是純淨的,即webapps目錄下不包含多餘的項目)

(3)從lucene.apache.org官網或者其他資源網下載Solr資源(由於Solr目前更新都很頻繁,差不多是兩個月就有新版本,而且對於Solr4和Solr5之間存在着不同,所以這個根據需要下載版本吧。我用的是Solr4版本中的4.10.3版本)

(4)將下載的solr中,進入下圖的目錄,然後將war包拷貝到Tomcat中的webapps下面

(5)解壓拷貝過去的war包,並將解壓完成之後,將之前的war包進行刪除,原因就是我們需要修改解壓的內容,否則不刪除,在部署之後又會覆蓋,所以要進行刪除處理。

(6)從下載好的solr中的下面的目錄,將jar包拷貝到Tomcat下的solr中的lib目錄

(7)創建一個Solr服務器的核心家文件夾,並將其與tomcat保持同級。

(8)修改tomcat目錄webapps下的solr的web.xml文件內容,修改solr核心家的內容

(9)運行tomcat

(10)運行成功後,訪問鏈接http://localhost:8080/solr     即可進入到solr服務器的首頁。

三:Solr服務器配置中文分詞器
步驟:

(1)拷貝中文分詞器IK分詞包到Solr服務中的lib目錄中

(2)在Solr中的WEB-INF下面創建classes文件目錄,用於存放中文分詞器的分詞配置

ext.dic的內容,比如如下:

高富帥
黑馬程序員
二維表
這樣的話,碰到這樣的詞就不會進行拆分了,所以,一些網絡新詞就可以在這裏進行配置。

IKAnalyer.cfg.xml文件 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
    <comment>IK Analyzer 擴展配置</comment>
    <!--用戶可以在這裏配置自己的擴展字典 -->
    <entry key="ext_dict">ext.dic;</entry> 
    
    <!--用戶可以在這裏配置自己的擴展停止詞字典-->
    <entry key="ext_stopwords">stopword.dic;</entry> 
    
</properties>
stopwords.dic內容:




a
an
and
are
as
 
這樣的話,對於上面的字就不會進行顯示處理了,因爲這些都是沒有意義的詞彙。

(3)在覈心solr家中,找到配置文件schema.xml,添加中文分詞器的配置。

添加如下內容:

<!--配置中文分詞器-->
    <fieldType name="text_ik" class="solr.TextField">
        <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>
    <!--配置幾個域,這幾個就支持中文分詞了,當然自己根據需求進行創建即可-->
    <field name="title_ik" type="text_ik" indexed="true" stored="true"/>
    <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/
(4)重啓tomcat,進行測試是否配置完成。

(5)訪問Solr主頁,進行測試

四:配置solr服務器導入數據庫數據
步驟:

(1)導包

首先是需要在solr的核心庫中添加一些導入數據的包,需要如下:

注意:因爲在collection中,最初是沒有lib這個文件目錄的,所以需要自己創建一個lib目錄,然後把相應的jar包添加進去。

(2)在collection中的config目錄下的solrconfig.xml中添加數據導入處理器

<requestHandler name="/dataimport" 
   class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
        <str name="config">data-config.xml</str>
    </lst>
  </requestHandler>
(3)在collection中的config目錄添加一個data-config.xml(這個與上面一步配置的名字要相同)

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
            driver="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/需要導入數據的數據庫名"
            user="數據庫賬號"
            password="數據庫密碼"/>
<document>
    <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products">
    <field column="pid" name="id" />
    <field column="name" name="product_name" />
    <field column="catalog_name" name="product_catalog_name" />
    <field column="price" name="product_price" />
    <field column="description" name="product_description" />
    <field column="picture" name="product_picture" />
    </entity>
</document>
</dataConfig>
備註:上面的內容,其實一看就大體明白了什麼意思,主要就是配置要導入哪個數據庫,導入的字段有什麼,如果不配置的話,那麼solr服務器是無法判斷得到的字段的,所以需要進行配置,相當於一個映射配置。

注意:我上面的entity裏面的內容就是我需要導入數據的字段的些內容,所以,根據需求進行自行匹配。

(4)在collection中config目錄下的schema.xml添加如下內容:(這個是爲了能夠便於對導入數據庫中的數據,與solr中的域進行匹配,因爲我們都知道,如果solr域中不存在相應的域,那麼是無法進行查詢修改刪除操作的,那麼就不利於我們在以後的項目中對數據庫相應字段的處理,所以,這一步是可有可無,但是配置了就有很多的好處)

<!--配置從數據庫導入到sorl中的數據的字段內容,所以每次要從數據庫導入什麼就需要配置什麼-->
    <field name="product_name" type="text_ik" indexed="true" stored="true"/>
    <field name="product_price" type="float" indexed="true" stored="true"/>
    <field name="product_description" type="text_ik" indexed="true" stored="false"/>
    <field name="product_picture" type="string" indexed="false" stored="true"/>
    <field name="product_catalog_name" type="string" indexed="true" stored="true"/>
    <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true" />
    <copyField source="product_name" dest="product_keywords" />
    <copyField source="product_description" dest="product_keywords" />
(5)重啓tomcat,然後登陸solr服務

(6)進行添加數據的處理

(7)點擊上面圖中的Execute按鈕即可進行數據的導入了,然後再進去Query就可查詢到導入的數據了哦!!

五:SolrJ進行數據的增刪改查的處理
package com.hnu.scw.solr;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
/**
 * solrj的相關開發
 * @author scw
 *2018-02-26
 */
public class SolrManager {
    /**
     * 添加文檔數據到solr服務器中
     * @throws Exception
     */
    @Test
    public void addContent() throws Exception{
        //設置solr服務器的路徑,默認是使用第一個collection庫,所以路徑最後可以不加collection1
        String baseURL = "http://localhost:8080/solr";
        //如果要使用第二個collection庫,那麼就使用下面的鏈接
        //String baseURL2 = "http://localhost:8080/solr/collection2";        
        //創建服務器連接對象
        HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL);
        //創建新的文檔對象
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        //設置文檔的域
        solrInputDocument.setField("id", "haha222");
        solrInputDocument.setField("name", "佘超偉123");
        //進行添加
        httpSolrServer.add(solrInputDocument);
        //進行手動提交,否則無法進行添加
        httpSolrServer.commit();
    }
    /**
     * 進行刪除文檔操作
     * @throws SolrServerException
     * @throws IOException
     */
    @Test
    public void deleteContent() throws Exception{
        String baseURL = "http://localhost:8080/solr";
        SolrServer httpSolrServer = new HttpSolrServer(baseURL);
        //刪除全部,第一個參數是設置需要刪除的數據的域和值,第二個是執行後多久進行刪除操作
        //httpSolrServer.deleteByQuery("*:*",1000);
        //刪除某個特定域的特定值的數據
        httpSolrServer.deleteByQuery("id:haha",1000);
    }
    
    /**
     * 修改文檔內容
     * 修改其實和添加是一樣的,因爲只要添加的ID是一樣的,那麼就會把原來的刪除了,然後再添加一個
     * @throws IOException 
     * @throws SolrServerException 
     */
    @Test
    public void updateContent() throws SolrServerException, IOException{
        String baseURL = "http://localhost:8080/solr";
        SolrServer httpSolrServer = new HttpSolrServer(baseURL);
        //創建新的文檔對象
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        //設置文檔的域
        solrInputDocument.setField("id", "haha123");
        solrInputDocument.setField("name", "哈哈123");
        httpSolrServer.add(solrInputDocument);
    }
    
    /**
     * 查詢數據(多功能的顯示處理)
     * @throws Exception 
     */
    @Test
    public void queryContent() throws Exception{
        String baseURL = "http://localhost:8080/solr";
        SolrServer httpSolrServer = new HttpSolrServer(baseURL);
        //創建查詢數據對象(便於設置查詢條件)
        SolrQuery solrQuery = new SolrQuery();
        //設置查詢的域和值,這個在之後的項目中可以用於動態
        //方法一:參數q就代表query查詢
        //solrQuery.set("q","name:佘超偉123");
        //方法二:(一般使用該方法)
        solrQuery.setQuery("name:佘超偉");
        //方法三:通過設置默認域
        //solrQuery.set("df", "name");
        //solrQuery.setQuery("佘超偉");
        
        //設置查詢過濾條件(可以設置多個,只要域和值有改變就可以了)
        //solrQuery.set("fq", "id:haha123");
        //添加排序方式(可選內容)
        //solrQuery.addSort("需要排序的域",ORDER.asc);//升序
        //solrQuery.addSort("需要排序的域",ORDER.desc);//降序
        //設置分頁處理(比如這是設置每次顯示5個)
        solrQuery.setStart(0);
        solrQuery.setRows(5);
        //設置只查詢顯示指定的域和值(第二個參數可以是多個,之間用“逗號”分割)
        //solrQuery.set("fl", "name");
        //設置某域進行高亮顯示
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("name");
        //設置高亮顯示格式的前後綴
        solrQuery.setHighlightSimplePre("<span style='color:red'>");
        solrQuery.setHighlightSimplePost("</span");    
        
        //執行查詢,獲得查詢結果對象
        QueryResponse query = httpSolrServer.query(solrQuery);
        //獲取查詢的結果集
        SolrDocumentList results = query.getResults();
        //獲取高亮顯示的查詢結果
        //注意點:因爲高亮的結果和正常的查詢結果是不一樣的,所以要進行特別的處理
        Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
        //遍歷結果集
        for (SolrDocument solrDocument : results) {
            String idStr = (String) solrDocument.get("id");
            System.out.println("id----------------" + idStr);
            String nameStr = (String) solrDocument.get("name");
            System.out.println("name----------------" + nameStr);
            System.out.println("===========高亮顯示=====================");
            Map<String, List<String>> map = highlighting.get(idStr);
            List<String> list = map.get("name");
            String resultString = list.get(0);
            System.out.println("高亮結果爲:-----" + resultString);
        }        
    }
}
六:Solr服務器中的後臺數據的增刪改查處理
這個其實就類似SolrJ的代碼,代碼看懂的話,這個後臺操作是一樣的,而且不需要進行代碼處理,相對更加方便,但是對於實際的項目開發中,這樣肯定是不好的,所以還是需要通過代碼進行編寫更爲有效。

七:京東商城的站內搜索項目實踐
效果圖:


步驟:

(1)環境搭建-------SpringMVC+Solr+mysql

1:導包------Springmvc+Solr+數據庫驅動的jar包

2:編寫web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JingDongSolr</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!--配置post提交防止亂碼 -->
  <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
  <!--配置springmvc前端控制器 -->
  <servlet>
      <servlet-name>JingDongSolr</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
          <servlet-name>JingDongSolr</servlet-name>
          <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
3:編寫springmvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
        <!-- 掃描基本包  @Controller  @Service @Respostory -->
        <context:component-scan base-package="com.hnu.scw"/>
        <!-- 三大組件 -->
        <mvc:annotation-driven/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        <!-- 對靜態資源文件的訪問  restful-->     
         <mvc:resources mapping="/images/**" location="/images/" />
        <mvc:resources mapping="/resource/**" location="/resource/" />
        
        <!-- 配置SolrJ -->
        <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
            <constructor-arg value="http://localhost:8080/solr/collection1"/>
        </bean>
 </beans>
4:編寫jsp,css,js和添加需要的圖片資源

product_list.jsp-------------顯示商品信息,即主頁

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<!-- saved from url=(0047)http://list.jd.com/list.html?cat=1315,1343,1355 -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="utf-8" http-equiv="charset">
<link rel="stylesheet" type="text/css"
    href="<c:url value='/resource'/>/base.css" media="all">
<link rel="stylesheet" type="text/css"
    href="<c:url value='/resource'/>/plist20131112.css" media="all">
<link rel="stylesheet" type="text/css"
    href="<c:url value='/resource'/>/list-page-20141009.css" media="all">
<link rel="stylesheet" type="text/css"
    href="<c:url value='/resource'/>/pop_compare.css" media="all">
<link rel="shortcut icon" type="image/ico"
    href="http://list.jd.com/favicon.ico">
<script type="text/javascript"
    src="<c:url value='/resource'/>/jquery-1.2.6.pack.js"></script>
<style id="style-1-cropbar-clipper">/* Copyright 2014 Evernote Corporation. All rights reserved. */
.en-markup-crop-options {
    top: 18px !important;
    left: 50% !important;
    margin-left: -100px !important;
    width: 200px !important;
    border: 2px rgba(255,255,255,.38) solid !important;
    border-radius: 4px !important;
}
 
.en-markup-crop-options div div:first-of-type {
    margin-left: 0px !important;
}
</style>
<script type="text/javascript">
    function query() {
        //執行關鍵詞查詢時清空過濾條件
        document.getElementById("catalog_name").value="";
        document.getElementById("price").value="";
        //執行查詢
        queryList();
    }
    function queryList() {
        //提交表單
        document.getElementById("actionForm").submit();
    }
    function filter(key, value) {
        document.getElementById(key).value=value;
        //執行查詢
        queryList();
    }
    function sort() {
        var s = document.getElementById("sort").value; 
        if (s != "1") {
            s = "1";
        } else {
            s = "0";
        }
        document.getElementById("sort").value = s;
        //執行查詢
        queryList();
    }
</script>
</head>
<body class="root61">
<div id="shortcut-2013">
    <div class="w">
        <ul class="fl lh">
            <li class="fore1 ld"><b></b><a href="#" rel="nofollow">收藏京東</a></li>
        </ul>
        <ul class="fr lh">
            <li class="fore1" id="loginbar">您好,歡迎來到京東!<span><a href="#">[登錄]</a> <a href="#" class="link-regist">[免費註冊]</a></span></li>
            <li class="fore2 ld">
                <s></s>
                <a href="#" rel="nofollow">我的訂單</a>
            </li>
            <li class="fore2-1 ld" id="jd-vip"><i></i>
                <i></i>
                <s></s>
                <a target="_blank" rel="nofollow" href="http://vip.jd.com/">會員俱樂部</a>
            </li>
            <li class="fore2-2 ld" id="jd-dakehu">        <i></i><s></s>        <a href="http://b.jd.com/" target="_blank" rel="nofollow">企業頻道</a>    </li>
            <li class="fore3 ld menu" id="app-jd" data-widget="dropdown" clstag="homepage|keycount|home2013|01d"><s></s>
                <i></i>
                <span class="outline"></span>
                <span class="blank"></span>
                <a href="http://app.jd.com/" target="_blank">手機京東</a>
                <b></b>
            </li>
            <li class="fore4 ld menu" id="biz-service" data-widget="dropdown">
                <s></s>
                <span class="outline"></span>
                <span class="blank"></span>
                客戶服務
                <b></b>
            </li>
            <li class="fore5 ld menu" id="site-nav" data-widget="dropdown">
                <s></s>
                <span class="outline"></span>
                <span class="blank"></span>
                網站導航
                <b></b>
            </li>
        </ul>
        <span class="clr"></span>
    </div>
</div><!--shortcut end-->
<div id="o-header-2013">
    <div class="w" id="header-2013">
        <div id="logo-2013" class="ld"><a href="http://www.jd.com/" hidefocus="true"><b></b><img src="<c:url value='/resource'/>/logo-201305.png" width="270" height="60" alt="京東"></a></div>
        <!--logo end-->
        <div id="search-2013">
            <div class="i-search ld">
                <ul id="shelper" class="hide"></ul>
                <form id="actionForm" action="${pageContext.request.contextPath}/list" method="POST">
                <div class="form">
                    <input type="text" class="text" accesskey="s" name="queryString" id="key" value="${queryString }"
                        autocomplete="off" onkeydown="javascript:if(event.keyCode==13) {query()}">
                    <input type="button" value="搜索" class="button" onclick="query()">
                </div>
                <input type="hidden" name="catalog_name" id="catalog_name" value="${catalog_name }"/> 
                <input type="hidden" name="price" id="price" value="${price }"/> 
                <input type="hidden" name="sort" id="sort" value="${sort }"/> 
                </form>
            </div>
            <div id="hotwords"></div>
        </div>
        <!--search end-->
        <div id="my360buy-2013">
            <dl>
                <dt class="ld"><s></s><a href="http://home.jd.com/">我的京東</a><b></b></dt>
                <dd>
                    <div class="loading-style1"><b></b>加載中,請稍候...</div>
                </dd>
            </dl>
        </div>
        <!--my360buy end-->
        <div id="settleup-2013">
            <dl>
                <dt class="ld"><s></s><span class="shopping"><span id="shopping-amount">0</span></span><a href="http://cart.jd.com/cart/cart.html" id="settleup-url">去購物車結算</a> <b></b> </dt>
                <dd>
                    <div class="prompt">
                        <div class="loading-style1"><b></b>加載中,請稍候...</div>
                    </div>
                </dd>
            </dl>
        </div>
        <!--settleup end-->
    </div>
    <!--header end-->
    <div class="w">
        <div id="nav-2013">
            <div id="categorys-2013" class="categorys-2014">
                <div class="mt ld">
                    <h2><a href="http://www.jd.com/allSort.aspx">全部商品分類<b></b></a></h2>
                </div>
            </div>
            <div id="treasure"></div>
            <ul id="navitems-2013">
                <li class="fore1" id="nav-home"><a href="http://www.jd.com/">首頁</a></li>
                <li class="fore2" id="nav-fashion"><a href="http://fashion.jd.com/">服裝城</a></li>
                <li class="fore3" id="nav-chaoshi"><a href="http://channel.jd.com/chaoshi.html">食品</a></li>
                <li class="fore4" id="nav-tuan"><a href="http://tuan.jd.com/" target="_blank">團購</a></li>
                <li class="fore5" id="nav-auction"><a href="http://auction.jd.com/">奪寶島</a></li>
                <li class="fore6" id="nav-shan"><a href="http://red.jd.com/">閃購</a></li>
                <li class="fore7" id="nav-jinrong"><a href="http://jr.jd.com/" target="_blank">金融</a></li>
            </ul>
        </div>
    </div>
</div>
<div class="w">
    <div class="breadcrumb">
        <strong><a href="#">服飾內衣</a></strong><span> > <a
            href="#">女裝</a> > <a href="#">T恤</a></span>
    </div>
</div>
<div class="w main">
<div class="right-extra">
<div id="select" clstag="thirdtype|keycount|thirdtype|select" class="m">
    <div class="mt">
        <h1>
            T恤 -<strong> 商品篩選</strong>
        </h1>
    </div>
    <div class="mc attrs">
        <div data-id="100001" class="brand-attr">
            <div class="attr">
                <div class="a-key">商品類別:</div>
                <div class="a-values">
                    <div class="v-tabs">
                        <div class="tabcon">
                            <div>
                                <a href="javascript:filter('catalog_name', '幽默雜貨')" >幽默雜貨</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '時尚衛浴')">時尚衛浴</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '另類文體')">另類文體</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '創意相架')">創意相架</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '巧妙收納')">巧妙收納</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '與鐘不同')">與鐘不同</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '個性男人')">個性男人</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '電腦周邊')">電腦周邊</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '品質家電')">品質家電</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '品味茶杯')">品味茶杯</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '四季用品')">四季用品</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '健康寶寶')">健康寶寶</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '新潮美容')">新潮美容</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '產品配件')">產品配件</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '雅緻燈飾')">雅緻燈飾</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '陽光車飾')">陽光車飾</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '趣味紙抽')">趣味紙抽</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '布藝毛絨')">布藝毛絨</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '益智手工')">益智手工</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '環保餐具')">環保餐具</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '閃亮匙扣')">閃亮匙扣</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '手機飾品')">手機飾品</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '精品數碼')">精品數碼</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '理財錢罐')">理財錢罐</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '美味廚房')">美味廚房</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '保健按摩')">保健按摩</a>
                            </div>                                              
                            <div>                                              
                                <a href="javascript:filter('catalog_name', '魅力女人')">魅力女人</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div data-id="100002" class="prop-attrs">
            <div class="attr">
                <div class="a-key">價格:</div>
                <div class="a-values">
                    <div class="v-fold">
                        <ul class="f-list">
                            <li><a href="javascript:filter('price','0-9')">0-9</a></li>
                            <li><a href="javascript:filter('price','10-19')">10-19</a></li>
                            <li><a href="javascript:filter('price','20-29')">20-29</a></li>
                            <li><a href="javascript:filter('price','30-39')">30-39</a></li>
                            <li><a href="javascript:filter('price','40-49')">40-49</a></li>
                            <li><a href="javascript:filter('price','50-*')">50以上</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="filter">
    <div class="cls"></div>
    <div class="fore1">
        <dl class="order">
            <dt>排序:</dt>
            <dd>
                <a href="javascript:sort()">價格</a><b></b>
            </dd>
        </dl>
        <dl class="activity">
            <dd></dd>
        </dl>
        <div class="pagin pagin-m">
            <span class="text"><i>1</i>/200</span>
            <a href="javascript:;" class="prev">上一頁<b></b></a>
            <a href="javascript:;" class="next">下一頁<b></b></a>
        </div>
        <div class="total">
            <span>共<strong>2000</strong>個商品
            </span>
        </div>
        <span class="clr"></span>
    </div>
</div>
<!--商品列表開始-->
<div id="plist" class="m plist-n7 plist-n8 prebuy">
    <ul class="list-h">
        <c:forEach var="item" items="${productModels }">
        <li pid="${item.pid }">
            <div class="lh-wrap">
                <div class="p-img">
                    <a target="_blank" href="#">
                        <img width="220" height="282" class="err-product" src="images/${item.picture}">
                    </a>
                </div>
                <div class="p-name">
                    <a target="_blank" href="#">${item.name }</a>
                </div>
                <div class="p-price">
                    <strong>¥<fmt:formatNumber value="${item.price}" maxFractionDigits="2"/></strong><span id="p1269191543"></span>
                </div>
            </div>
        </li>
        </c:forEach>
    </ul>
</div>
<!--商品列表結束-->
</div>
<div class="left">
    <div id="sortlist" clstag="thirdtype|keycount|thirdtype|sortlist"
        class="m">
        <div class="mt">
            <h2>服飾內衣</h2>
        </div>
        <div class="mc">
            <div class="item current">
                <h3>
                    <b></b>女裝
                </h3>
                <ul>
                    <li><a href="http://list.jd.com/1315-1343-1355.html">T恤</a></li>
                    <li><a href="http://list.jd.com/1315-1343-1354.html">襯衫</a></li>
                    <li><a href="http://list.jd.com/1315-1343-1356.html">針織衫</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9713.html">雪紡衫</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9710.html">衛衣</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9714.html">馬甲</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9719.html">連衣裙</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9720.html">半身裙</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9715.html">牛仔褲</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9717.html">休閒褲</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9716.html">打底褲</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9718.html">正裝褲</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9711.html">小西裝</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9712.html">短外套</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9708.html">風衣</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9706.html">毛呢大衣</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9707.html">真皮皮衣</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9705.html">棉服</a></li>
                    <li><a href="http://list.jd.com/1315-1343-3983.html">羽絨服</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9722.html">大碼女裝</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9721.html">中老年女裝</a></li>
                    <li><a href="http://list.jd.com/1315-1343-9723.html">婚紗</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11985.html">打底衫</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11986.html">旗袍/唐裝</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11987.html">加絨褲</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11988.html">吊帶/背心</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11989.html">羊絨衫</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11991.html">短褲</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11993.html">皮草</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11996.html">禮服</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11998.html">仿皮皮衣</a></li>
                    <li><a href="http://list.jd.com/1315-1343-11999.html">羊毛衫</a></li>
                    <li><a href="http://list.jd.com/1315-1343-12000.html">設計師/潮牌</a></li>
                </ul>
            </div>
            <div class="item">
                <h3>
                    <b></b>男裝
                </h3>
            </div>
            <div class="item">
                <h3>
                    <b></b>內衣
                </h3>
            </div>
            <div class="item">
                <h3>
                    <b></b>服飾配件
                </h3>
            </div>
        </div>
    </div>
    <div id="limitBuy">
        <div id="limitbuy9199"
            clstag="thirdtype|keycount|thirdtype|limitbuy536"
            class="m limitbuy hide">
            <div class="mt">
                <h2>服飾鞋帽</h2>
            </div>
            <div class="mc">
                <div id="clock9199" class="clock">正在加載…</div>
                <div id="limit9199"></div>
            </div>
        </div>
    </div>
    <div id="ad_left" reco_id="6" class="m m0 hide"></div>
    <!--用戶最終購買-->
    <div id="finalbuy" class="hide m m0" style="display: block;">
        <div class="mt">
            <h2>
                瀏覽<font color="red">T恤</font>最終購買
            </h2>
        </div>
        <div class="mc">
        </div>
    </div>
    <div id="weekRank" clstag="thirdtype|keycount|thirdtype|mrank"
        class="m rank">
        <div class="mt">
            <h2>一週銷量排行</h2>
        </div>
        <div class="mc">
        </div>
    </div>
</div><!--<div class="left">-->
 
<span class="clr"></span>
<div id="Collect_Tip" class="Tip360 w260"></div>
 
</div><!--<div class="w main">-->
 
 
<div class="w">
    <div id="service-2013">
        <dl class="fore1">
            <dt><b></b><strong>購物指南</strong></dt>
            <dd>
                <div><a href="http://help.jd.com/help/question-56.html" target="_blank" rel="nofollow">購物流程</a></div>
                <div><a href="http://help.jd.com/help/question-57.html" target="_blank" rel="nofollow">會員介紹</a></div>
                <div><a href="http://help.jd.com/help/question-181.html" target="_blank" rel="nofollow">團購/機票</a></div>
                <div><a href="http://help.jd.com/help/question-61.html" target="_blank" rel="nofollow">常見問題</a></div>
                <div><a href="http://help.jd.com/help/question-63.html" target="_blank" rel="nofollow">大家電</a></div>
                <div><a href="http://help.jd.com/index.html" target="_blank" rel="nofollow">聯繫客服</a></div>
            </dd>
        </dl>
        <dl class="fore2">
            <dt><b></b><strong>配送方式</strong></dt>
            <dd>
                <div><a href="http://help.jd.com/help/question-64.html" target="_blank" rel="nofollow">上門自提</a></div>
                <div><a href="http://help.jd.com/help/question-360.html" target="_blank" rel="nofollow">211限時達</a></div>
                <div><a href="http://help.jd.com/help/distribution-768.html" target="_blank" rel="nofollow">配送服務查詢</a></div>
                <div><a href="http://help.jd.com/help/question-892.html#help2215" target="_blank" rel="nofollow">配送費收取標準</a></div>
                
                <div><a href="http://en.jd.com/chinese.html" target="_blank">海外配送</a></div>
            </dd>
        </dl>
        <dl class="fore3">
            <dt><b></b><strong>支付方式</strong></dt>
            <dd>
                <div><a href="http://help.jd.com/help/question-67.html" target="_blank" rel="nofollow">貨到付款</a></div>
                <div><a href="http://help.jd.com/help/question-68.html" target="_blank" rel="nofollow">在線支付</a></div>
                <div><a href="http://help.jd.com/help/question-71.html" target="_blank" rel="nofollow">分期付款</a></div>
                <div><a href="http://help.jd.com/help/question-69.html" target="_blank" rel="nofollow">郵局匯款</a></div>
                <div><a href="http://help.jd.com/help/question-70.html" target="_blank" rel="nofollow">公司轉賬</a></div>
            </dd>
        </dl>
        <dl class="fore4">
            <dt><b></b><strong>售後服務</strong></dt>
            <dd>
                <div><a href="http://myjd.jd.com/afs/help/afshelp.action" target="_blank" rel="nofollow">售後政策</a></div>
                <div><a href="http://help.jd.com/help/question-99.html" target="_blank" rel="nofollow">價格保護</a></div>
                <div><a href="http://help.jd.com/help/question-100.html" target="_blank" rel="nofollow">退款說明</a></div>
                <div><a href="http://myjd.jd.com/repair/repairs.action" target="_blank" rel="nofollow">返修/退換貨</a></div>
                <div><a href="http://help.jd.com/help/question-881.html" target="_blank" rel="nofollow">取消訂單</a></div>
            </dd>
        </dl>
        <dl class="fore5">
            <dt><b></b><strong>特色服務</strong></dt>
            <dd>
                <div><a href="http://help.jd.com/help/question-79.html" target="_blank">奪寶島</a></div>
                <div><a href="http://help.jd.com/help/question-86.html" target="_blank">DIY裝機</a></div>
                <div><a href="http://fuwu.jd.com/" target="_blank" rel="nofollow">延保服務</a></div>
                <div><a href="http://giftcard.jd.com/market/index.action" target="_blank" rel="nofollow">京東E卡</a></div>
                <div><a href="http://help.jd.com/help/question-91.html" target="_blank" rel="nofollow">節能補貼</a></div>
                <div><a href="http://mobile.jd.com/" target="_blank" rel="nofollow">京東通信</a></div>
            </dd>
        </dl>
        <span class="clr"></span>
    </div>
</div><!-- service end --><div class="w">
    <div id="footer-2013">
        <div class="links">
            <a rel="nofollow" target="_blank" href="http://www.jd.com/intro/about.aspx">關於我們</a>|<a rel="nofollow" target="_blank" href="http://www.jd.com/contact/">聯繫我們</a>|<a rel="nofollow" target="_blank" href="http://zhaopin.jd.com/">人才招聘</a>|<a rel="nofollow" target="_blank" href="http://www.jd.com/contact/joinin.aspx">商家入駐</a>|<a rel="nofollow" target="_blank" href="http://sale.jd.com/act/y3surX7qpM.html">廣告服務</a>|<a rel="nofollow" target="_blank" href="http://app.jd.com/">手機京東</a>|<a target="_blank" href="http://club.jd.com/links.aspx">友情鏈接</a>|<a target="_blank" href="http://cps.jd.com/">銷售聯盟</a>|<a href="http://club.jd.com/" target="_blank">京東社區</a>|<a href="http://gongyi.jd.com/" target="_blank">京東公益</a></div>
        <div class="copyright">北京市公安局朝陽分局備案編號110105014669  |  京ICP證070359號  |  互聯網藥品信息服務資格證編號(京)-非經營性-2011-0034<br><a rel="nofollow" href="http://misc.360buyimg.com/skin/df/i/com/f_music.jpg" target="_blank">音像製品經營許可證蘇宿批005號</a>|  出版物經營許可證編號新出發(蘇)批字第N-012號  |  互聯網出版許可證編號新出網證(京)字150號<br><a href="http://misc.360buyimg.com/wz/wlwhjyxkz.jpg" target="_blank">網絡文化經營許可證京網文[2011]0168-061號</a>Copyright © 2004-2015  京東JD.com 版權所有<br>京東旗下網站:<a href="http://en.jd.com/" target="_blank">English Site</a></div>
        <div class="authentication"><a rel="nofollow" target="_blank" href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202007080200026"><img width="108" height="40" alt="經營性網站備案中心" src="<c:url value='/resource'/>/108_40_zZOKnl.gif" class="err-product"></a>
            <a rel="nofollow" target="_blank" tabindex="-1"
                href="https://ss.cnnic.cn/verifyseal.dll?sn=2008070300100000031&ct=df&pa=294005"
                id="urlknet"><img width="108" height="40" border="true"
                name="CNNIC_seal" alt="可信網站"
                src="<c:url value='/resource'/>/kxwz.gif"
                class="err-product"></a>
            <a rel="nofollow" target="_blank"
                href="http://www.bj.cyberpolice.cn/index.do"><img width="108"
                height="40" alt="朝陽網絡警察"
                src="<c:url value='/resource'/>/cywljc.png"
                class="err-product"></a>
            <a rel="nofollow" target="_blank"
                href="https://search.szfw.org/cert/l/CX20120111001803001836"><img
                width="112" height="40"
                src="<c:url value='/resource'/>/112_40_WvArIl.png"
                class="err-product"></a>
        </div>
    </div>
</div>
</body>
</html>

(2)代碼開發

實體pojo:

商品實體類:

package com.hnu.scw.model;
/**
 * 商品實體
 * @author scw
 *
 */
public class ProductModel {
    // 商品編號
    private String pid;
    // 商品名稱
    private String name;
    // 商品分類名稱
    private String catalog_name;
    // 價格
    private float price;
    // 商品描述
    private String description;
    // 圖片名稱
    private String picture;
    
    
    public String getPid() {
        return pid;
    }
    public void setPid(String pid) {
        this.pid = pid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCatalog_name() {
        return catalog_name;
    }
    public void setCatalog_name(String catalog_name) {
        this.catalog_name = catalog_name;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getPicture() {
        return picture;
    }
    public void setPicture(String picture) {
        this.picture = picture;
    }
}
檢索實體類:

package com.hnu.scw.model;
/**
 * 搜索商品的實體類
 * @author scw
 *
 */
public class ProductSearch {
    private String queryString;  //關鍵字
    private String catalog_name; //類別
    private String price;  //價格
    private String sort;  //排序類型
    public String getQueryString() {
        return queryString;
    }
    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }
    public String getCatalog_name() {
        return catalog_name;
    }
    public void setCatalog_name(String catalog_name) {
        this.catalog_name = catalog_name;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getSort() {
        return sort;
    }
    public void setSort(String sort) {
        this.sort = sort;
    }    
}
dao層:

package com.hnu.scw.dao;
 
import java.util.List;
 
import com.hnu.scw.model.ProductModel;
import com.hnu.scw.model.ProductSearch;
 
public interface SearchProductDao {
    public List<ProductModel> searchProduct(ProductSearch productSearch) throws Exception;
}
package com.hnu.scw.dao.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.hnu.scw.dao.SearchProductDao;
import com.hnu.scw.model.ProductModel;
import com.hnu.scw.model.ProductSearch;
@Repository
public class SearchProductDaoImpl implements SearchProductDao {
    //通過springmvc來進行注入solr服務器
    @Autowired
    private SolrServer solrServer;
 
    @Override
    public List<ProductModel> searchProduct(ProductSearch productSearch) throws Exception {
        SolrQuery solrQuery = new SolrQuery();
        //設置關鍵字
        solrQuery.setQuery(productSearch.getQueryString());
        //設置默認檢索域
        solrQuery.set("df", "product_keywords");
        //設置過濾條件
        if(null != productSearch.getCatalog_name() && !"".equals(productSearch.getCatalog_name())){
            solrQuery.set("fq", "product_catalog_name:" + productSearch.getCatalog_name());
        }
        if(null != productSearch.getPrice() && !"".equals(productSearch.getPrice())){
            //0-9   50-*  對價格進行過濾
            String[] p = productSearch.getPrice().split("-");
            solrQuery.set("fq", "product_price:[" + p[0] + " TO " + p[1] + "]");
        }
        // 價格排序
        if ("1".equals(productSearch.getSort())) {
            solrQuery.addSort("product_price", ORDER.desc);
        } else {
            solrQuery.addSort("product_price", ORDER.asc);
        }
        // 分頁
        solrQuery.setStart(0);
        solrQuery.setRows(16);
        // 只查詢指定域
        solrQuery.set("fl", "id,product_name,product_price,product_picture");
        // 高亮
        // 打開開關
        solrQuery.setHighlight(true);
        // 指定高亮域
        solrQuery.addHighlightField("product_name");
        // 前綴
        solrQuery.setHighlightSimplePre("<span style='color:red'>");
        solrQuery.setHighlightSimplePost("</span>");
        // 執行查詢
        QueryResponse response = solrServer.query(solrQuery);
        // 文檔結果集
        SolrDocumentList docs = response.getResults();
 
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        
        List<ProductModel> productModels = new ArrayList<ProductModel>();
        for (SolrDocument doc : docs) {
            ProductModel productModel = new ProductModel();
            productModel.setPid((String) doc.get("id"));
            productModel.setPrice((Float) doc.get("product_price"));
            productModel.setPicture((String) doc.get("product_picture"));
            Map<String, List<String>> map = highlighting.get((String) doc.get("id"));
            List<String> list = map.get("product_name");
            
            productModel.setName(list.get(0));
            productModels.add(productModel);
        }
        return productModels;
    }
 
}
service層:

package com.hnu.scw.service;
 
import java.util.List;
import com.hnu.scw.model.ProductModel;
import com.hnu.scw.model.ProductSearch;
 
public interface SearchProductService {
    
    public List<ProductModel> searchProduct(ProductSearch productSearch) throws Exception;
 
}
package com.hnu.scw.service.impl;
 
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hnu.scw.dao.impl.SearchProductDaoImpl;
import com.hnu.scw.model.ProductModel;
import com.hnu.scw.model.ProductSearch;
import com.hnu.scw.service.SearchProductService;
 
@Service
public class SearchProductImpl implements SearchProductService{
    @Autowired
    private SearchProductDaoImpl searchProductDaoImpl;
    
    @Override
    public List<ProductModel> searchProduct(ProductSearch productSearch) throws Exception {
        return searchProductDaoImpl.searchProduct(productSearch);
    }
 
}
controller層:

package com.hnu.scw.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
/**
 * 進行商品搜索處理(通過solr)
 * @author scw
 *2018-02-27
 */
import org.springframework.web.bind.annotation.RequestMapping;
import com.hnu.scw.model.ProductModel;
import com.hnu.scw.model.ProductSearch;
import com.hnu.scw.service.impl.SearchProductImpl;
@Controller
public class SearchProductController {
    
    @Autowired
    private SearchProductImpl searchProductImpl ;
    
    /**
     * 對於搜索的處理,包括關鍵字,價格,類別,還有排序方式
     * @param productSearch
     * @return
     * @throws Exception 
     */
    @RequestMapping(value="/list")
    public String searchProduct(ProductSearch productSearch , Model model) throws Exception{
        //獲取到檢索的所有結果
        List<ProductModel> searchProducts = searchProductImpl.searchProduct(productSearch);
        //設置回顯內容
        model.addAttribute("productModels", searchProducts);
        model.addAttribute("queryString", productSearch.getQueryString());
        model.addAttribute("catalog_name", productSearch.getCatalog_name());
        model.addAttribute("price", productSearch.getPrice());
        model.addAttribute("sort", productSearch.getSort());
        return "product_list";
    }
    
    
}
(3)進行測試

注意事項:
第一點:商品的信息最初是存放在mysql數據庫中,而要實現Solr的站內搜索,就需要把數據庫中的商品數據導入到Solr服務器中,這個在上面的知識點已經提到過了,所以就不多介紹,可以翻到上面進行查閱。大體的數據如下:

第二點:因爲,這是單機的Solr檢索,而且Solr服務器也是在本地開啓的,而現在又有一個Web項目,所以端口就需要進行修改下,我這裏Solr服務器是用的8080端口,而Web項目是用的8081端口。。修改端口方式如下:

進行測試,訪問項目鏈接,

由於我本身的數據中就只有部分,所以,我進行檢索檯燈,得到如下的結果:

如果有需要這個Demo的小夥伴,那麼可以通過百度雲鏈接來進行獲取,如果寫得不明白的地方,也歡迎各位進行交流。

https://pan.baidu.com/s/1dRClm2           密碼:olio

        好了,對於Solr的單機運行就到這裏了,當然,比如像真正的京東和淘寶,都不是單機的Solr,肯定是集羣的,這樣的話,又存在着有不同的地方,另外,還有一種比較好的搜索架構也是挺不錯的---Elasticsearch,它也是基於Lucene的系統架構,但是是一種分佈式的檢索,所以,與Solr各有優勢,這個就需要看具體的需求來決定了。總而言之,技術沒有總結,只有不斷的努力學習和研究。。。。。

數據庫中數據導入solr中,一種方式是通過solr界面按鈕進行導入,另外一種是通過 運行 http://solr-host:port/dataimpor?command=full-import 導入數據(此方法適合動態拉庫數據)。


--------------------- 
作者:Cs_hnu_scw 
來源:CSDN 
原文:https://blog.csdn.net/cs_hnu_scw/article/details/79388080 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章