首页  »   搜索引擎

solr搜索引擎_电商搜寻(java代码实现)

网友分享于:2013-03-21  浏览:0次
solr搜索引擎_电商搜索(java代码实现)
pom.xml:
<!--solr搜索引擎-->
		<dependency>
  			<groupId>org.apache.solr</groupId>
            <artifactId>solr-core</artifactId>
            <version>4.10.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
		<dependency>
		    <groupId>org.apache.solr</groupId>
		    <artifactId>solr-solrj</artifactId>
		    <version>4.10.3</version>
		</dependency>


spring-mvc.xml:
    <!-- JSP视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> //返回jsp解析用
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>


Product.java
package org.xdemo.example.SpringActivemq.model;

public class Product {
	// 商品编号
	private String id;
	// 商品名称(发送solr请求用)
	private String product_name;
	// 商品分类名称
	private String catalog_name;
	// 价格
	private float product_price;
	// 商品描述
	private String description;
	// 图片名称
	private String product_picture;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getProduct_name() {
		return product_name;
	}

	public void setProduct_name(String product_name) {
		this.product_name = product_name;
	}

	public String getCatalog_name() {
		return catalog_name;
	}

	public void setCatalog_name(String catalog_name) {
		this.catalog_name = catalog_name;
	}

	public float getProduct_price() {
		return product_price;
	}

	public void setProduct_price(float product_price) {
		this.product_price = product_price;
	}

	public String getProduct_picture() {
		return product_picture;
	}

	public void setProduct_picture(String product_picture) {
		this.product_picture = product_picture;
	}
}



ResultModel.java
package org.xdemo.example.SpringActivemq.model;

import java.util.List;

public class ResultModel<T> {
	// 商品列表
	private List<T> list;
	// 商品总数
	private Long recordCount;
	// 总页数
	private Long pageCount;
	// 当前页
	private Long curPage;

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	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;
	}

	public Long getCurPage() {
		return curPage;
	}

	public void setCurPage(Long curPage) {
		this.curPage = curPage;
	}

}




controller:
package org.xdemo.example.SpringActivemq.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.RequestMethod;
import org.xdemo.example.SpringActivemq.model.Product;
import org.xdemo.example.SpringActivemq.model.QueryVo;
import org.xdemo.example.SpringActivemq.model.ResultModel;
import org.xdemo.example.SpringActivemq.service.ProductService;

@Controller
@RequestMapping("/solrJD")
public class ProductController {

	@Autowired
	private ProductService productService;

	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String list(QueryVo queryVo, Model model) throws Exception {
		ResultModel<Product> resultModel = productService.findAll(queryVo);
		// 将结果存入到model域中
		model.addAttribute("list", resultModel);
		// 查询条件回显
		model.addAttribute("queryString", queryVo.getQueryString());
		model.addAttribute("catalog_name", queryVo.getCatalog_name());
		model.addAttribute("price", queryVo.getPrice());
		model.addAttribute("curPage", queryVo.getPage());
		model.addAttribute("sort", queryVo.getSort());

		return "product_list";
	}
}


ProductService.java
package org.xdemo.example.SpringActivemq.service;

import org.xdemo.example.SpringActivemq.model.Product;
import org.xdemo.example.SpringActivemq.model.QueryVo;
import org.xdemo.example.SpringActivemq.model.ResultModel;

public interface ProductService {
	/**
	 * 分页条件查询所有商品
	 * 
	 * @param solrQuery
	 * @return
	 * @throws Exception
	 */
	public ResultModel<Product> findAll(QueryVo queryVo) throws Exception;
}



ProductServiceImpl.java
package org.xdemo.example.SpringActivemq.service.impl;

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 org.xdemo.example.SpringActivemq.mapper.ProductMapper;
import org.xdemo.example.SpringActivemq.model.Product;
import org.xdemo.example.SpringActivemq.model.QueryVo;
import org.xdemo.example.SpringActivemq.model.ResultModel;
import org.xdemo.example.SpringActivemq.service.ProductService;


/**
 * 实现类
 * @author Easong
 *
 */
@Service
public class ProductServiceImpl implements ProductService {

	// 每页显示的条数
	private final static Integer PAGE_SIZE = 32;

	@Autowired
	private ProductMapper productMapper;

	@Override
	public ResultModel<Product> findAll(QueryVo queryVo) throws Exception {

		// 创建solr查询对象
		SolrQuery solrQuery = new SolrQuery();

		// 设置查询关键字
		if (queryVo.getQueryString() != null && !"".equals(queryVo.getQueryString())) {
			solrQuery.setQuery("product_name:" +queryVo.getQueryString());
		} else {
			solrQuery.setQuery("*:*");
		}

		// 设置过滤查询--商品类别
		if (queryVo.getCatalog_name() != null && !"".equals(queryVo.getCatalog_name())) {
			solrQuery.setFilterQueries("catalog_name:" + queryVo.getCatalog_name());
		}

		// 设置价格排序
		if ("1".equals(queryVo.getSort())) {
			// 升序
			solrQuery.setSort("product_price", ORDER.asc);
		} else if("0".equals(queryVo.getSort())){
			// 降序
			solrQuery.setSort("product_price", ORDER.desc);
		}
		// 设置价格查询区间
		if (queryVo.getPrice() != null && !"".equals(queryVo.getPrice())) {
			String[] split = queryVo.getPrice().split("-");
			if (split != null && split.length > 1) {
				solrQuery.setFilterQueries("product_price:[" + split[0] + " TO " + split[1] + "]");
			}
		}

		// 分页查询
		if (queryVo.getPage() == null) {
			queryVo.setPage(1);
		}
		Integer currPage = queryVo.getPage();
		// 开始索引
		solrQuery.setStart((currPage - 1) * PAGE_SIZE);
		// 每页显示条数
		solrQuery.setRows(PAGE_SIZE);

		// 设置默认搜索域
		solrQuery.set("df", "product_keywords");
		// 开启高亮显示
		solrQuery.setHighlight(true);
		// 设置显示域名
		solrQuery.addHighlightField("product_name");
		// 设置前缀
		solrQuery.setHighlightSimplePre("<span style=\"color:red\">");
		// 设置后缀
		solrQuery.setHighlightSimplePost("</span>");
		// 调用Mapper层
		ResultModel<Product> resultModel = productMapper.findAll(solrQuery);
		// 设置当前页
		resultModel.setCurPage(currPage.longValue());
		// 设置总页数
		Double pageSize = Math.ceil(resultModel.getRecordCount().doubleValue() / PAGE_SIZE);
		resultModel.setPageCount(pageSize.longValue());
		return resultModel;
	}

}


ProductMapper.java
package org.xdemo.example.SpringActivemq.mapper;

import org.apache.solr.client.solrj.SolrQuery;
import org.xdemo.example.SpringActivemq.model.Product;
import org.xdemo.example.SpringActivemq.model.ResultModel;


public interface ProductMapper {
	/**
	 * 分页条件查询所有商品
	 * @param solrQuery
	 * @return
	 * @throws Exception 
	 */
	public ResultModel<Product> findAll(SolrQuery solrQuery) throws Exception;
}


ProductMapperImpl.java
package org.xdemo.example.SpringActivemq.mapper.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.soap.Text;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Repository;
import org.xdemo.example.SpringActivemq.mapper.ProductMapper;
import org.xdemo.example.SpringActivemq.model.Product;
import org.xdemo.example.SpringActivemq.model.ResultModel;

@Repository
public class ProductMapperImpl implements ProductMapper {

	@Override
	public ResultModel<Product> findAll(SolrQuery solrQuery) throws Exception {
		// 创建连接solr服务器对象
		SolrServer solrServer = new HttpSolrServer("http://192.168.92.129:8080/solr/collection1");
		// 创建ResultModel对象
		ResultModel<Product> results = new ResultModel<Product>();
		// 创建集合,存储Product
		List<Product> productList = new ArrayList<Product>();
		// 执行查询
		QueryResponse queryResponse = solrServer.query(solrQuery);
		// 获取Document结果集
		SolrDocumentList solrDocumentList = queryResponse.getResults();
		// 设置总记录数
		results.setRecordCount(solrDocumentList.getNumFound());
		// 高亮显示
		Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
		// 遍历结果集
		for (SolrDocument doc : solrDocumentList) {
			// 创建Product对象
			Product product = new Product();

			// 设置商品的编号
			product.setId((String) doc.get("id"));
			List<String> list = highlighting.get(doc.get("id")).get("product_name");

			if (list == null) {
				// 设置商品的名称
				product.setProduct_name(doc.get("product_name").toString());
			} else {
				// 设置高亮显示名称
				product.setProduct_name(list.get(0));
			}
			
			// 设置商品分类名称
			product.setCatalog_name((String) doc.get("catalog_name"));
			// 设置商品价格
			product.setProduct_price((Float) doc.get("product_price"));
			// 设置商品图片名称
			product.setProduct_picture((String) doc.get("product_picture"));
			// 将商品添加到集合中
			product.setDescription((String) doc.get("description"));
			productList.add(product);
		}
		results.setList(productList);
		return results;
	}

}









相关解决方案

最新解决方案