后期補(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> |
第四步:從索引庫(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" />
延伸 · 閱讀
精彩推薦
|