一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 利用solr實(shí)現(xiàn)商品的搜索功能(實(shí)例講解)

利用solr實(shí)現(xiàn)商品的搜索功能(實(shí)例講解)

2021-02-22 11:42小蝦米的java夢(mèng) Java教程

下面小編就為大家分享一篇利用solr實(shí)現(xiàn)商品的搜索功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

后期補(bǔ)充:

為什么要用solr服務(wù),為什么要用luncence?

問(wèn)題提出:當(dāng)我們?cè)L問(wèn)購(gòu)物網(wǎng)站的時(shí)候,我們可以根據(jù)我們隨意所想的內(nèi)容輸入關(guān)鍵字就可以查詢(xún)出相關(guān)的內(nèi)容,這是怎么做到呢?這些隨意的數(shù)據(jù)不可能是根據(jù)數(shù)據(jù)庫(kù)的字段查詢(xún)的,那是怎么查詢(xún)出來(lái)的呢,為什么千奇百怪的關(guān)鍵字都可以查詢(xún)出來(lái)呢?

答案就是全文檢索工具的實(shí)現(xiàn),luncence采用了詞元匹配和切分詞。舉個(gè)例子:北京天安門(mén)------luncence切分詞:北京 京天 天安 安門(mén) 等等這些分詞。所以我們搜索的時(shí)候都可以檢索到。

有一種分詞器就是ikanalyzer中文分詞器,它有細(xì)粒度切分和智能切分,即根據(jù)某種智能算法。

這就使用solr的最大的好處:檢索功能的實(shí)現(xiàn)。

使用步驟;

(1)solr服務(wù)器搭建,因?yàn)椋螅铮欤蚴怯茫辏幔觯幔甸_(kāi)發(fā)的,所以需要jdk和tomcat。搭建部署

(2)搭建完成后,我們需要將要展示的字段引入solr的庫(kù)中。配置spring與solr結(jié)合,工程啟動(dòng)的時(shí)候啟動(dòng)solr

(3)將數(shù)據(jù)庫(kù)中的查詢(xún)內(nèi)容導(dǎo)入到solr索引庫(kù),這里使用的是solrj的客戶(hù)端實(shí)現(xiàn)的。具體使用可以參考api

(4)建立搜索服務(wù),供客戶(hù)端調(diào)用。調(diào)用solr,查詢(xún)內(nèi)容,這中間有分頁(yè)功能的實(shí)現(xiàn)。solr高亮顯示的實(shí)現(xiàn)。

(5)客戶(hù)端接收頁(yè)面的請(qǐng)求參數(shù),調(diào)用搜索服務(wù),進(jìn)行搜索。

業(yè)務(wù)字段判斷標(biāo)準(zhǔn):

1、在搜索時(shí)是否需要在此字段上進(jìn)行搜索。例如:商品名稱(chēng)、商品的賣(mài)點(diǎn)、商品的描述

(這些相當(dāng)于將標(biāo)簽給了solr,導(dǎo)入商品數(shù)據(jù)后,solr?qū)@些字段的對(duì)應(yīng)的商品的具體內(nèi)容進(jìn)行分詞切分,然后,我們就可以搜索到相關(guān)內(nèi)容了)

2、后續(xù)的業(yè)務(wù)是否需要用到此字段。例如:商品id。

需要用到的字段:

1、商品id

2、商品title

3、賣(mài)點(diǎn)

4、價(jià)格

5、商品圖片

6、商品分類(lèi)名稱(chēng)

7、商品描述

solr中的業(yè)務(wù)字段:

1、id——》商品id

其他的對(duì)應(yīng)字段創(chuàng)建solr的字段。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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_desc" type="text_ik" indexed="true" stored="false" />
 
 
 
<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"/>
 
<copyfield source="item_desc" dest="item_keywords"/>

重新啟動(dòng)tomcat

solr 是apache下的一個(gè)頂級(jí)開(kāi)源項(xiàng)目,采用java開(kāi)發(fā),它是基于lucene的全文搜索服務(wù)器。solr提供了比lucene更為豐富的查詢(xún)語(yǔ)言,同時(shí)實(shí)現(xiàn)了可配置、可擴(kuò)展,并對(duì)索引、搜索性能進(jìn)行了優(yōu)化。

solr是一個(gè)全文檢索服務(wù)器,只需要進(jìn)行配置就可以實(shí)現(xiàn)全文檢索服務(wù)。有效降低頻繁訪問(wèn)數(shù)據(jù)庫(kù)對(duì)數(shù)據(jù)庫(kù)造成的壓力。

第一步:將solr部署在linux系統(tǒng)下。

第二步:solrj是solr的客戶(hù)端,使用它需要依賴(lài)solrj的jar包。

第三步:將數(shù)據(jù)庫(kù)的內(nèi)容添加到solr的索引庫(kù),這樣查詢(xún)就在索引庫(kù)查詢(xún),而不是數(shù)據(jù)庫(kù)了。

controller層:

?
1
2
3
4
5
6
7
8
9
10
11
12
@controller
@requestmapping("/manager")
public class itemcontroller {
 @autowired
 private itemservice itemservice;
 @requestmapping("/importall")
 @responsebody
 public taotaoresult importallitem(){
   taotaoresult result= itemservice.importallitem();
   return result;
 }
}<br>service層編寫(xiě):<br>多表查詢(xún)商品,顯示在頁(yè)面的邏輯編寫(xiě):<br>mapper.java
?
1
2
3
4
5
6
7
8
9
10
package com.taotao.search.mapper;
 
import java.util.list;
 
import com.taotao.search.pojo.item;
 
public interface itemmapper {
 list<item> getitemlist();
 
}

mapper.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.taotao.search.mapper.itemmapper">
<select id="getitemlist" resulttype="com.taotao.search.pojo.item">
 select
 a.id,
 a.title,
 a.sell_point,
 a.price,
 a.image,
 b. name category_name
 from
 tb_item a
 left join tb_item_cat b on a.cid = b.id
</select>
</mapper>

利用solr實(shí)現(xiàn)商品的搜索功能(實(shí)例講解)

第四步:從索引庫(kù)查詢(xún)的邏輯編寫(xiě):

?

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            //從索引庫(kù)里面獲取商品信息,現(xiàn)在這個(gè)dao層是從索引庫(kù)獲取信息,因?yàn)橹暗膶?xiě)的邏輯是將db里面的數(shù)據(jù)導(dǎo)入到索引庫(kù)。后面的查詢(xún)都是從索引庫(kù)中進(jìn)行,而不從數(shù)據(jù)庫(kù)了
            @repository
            public class searchdaoimpl implements searchdao {
             @autowired
             private solrserver solrserver;
             
             @override
             public searchresult search(solrquery query) throws exception {
              //這是從索引庫(kù)里面,直接執(zhí)行查詢(xún)
              queryresponse response = solrserver.query(query);
              //獲取查詢(xún)的結(jié)果
              solrdocumentlist documentlist= response.getresults();
               
              searchresult result=new searchresult();
              //這是獲取總記錄數(shù)
              result.setrecordcount(documentlist.getnumfound());
               
              list<item> itemlist=new arraylist<>();
              //商品的高亮顯示,即當(dāng)鼠標(biāo)移到字上時(shí),該字體變色,這是從queryresponse中獲取的
              map<string, map<string, list<string>>> highlighting = response.gethighlighting();
               
              for (solrdocument solrdocument : documentlist) {
                
               //每個(gè)solrdocument都是一個(gè)商品pojo的內(nèi)容,所以這里要?jiǎng)?chuàng)建一個(gè)商品的pojo對(duì)象,來(lái)獲取詳細(xì)的字段
               item item=new item();
               item.setid((string) solrdocument.get("id"));
               //高亮顯示是title的高亮顯示
               list<string> list = highlighting.get(solrdocument.get("id")).get("item_title");
               string height="345" src="/uploads/allimg/210222/1143313629-1.jpg" width="764" />

            請(qǐng)求的url:

            /search/query?q={查詢(xún)條件}&page={page}&rows={rows}

            返回的結(jié)果:taotaoresult包裝商品列表。

            創(chuàng)建一個(gè)sql語(yǔ)句對(duì)應(yīng)的pojo,單獨(dú)建立一個(gè)pojo

            用來(lái)裝顯示的內(nèi)容列表:

            ?
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            public class item {
              private string id;
              private string title;
              private string sell_point;
              private long price;
              private string image;
              private string category_name;
              private string item_des;
             
            }

            controller層:

            ?
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            @controller
            public class searchcontroller {
             @autowired
             private searchservice searchservice;
              
             @requestmapping(value="/query", method=requestmethod.get)
             @responsebody
             public taotaoresult search(@requestparam("q")string querystring,
               @requestparam(defaultvalue="1")integer page,
               @requestparam(defaultvalue="60")integer rows) {
              //查詢(xún)條件不能為空
              if (stringutils.isblank(querystring)) {
               return taotaoresult.build(400, "查詢(xún)條件不能為空");
              }
              searchresult searchresult = null;
              try {
               querystring = new string(querystring.getbytes("iso8859-1"), "utf-8");
               searchresult = searchservice.search(querystring, page, rows);
              } catch (exception e) {
               e.printstacktrace();
               return taotaoresult.build(500, exceptionutil.getstacktrace(e));
              }
              return taotaoresult.ok(searchresult);
               
             }
             }<br><br><br>
            1
            <span style="font-size: 16px">service層:利用solrj的solrqurery來(lái)查詢(xún):</span>

            前提是要寫(xiě)好如何從索引庫(kù)讀取數(shù)據(jù):  

            下面是服務(wù)的接口層編寫(xiě):

            controller:

            ?
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            @controller
            public class searchcontroller {
             @autowired
             private searchservice searchservice;
              
             @requestmapping(value="/query", method=requestmethod.get)
             @responsebody
             public taotaoresult search(@requestparam("q")string querystring,
               @requestparam(defaultvalue="1")integer page,
               @requestparam(defaultvalue="60")integer rows) {
              //查詢(xún)條件不能為空
              if (stringutils.isblank(querystring)) {
               return taotaoresult.build(400, "查詢(xún)條件不能為空");
              }
              searchresult searchresult = null;
              try {
               querystring = new string(querystring.getbytes("iso8859-1"), "utf-8");
               searchresult = searchservice.search(querystring, page, rows);
              } catch (exception e) {
               e.printstacktrace();
               return taotaoresult.build(500, exceptionutil.getstacktrace(e));
              }
              return taotaoresult.ok(searchresult);
               
             }
             }
            ?
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            @service
            public class searchserviceimpl implements searchservice {
             @autowired
             private searchdao searchdao;
              
             
             @override
             public searchresult search(string querystring, int page, int rows) throws exception {
             solrquery query=new solrquery();
             query.setquery(querystring);
             query.setstart((page-1)*rows);
             query.setrows(rows);
             //設(shè)置默認(rèn)的查詢(xún)搜索域,即默認(rèn)的查詢(xún)
             query.set("df","item_keywords");
             //設(shè)置高亮顯示
             query.sethighlight(true);
              
             query.addhighlightfield("item_title");
             query.sethighlightsimplepre("<em style=\"color:red\">");
             query.sethighlightsimplepost("</em>");
            //執(zhí)行查詢(xún)
             searchresult searchresult = searchdao.search(query);
             //根據(jù)結(jié)果來(lái)計(jì)算商品總共多少頁(yè)
             long recordcount=searchresult.getrecordcount();
             long pagecount=recordcount/rows;
             if (recordcount % rows > 0) {
              pagecount++;
             }
             searchresult.setpagecount(pagecount);
             searchresult.setcurpage((long) page);
              
              return searchresult;
             }
             
            }

            客戶(hù)端通過(guò)輸入商品來(lái)實(shí)現(xiàn)搜索功能:

            controller層:

            @controller

            ?
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            public class searchcontroller {
             @autowired
             private searchservice searchservice;
              
             @requestmapping("/search")
             public string search(@requestparam("q")string querystring, @requestparam(defaultvalue="1")integer page, model model) {
              if (querystring != null) {
               try {
                querystring = new string(querystring.getbytes("iso8859-1"), "utf-8");
               } catch (unsupportedencodingexception e) {
                e.printstacktrace();
               }
              }
              searchresult searchresult = searchservice.search(querystring, page);
              //向頁(yè)面?zhèn)鬟f參數(shù)
              model.addattribute("query", querystring);
              //model.addattribute("totalpages", searchresult.getpagecount());
              model.addattribute("itemlist", searchresult.getitemlist());
              model.addattribute("page", page);
               
              return "search";
               
             }
            }

            service層:

            ?
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            @service
            public class searchserviceimpl implements searchservice {
              
             @value("${search_base_url}")
             private string search_base_url;
             
             @override
             public searchresult search(string querystring, int page) {
              //這里需要的是連接+參數(shù).這里每頁(yè)顯示的記錄條數(shù),可以傳遞也可以不用傳遞
              // 調(diào)用taotao-search的服務(wù)
              //查詢(xún)參數(shù)
              map<string, string> param = new hashmap<>();
              param.put("q", querystring);
              param.put("page", page + "");
              try {
               //調(diào)用服務(wù)
               string json = httpclientutil.doget(search_base_url, param);
               //把字符串轉(zhuǎn)換成java對(duì)象
               taotaoresult taotaoresult = taotaoresult.formattopojo(json, searchresult.class);
               searchresult result = (searchresult) taotaoresult.getdata();
               return result;
              /* if (taotaoresult.getstatus() == 200) {
                 
               }*/
                
              } catch (exception e) {
               e.printstacktrace();
               return null;
              }
              
             }
             
            }

            以上這篇利用solr實(shí)現(xiàn)商品的搜索功能(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

            原文鏈接:https://www.cnblogs.com/fengli9998/p/6475970.html

            延伸 · 閱讀

            精彩推薦
            主站蜘蛛池模板: kkkk4444在线看片 | 狠狠久久久久综合网 | 午夜办公室在线观看高清电影 | 欧美在线视频 一区二区 | 欧美精品1区2区 | 催奶师小说| 日韩毛片在线 | 91麻豆国产福利在线观看 | 欧美激情亚洲 | 精品一二三区久久AAA片 | 日本三级香港三级久久99 | 美国69xxxx59 | 奶茶视频有容乃大 | 国产一区国产二区国产三区 | 国产成人精品三级在线 | 欧美日韩国产成人综合在线影院 | 欧美a级在线观看 | 久久视频这有精品63在线国产 | 男男gaygays国内| 美女福利视频午夜在线 | 亚洲国产高清一区二区三区 | 99青青青精品视频在线 | 日本免费观看的视频在线 | 鸭子玩富婆流白浆视频 | 亚洲欧美专区精品久久 | 国产成+人+综合+亚洲不卡 | 91免费播放| 亚洲午夜久久久久影院 | 我的奶头被客人吸的又肿又红 | 免费的毛片视频 | 久久视频在线视频观看天天看视频 | videos变态极端 | 俄罗斯bbbbbbbbb大片 | 肉车各种play文r | 456老汉gay | 2020最新版的ab片 | 亚洲3dxxxx动漫xxx | 大胸被c出奶水嗷嗷叫 | 久久免费特黄毛片 | 深夜网站在线观看 | 99视频一区|