最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年11月28日 10:24:00   作者:小蝦米的java夢  
下面小編就為大家分享一篇利用solr實(shí)現(xiàn)商品的搜索功能,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

后期補(bǔ)充:

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

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

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

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

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

使用步驟;

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

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

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

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

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

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

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

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

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

需要用到的字段:

1、商品id

2、商品title

3、賣點(diǎn)

4、價(jià)格

5、商品圖片

6、商品分類名稱

7、商品描述

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

1、id——》商品id

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

<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í)開源項(xiàng)目,采用Java開發(fā),它是基于Lucene的全文搜索服務(wù)器。Solr提供了比Lucene更為豐富的查詢語言,同時(shí)實(shí)現(xiàn)了可配置、可擴(kuò)展,并對索引、搜索性能進(jìn)行了優(yōu)化。

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

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

第二步:solrJ是solr的客戶端,使用它需要依賴solrJ的jar包。

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

controller層:

@Controller
@RequestMapping("/manager")
public class ItemController {
 @Autowired
 private ItemService itemService;
 @RequestMapping("/importall")
 @ResponseBody
 public TaotaoResult importAllItem(){
   TaotaoResult result= itemService.importAllItem();
   return result;
 }
}<br>service層編寫:<br>多表查詢商品,顯示在頁面的邏輯編寫:<br>mapper.java
package com.taotao.search.mapper;
 
import java.util.List;
 
import com.taotao.search.pojo.Item;
 
public interface ItemMapper {
 List<Item> getItemList();
 
}

mapper.xml

<?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>

第四步:從索引庫查詢的邏輯編寫:

//從索引庫里面獲取商品信息,現(xiàn)在這個(gè)dao層是從索引庫獲取信息,因?yàn)橹暗膶懙倪壿嬍菍b里面的數(shù)據(jù)導(dǎo)入到索引庫。后面的查詢都是從索引庫中進(jìn)行,而不從數(shù)據(jù)庫了
@Repository
public class SearchDaoImpl implements SearchDao {
 @Autowired
 private SolrServer solrServer;
 
 @Override
 public SearchResult search(SolrQuery query) throws Exception {
  //這是從索引庫里面,直接執(zhí)行查詢
  QueryResponse response = solrServer.query(query);
  //獲取查詢的結(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對象,來獲取詳細(xì)的字段
   Item item=new Item();
   item.setId((String) solrDocument.get("id"));
   //高亮顯示是title的高亮顯示
   List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
   String title="";
   if (list!=null && list.size()>0) {
    title=list.get(0); 
   }
   else{
    title=(String) solrDocument.get("item_title"); 
   }
   
   item.setTitle(title);
   item.setPrice((Long) solrDocument.get("item_price"));
   item.setImage((String) solrDocument.get("item_image"));
   item.setCategory_name((String) solrDocument.get(" item_category_name"));
   item.setSell_point((String) solrDocument.get("item_sell_point"));
   itemList.add(item);
  }
   
   result.setItemList(itemList);
  
  return result;
 }
 
}

第五步:索引庫內(nèi)容建立好后,開始編寫對外的服務(wù)接口,即通過條件搜索具體的商品,比如手機(jī),會(huì)顯示出總共的手機(jī)列表信息,第幾頁,總共多少頁,總共多少個(gè)搜索結(jié)果

請求的url:

/search/query?q={查詢條件}&page={page}&rows={rows}

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

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

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

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層:

@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) {
  //查詢條件不能為空
  if (StringUtils.isBlank(queryString)) {
   return TaotaoResult.build(400, "查詢條件不能為空");
  }
  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來查詢:</span>

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

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

controller:

@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) {
  //查詢條件不能為空
  if (StringUtils.isBlank(queryString)) {
   return TaotaoResult.build(400, "查詢條件不能為空");
  }
  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);
   
 } 
 }
@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)的查詢搜索域,即默認(rèn)的查詢
 query.set("df","item_keywords");
 //設(shè)置高亮顯示
 query.setHighlight(true);
  
 query.addHighlightField("item_title");
 query.setHighlightSimplePre("<em style=\"color:red\">");
 query.setHighlightSimplePost("</em>");
//執(zhí)行查詢
 SearchResult searchResult = searchDao.search(query);
 //根據(jù)結(jié)果來計(jì)算商品總共多少頁
 long recordCount=searchResult.getRecordCount();
 long pageCount=recordCount/rows;
 if (recordCount % rows > 0) {
  pageCount++; 
 }
 searchResult.setPageCount(pageCount);
 searchResult.setCurPage((long) page);
  
  return searchResult;
 }
 
}

客戶端通過輸入商品來實(shí)現(xiàn)搜索功能:

controller層:

@Controller

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);
  //向頁面?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層:

@Service
public class SearchServiceImpl implements SearchService {
  
 @Value("${SEARCH_BASE_URL}")
 private String SEARCH_BASE_URL;
 
 @Override
 public SearchResult search(String queryString, int page) {
  //這里需要的是連接+參數(shù).這里每頁顯示的記錄條數(shù),可以傳遞也可以不用傳遞
  // 調(diào)用taotao-search的服務(wù)
  //查詢參數(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對象
   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í)例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • 如何用SpringBoot 進(jìn)行測試

    如何用SpringBoot 進(jìn)行測試

    這篇文章主要介紹了如何用SpringBoot 進(jìn)行測試,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • 基于FeignException$InternalServerError的解決方案

    基于FeignException$InternalServerError的解決方案

    這篇文章主要介紹了FeignException$InternalServerError的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 親手帶你解決Debug Fastjson的安全漏洞

    親手帶你解決Debug Fastjson的安全漏洞

    這篇文章主要介紹了親手帶你解決Debug Fastjson的安全漏洞,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Delegate IDE build/run actions to maven 配置會(huì)影響程序運(yùn)行嗎?

    Delegate IDE build/run actions to maven 配置會(huì)影響程序運(yùn)行嗎?

    這篇文章主要介紹了Delegate IDE build/run actions to maven 配置會(huì)影響程序運(yùn)行嗎,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)

    Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)

    這篇文章主要介紹了Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • SpringBoot三種方法接口返回日期格式化小結(jié)

    SpringBoot三種方法接口返回日期格式化小結(jié)

    本文介紹了三種在Spring Boot中格式化接口返回日期的方法,包含使用@JsonFormat注解、全局配置JsonConfig、以及在yml文件中配置時(shí)區(qū),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-01-01
  • 本地jvm執(zhí)行flink程序帶web ui的操作

    本地jvm執(zhí)行flink程序帶web ui的操作

    這篇文章主要介紹了本地jvm執(zhí)行flink程序帶web ui的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot進(jìn)行參數(shù)校驗(yàn)的方法詳解

    SpringBoot進(jìn)行參數(shù)校驗(yàn)的方法詳解

    在日常的接口開發(fā)中,為了防止非法參數(shù)對業(yè)務(wù)造成影響,經(jīng)常需要對接口的參數(shù)進(jìn)行校驗(yàn)。本文通過示例詳細(xì)講解了SpringBoot如何進(jìn)行參數(shù)校驗(yàn)的,感興趣的可以學(xué)習(xí)一下
    2022-04-04
  • SpringBoot之Refresh流程的簡單說明

    SpringBoot之Refresh流程的簡單說明

    這篇文章主要介紹了SpringBoot之Refresh流程的簡單說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • springboot-controller的使用詳解

    springboot-controller的使用詳解

    本篇文章主要介紹了springboot-controller的使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08

最新評(píng)論

凤阳县| 古蔺县| 永州市| 大城县| 靖安县| 克山县| 新沂市| 灌阳县| 阿坝县| 凤凰县| 平江县| 桑植县| 固安县| 太原市| 寻甸| 高碑店市| 伊通| 富川| 英吉沙县| 乌兰县| 香格里拉县| 商都县| 陆良县| 遂昌县| 纳雍县| 长治市| 临朐县| 大方县| 偃师市| 阳高县| 淅川县| 孙吴县| 永善县| 漳浦县| 孝昌县| 右玉县| 邵武市| 烟台市| 西吉县| 宣化县| 云安县|