电商项目第六天

电商项目第六天

solr服务器的搭建

单独文章

java配置solr接口
<bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
	<constructor-arg value="http://192.168.200.128:8080/solr" />
</bean>
前台搜索

controller层:

@Autowired
private SearchService searchService;
@Autowired
private BrandService brandService;

@RequestMapping(value="/search")
public String search(Integer pageNo,String keyword,Long brandId,String price,Model model){
	
	List<Brand> brands = brandService.selectBrandListFromRedis();
	//信息的回显
	model.addAttribute("brands", brands);
	model.addAttribute("brandId", brandId);
	model.addAttribute("price", price);
	
	//已选容器Map(已选标签回显)
	Map<String,String> map = new HashMap<String,String>();
	if(null!=brandId){
		for (Brand brand : brands) {
			if(brandId == brand.getId()){
				map.put("品牌", brand.getName());
				break;
			}
		}
	}		
	if(null != price&& "" !=price){
		if(price.contains("-")){
			map.put("价格", price);
		}else{
			map.put("价格", price+"以上");
		}
	}
	model.addAttribute("map", map);
	Pagination pagination = searchService.selectPaginationListByQuery(pageNo, keyword,brandId,price);
	model.addAttribute("pagination",pagination);
	return "search";
}

servie:

@Autowired
private SolrServer solrServer;

//全文检索
public Pagination selectPaginationListByQuery(Integer pageNo,String keyword,Long brandId,String price){
	List<Product> products = new ArrayList<Product>();
	ProductQuery productQuery = new ProductQuery();
	productQuery.setPageNo(Pagination.cpn(pageNo));
	productQuery.setPageSize(15);
	
	StringBuilder params = new StringBuilder();
	
	SolrQuery solrQuery = new SolrQuery();
	
	//关键词
	solrQuery.set("q", "name_ik:"+keyword);
	params.append("keyword=").append(keyword);
	//过滤条件
	if(null != brandId){
		solrQuery.addFilterQuery("brandId:"+brandId);
	}
	if(null != price){
		String[] split = price.split("-");
		if(split.length == 2){
			solrQuery.addFilterQuery("price:["+split[0]+" TO "+split[1]+"]");
		}else{
			solrQuery.addFilterQuery("price:["+split[0]+" TO *]");
		}
	}
	//高亮
	solrQuery.setHighlight(true);
	solrQuery.addHighlightField("name_ik");
	solrQuery.setHighlightSimplePre("<span style='color:red'>");
	solrQuery.setHighlightSimplePost("</span>");
	//排序
	solrQuery.addSort("price", ORDER.asc);
	//分页
	solrQuery.setStart(productQuery.getStartRow());
	solrQuery.setRows(productQuery.getPageSize());
	
	long numFound=0l;
	try {
		//执行查询
		QueryResponse response = solrServer.query(solrQuery);
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		//结果集
		SolrDocumentList docs = response.getResults();
		//获取分页条数
		numFound = docs.getNumFound();
		
		for (SolrDocument doc : docs) {
			Product p = new Product();
			p.setId(Long.parseLong((String)doc.get("id")));
			/*p.setName((String)doc.get("name_ik"));*/
			//高亮
			Map<String, List<String>> map = highlighting.get((String)doc.get("id"));
			List<String> list = map.get("name_ik");
			p.setName(list.get(0));
			p.setImgUrl((String)doc.get("url"));
			p.setPrice((Float)doc.get("price"));
			p.setBrandId(Long.parseLong(String.valueOf((Integer)doc.get("brandId"))));
			products.add(p);
		}
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	Pagination pagination = new Pagination(
			productQuery.getPageNo(),
			productQuery.getPageSize(),
			(int)numFound,
			products
			);
	
	pagination.pageView("/search", params.toString());
	return pagination;
}

第六天结束
所学知识:

  1. Solr服务器搭建与使用
  2. 前台业务流程

遇到错误:

  1. 按品牌筛选失败(原因,标签混淆,应用 addFilterQuery而不是 addFacetQuery)
  2. solr配置不够熟识,还需温习与练习

致谢晓欣老师

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值