(1)、主条件查询
(2)、根据商品分类名称过滤
(3)、价格期间过滤
(4)、价格排序
(5)、分页
(1)、总记录数
(2)、总页数
(3)、商品列表,包括:商品图片、商品标题、商品价格、关键词高亮显示
solrJ的jar包
solrJ依赖的jar包
日志相关jar包
springmvc相关jar包
只需要将springmvc整合如项目即可
(1)、项目整体
(2)、web.xml配置
jdsearch
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springmvc
*.action
(3)、springmvc.xml配置
package com.itheima.jd.pojo;
public class Product {
// 商品编号
private String pid;
// 商品名称
private String name;
// 商品分类名称
private String catalog_name;
// 价格
private float price;
// 图片名称
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 getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
}
package com.itheima.jd.pojo;
import java.util.List;
public class SearchResult {
private List productList;//结果集列表
private long recordCount;//总记录数
private long pageCount;//总页数
public List getProductList() {
return productList;
}
public void setProductList(List productList) {
this.productList = productList;
}
public long getRecordCount() {
return recordCount;
}
public void setRecordCount(long recordCount) {
this.recordCount = recordCount;
}
public long getPageCount() {
return pageCount;
}
public void setPageCount(long pageCount) {
this.pageCount = pageCount;
}
}
(1)、执行查询,得到查询结果
(2)、取查询结果的总记录数
(3)、取商品列表,封装到ProductList中
(4)、取高亮显示的结果
(5)、返回SearchResult对象
package com.itheima.jd.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.itheima.jd.pojo.Product;
import com.itheima.jd.pojo.SearchResult;
@Repository
public class SearchDao {
@Autowired
private SolrServer solrServer;
public SearchResult search(SolrQuery query) throws Exception{
//1.执行查询,得到查询结果
QueryResponse queryResponse = solrServer.query(query);
SolrDocumentList docList = queryResponse.getResults();
//2.取查询结果的总记录数
long count = docList.getNumFound();
//3.取商品列表,封装到productList中
List list = new ArrayList();
//4.取高亮显示的结果
Map>> highlighting = queryResponse.getHighlighting();
for (SolrDocument doc : docList) {
Product product = new Product();
product.setCatalog_name((String) doc.get("product_catalog_name"));
//取高亮结果
List list2 = highlighting.get(doc.get("id")).get("product_name");
String name = "";
if(list2 != null && list2.size() > 0){
name = list2.get(0);
}else{
name = (String) doc.get("product_name");
}
product.setName(name);
product.setPicture((String) doc.get("product_picture"));
product.setPid((String) doc.get("id"));
product.setPrice((float) doc.get("product_price"));
list.add(product);
}
//5.返回SearchResult对象
SearchResult result = new SearchResult();
result.setProductList(list);
result.setRecordCount(count);
return result;
}
}
a、主查询条件 String queryString
b、根据商品分类名称过滤 String catalog_name
c、价格区间过滤 String price
d、价格排序 int sort 0:升序 1:降序
e、分页 int page 页码从1开始,需要计算start,rows
SearchResult
a、根据参数创建SolrQuery对象
b、调用dao执行查询,可以得到SearchResult对象
c、取总记录数
d、计算总页数
e、返回SearchResult对象
package com.itheima.jd.service;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itheima.jd.dao.SearchDao;
import com.itheima.jd.pojo.SearchResult;
@Service
public class SearchService {
//设置分页中每页显示常量数
private static Integer ROWS = 10;
//注入dao
@Autowired
private SearchDao searchDao;
/**
* 查询业务逻辑
*/
public SearchResult search(String queryString, String catalog_name, String price,
int sort, int page) throws Exception{
//1.根据参数创建solrQuery对象
SolrQuery query = new SolrQuery();
//设置查询条件
if(queryString != null && !"".equals(queryString)){
query.setQuery(queryString);
}else{
query.setQuery("*:*");
}
//根据商品分类名称过滤查询
if(catalog_name != null && !"".equals(catalog_name)){
query.addFilterQuery("product_catalog_name:" + catalog_name);
}
//根据价格区间过滤
if(price != null && !"".equals(price)){
String[] strs = price.split("-");
query.addFilterQuery("product_price:[" + strs[0] + " TO " + strs[1] + "]");
}
//排序条件
if(sort == 0){
query.setSort("product_price",ORDER.asc);
}else{
query.setSort("product_price",ORDER.desc);
}
//分页组件
query.setStart((page - 1) * ROWS);
query.setRows(ROWS);
//设置默认搜索域
query.set("df","product_keywords");
//开启高亮显示
query.setHighlight(true);
query.addHighlightField("product_name");//设置高亮字段
query.setHighlightSimplePre("");//设置高亮前缀
query.setHighlightSimplePost("");//设置高亮后缀
//2.调用dao查询,获取SearchResult对象
SearchResult result = searchDao.search(query);
//3.获取总记录数
long recordCount = result.getRecordCount();
//4.计算总页数
long pageCount = recordCount / ROWS;
if(recordCount % ROWS > 0){
pageCount ++;
}
result.setPageCount(pageCount);
//5.返回SearchResult对象
return result;
}
}
package com.itheima.jd.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.itheima.jd.pojo.SearchResult;
import com.itheima.jd.service.SearchService;
@Controller
public class SearchController {
@Autowired
private SearchService searchService;
@RequestMapping("list")
public String search(String queryString, String catalog_name, String price,
@RequestParam(defaultValue="1") Integer page,
@RequestParam(defaultValue="1") Integer sort, Model model) throws Exception{
//调用service进行查询
SearchResult result = searchService.search(queryString, catalog_name, price, sort, page);
//回显结果
model.addAttribute("result",result);
model.addAttribute("queryString",queryString);
model.addAttribute("catalog_name",catalog_name);
model.addAttribute("price",price);
model.addAttribute("page",page);
model.addAttribute("sort",sort);
//返回逻辑视图
return "product_list";
}
}
url:http://pan.baidu.com/s/1gfgPIyn