服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - 利用solr实现商品的搜索功能(实例讲解)

利用solr实现商品的搜索功能(实例讲解)

2021-02-22 11:42小虾米的java梦 JAVA教程

下面小编就为大家分享一篇利用solr实现商品的搜索功能,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

后期补充:

为什么要用solr服务,为什么要用luncence?

问题提出:当我们访问购物网站的时候,我们可以根据我们随意所想的内容输入关键字就可以查询出相关的内容,这是怎么做到呢?这些随意的数据不可能是根据数据库的字段查询的,那是怎么查询出来的呢,为什么千奇百怪的关键字都可以查询出来呢?

答案就是全文检索工具的实现,luncence采用了词元匹配和切分词。举个例子:北京天安门------luncence切分词:北京 京天 天安 安门 等等这些分词。所以我们搜索的时候都可以检索到。

有一种分词器就是ikanalyzer中文分词器,它有细粒度切分和智能切分,即根据某种智能算法。

这就使用solr的最大的好处:检索功能的实现。

使用步骤;

(1)solr服务器搭建,因为solr是用java5开发的,所以需要jdk和tomcat。搭建部署

(2)搭建完成后,我们需要将要展示的字段引入solr的库中。配置spring与solr结合,工程启动的时候启动solr

(3)将数据库中的查询内容导入到solr索引库,这里使用的是solrj的客户端实现的。具体使用可以参考api

(4)建立搜索服务,供客户端调用。调用solr,查询内容,这中间有分页功能的实现。solr高亮显示的实现。

(5)客户端接收页面的请求参数,调用搜索服务,进行搜索。

业务字段判断标准:

1、在搜索时是否需要在此字段上进行搜索。例如:商品名称、商品的卖点、商品的描述

(这些相当于将标签给了solr,导入商品数据后,solr对这些字段的对应的商品的具体内容进行分词切分,然后,我们就可以搜索到相关内容了)

2、后续的业务是否需要用到此字段。例如:商品id。

需要用到的字段:

1、商品id

2、商品title

3、卖点

4、价格

5、商品图片

6、商品分类名称

7、商品描述

solr中的业务字段:

1、id——》商品id

其他的对应字段创建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"/>

重新启动tomcat

solr 是apache下的一个顶级开源项目,采用java开发,它是基于lucene的全文搜索服务器。solr提供了比lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。

solr是一个全文检索服务器,只需要进行配置就可以实现全文检索服务。有效降低频繁访问数据库对数据库造成的压力。

第一步:将solr部署在linux系统下。

第二步:solrj是solr的客户端,使用它需要依赖solrj的jar包。

第三步:将数据库的内容添加到solr的索引库,这样查询就在索引库查询,而不是数据库了。

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层编写:<br>多表查询商品,显示在页面的逻辑编写:<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实现商品的搜索功能(实例讲解)

第四步:从索引库查询的逻辑编写:

?
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
//从索引库里面获取商品信息,现在这个dao层是从索引库获取信息,因为之前的写的逻辑是将db里面的数据导入到索引库。后面的查询都是从索引库中进行,而不从数据库了
@repository
public class searchdaoimpl implements searchdao {
 @autowired
 private solrserver solrserver;
 
 @override
 public searchresult search(solrquery query) throws exception {
  //这是从索引库里面,直接执行查询
  queryresponse response = solrserver.query(query);
  //获取查询的结果
  solrdocumentlist documentlist= response.getresults();
   
  searchresult result=new searchresult();
  //这是获取总记录数
  result.setrecordcount(documentlist.getnumfound());
   
  list<item> itemlist=new arraylist<>();
  //商品的高亮显示,即当鼠标移到字上时,该字体变色,这是从queryresponse中获取的
  map<string, map<string, list<string>>> highlighting = response.gethighlighting();
   
  for (solrdocument solrdocument : documentlist) {
    
   //每个solrdocument都是一个商品pojo的内容,所以这里要创建一个商品的pojo对象,来获取详细的字段
   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" />

请求的url:

/search/query?q={查询条件}&page={page}&rows={rows}

返回的结果:taotaoresult包装商品列表。

创建一个sql语句对应的pojo,单独建立一个pojo

用来装显示的内容列表:

?
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) {
  //查询条件不能为空
  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>

前提是要写好如何从索引库读取数据:  

下面是服务的接口层编写:

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) {
  //查询条件不能为空
  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);
   
 }
 }
?
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);
 //设置默认的查询搜索域,即默认的查询
 query.set("df","item_keywords");
 //设置高亮显示
 query.sethighlight(true);
  
 query.addhighlightfield("item_title");
 query.sethighlightsimplepre("<em style=\"color:red\">");
 query.sethighlightsimplepost("</em>");
//执行查询
 searchresult searchresult = searchdao.search(query);
 //根据结果来计算商品总共多少页
 long recordcount=searchresult.getrecordcount();
 long pagecount=recordcount/rows;
 if (recordcount % rows > 0) {
  pagecount++;
 }
 searchresult.setpagecount(pagecount);
 searchresult.setcurpage((long) page);
  
  return searchresult;
 }
 
}

客户端通过输入商品来实现搜索功能:

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);
  //向页面传递参数
  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) {
  //这里需要的是连接+参数.这里每页显示的记录条数,可以传递也可以不用传递
  // 调用taotao-search的服务
  //查询参数
  map<string, string> param = new hashmap<>();
  param.put("q", querystring);
  param.put("page", page + "");
  try {
   //调用服务
   string json = httpclientutil.doget(search_base_url, param);
   //把字符串转换成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实现商品的搜索功能(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

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

延伸 · 阅读

精彩推荐