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

[b]pom.xml:[/b]

<!--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>


[b]spring-mvc.xml:[/b]
    <!-- 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>


[b]Product.java[/b]

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



[b]ResultModel.java[/b]
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;
}

}


[img]http://dl2.iteye.com/upload/attachment/0123/6243/4e443484-9eab-3687-b4aa-617e9e72ab6f.png[/img]

[b]controller:[/b]
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";
}
}


[b]ProductService.java[/b]
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;
}



[b]ProductServiceImpl.java[/b]

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

}


[b]ProductMapper.java [/b]
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;
}


[b]ProductMapperImpl.java[/b]

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

}


[img]http://dl2.iteye.com/upload/attachment/0123/6179/6e587698-aeea-33d6-9bc1-b9831a1ba79d.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0123/6181/08e40ba1-e485-32cc-878a-7723d548eded.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0123/6183/73dd1e16-9481-3646-b98d-c7dbd59b5fff.png[/img]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值