利用Solr实现电子商城检索功能

2024-01-20 20:20

本文主要是介绍利用Solr实现电子商城检索功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

利用Solr实现电子商城检索功能

Solr

Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-serviceAPI接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引;也可以通过Http Get操作提出查找请求,并得到XML格式的返回结果。

Solr实现全文检索的流程

  • 索引流程solr客户端(浏览器、java程序)可以向solr服务端发送POST请求,请求内容是包含Field等信息的xml
  • 搜索流程solr客户端(浏览器、java程序)可以向solr服务端发送GET请求,solr服务器返回一个xml文档。
  • Solr同样没有视图渲染的功能。

Solr索引搜索过程

  1. 用户输入搜索条件
  2. 对搜索条件进行分词处理(词条化、语言分析)
  3. 根据分词的结果查找索引
  4. 根据索引找到文档ID列表
  5. 根据文档ID列表找到具体的文档,根据出现的频次等计算权重(权重越大,结果越接近),最后将文档列表按照权重排序返回

在这里插入图片描述

Spring+Solr实现电子商城检索功能

Web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><welcome-file-list><welcome-file>product_list.jsp</welcome-file></welcome-file-list><!-- SpringMVC配置 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping><filter><filter-name>Character Encoding</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>Character Encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>

Spring 配置文件

<?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: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.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 配置扫描包 --><context:component-scan base-package="com.itheima" /><!-- 配置注解驱动 --><mvc:annotation-driven /><!-- jsp视图解析器 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前缀 --><property name="prefix" value="/WEB-INF/jsp/"></property><!-- 后缀 --><property name="suffix" value=".jsp"></property></bean><!-- 配置HttpSolrServer --><bean class="org.apache.solr.client.solrj.impl.HttpSolrServer"><constructor-arg value="http://localhost:8080/solr/"></constructor-arg></bean></beans>

这是JDong项目的Contrller层

@Controller
public class ProductController {@Autowiredprivate ProductService service;@RequestMapping("/list")public String list(String queryString, String catalog_name, String price,String sort, Integer page, Model model) throws Exception {System.out.println("访问了list");ResultModel rm = service.getProducts(queryString, catalog_name, price,sort, page);// 将查询结果放到request域model.addAttribute("result", rm);// 简单类型的数据回显model.addAttribute("queryString", queryString);model.addAttribute("catalog_name", catalog_name);model.addAttribute("price", price);model.addAttribute("sort", sort);model.addAttribute("page", page);return "product_list";}}

服务层部分

@Service
public class ProductServiceImpl implements ProductService {// 依赖注入HttpSolrServer@Autowiredprivate HttpSolrServer server;@Overridepublic ResultModel getProducts(String queryString, String catalogName,String price, String sort, Integer page) throws Exception {// 创建SolrQuery对象SolrQuery query = new SolrQuery();// 输入关键字if (StringUtils.isNotEmpty(queryString)) {query.setQuery(queryString);} else {query.setQuery("*:*");}// 输入商品分类过滤条件if (StringUtils.isNotEmpty(catalogName)) {query.addFilterQuery("product_catalog_name:" + catalogName);}// 输入价格区间过滤条件// price的值:0-9 10-19if (StringUtils.isNotEmpty(price)) {String[] ss = price.split("-");if (ss.length == 2) {query.addFilterQuery("product_price:[" + ss[0] + " TO " + ss[1]+ "]");}}// 设置排序if ("1".equals(sort)) {query.setSort("product_price", ORDER.desc);} else {query.setSort("product_price", ORDER.asc);}// 设置分页信息if (page == null)page = 1;query.setStart((page - 1) * 20);query.setRows(20);// 设置默认域query.set("df", "product_keywords");// 设置高亮信息query.setHighlight(true);query.addHighlightField("product_name");query.setHighlightSimplePre("<font style=\"color:red\" >");query.setHighlightSimplePost("</font>");QueryResponse response = server.query(query);// 查询出的结果SolrDocumentList results = response.getResults();// 记录总数long count = results.getNumFound();List<Products> products = new ArrayList<>();Products prod;// 获取高亮信息Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();for (SolrDocument doc : results) {prod = new Products();// 商品IDprod.setPid(doc.get("id").toString());List<String> list = highlighting.get(doc.get("id")).get("product_name");// 商品名称if (list != null)prod.setName(list.get(0));else {prod.setName(doc.get("product_name").toString());}// 商品价格prod.setPrice(Float.parseFloat(doc.get("product_price").toString()));// 商品图片地址prod.setPicture(doc.get("product_picture").toString());products.add(prod);}// 封装ResultModel对象ResultModel rm = new ResultModel();rm.setProductList(products);rm.setCurPage(page);rm.setRecordCount(count);int pageCount = (int) (count / 20);if (count % 20 > 0)pageCount++;// 设置总页数rm.setPageCount(pageCount);return rm;}
}

POJO类

public class Products {// 商品编号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;}}

响应封装类

public class ResultModel {// 商品列表private List<Products> productList;// 商品总数private Long recordCount;// 总页数private int pageCount;// 当前页private int curPage;public List<Products> getProductList() {return productList;}public void setProductList(List<Products> productList) {this.productList = productList;}public Long getRecordCount() {return recordCount;}public void setRecordCount(Long recordCount) {this.recordCount = recordCount;}public int getPageCount() {return pageCount;}public void setPageCount(int pageCount) {this.pageCount = pageCount;}public int getCurPage() {return curPage;}public void setCurPage(int curPage) {this.curPage = curPage;}}

实现效果
这里写图片描述

在这里插入图片描述

这篇关于利用Solr实现电子商城检索功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/627216

相关文章

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

mybatis-plus分表实现案例(附示例代码)

《mybatis-plus分表实现案例(附示例代码)》MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生,:本文主要介绍my... 目录文档说明数据库水平分表思路1. 为什么要水平分表2. 核心设计要点3.基于数据库水平分表注意事项示例

C#高效实现在Word文档中自动化创建图表的可视化方案

《C#高效实现在Word文档中自动化创建图表的可视化方案》本文将深入探讨如何利用C#,结合一款功能强大的第三方库,实现在Word文档中自动化创建图表,为你的数据呈现和报告生成提供一套实用且高效的解决方... 目录Word文档图表自动化:为什么选择C#?从零开始:C#实现Word文档图表的基本步骤深度优化:C

nginx跨域访问配置的几种方法实现

《nginx跨域访问配置的几种方法实现》本文详细介绍了Nginx跨域配置方法,包括基本配置、只允许指定域名、携带Cookie的跨域、动态设置允许的Origin、支持不同路径的跨域控制、静态资源跨域以及... 目录一、基本跨域配置二、只允许指定域名跨域三、完整示例四、配置后重载 nginx五、注意事项六、支持

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

MySQL查看表的历史SQL的几种实现方法

《MySQL查看表的历史SQL的几种实现方法》:本文主要介绍多种查看MySQL表历史SQL的方法,包括通用查询日志、慢查询日志、performance_schema、binlog、第三方工具等,并... 目录mysql 查看某张表的历史SQL1.查看MySQL通用查询日志(需提前开启)2.查看慢查询日志3.

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

MyBatis-Plus逻辑删除实现过程

《MyBatis-Plus逻辑删除实现过程》本文介绍了MyBatis-Plus如何实现逻辑删除功能,包括自动填充字段、配置与实现步骤、常见应用场景,并展示了如何使用remove方法进行逻辑删除,逻辑删... 目录1. 逻辑删除的必要性编程1.1 逻辑删除的定义1.2 逻辑删php除的优点1.3 适用场景2.

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp